o
    ÆhJ                     @  s0  d dl mZ d dlmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ ejr[d d
lmZ ddlmZ G dd dZejd%ddZG dd dejZG dd dZG dd dej Z!d&dd Z"G d!d" d"Z#G d#d$ d$Z$dS )'    )annotationsN)TracebackType   )_compat)
formatting)termui)utils)_find_binary_reader)ReadableBuffer)Commandc                   @  sl   e Zd Zd#ddZd$ddZd%ddZd&d'ddZd&d'ddZd&d'ddZd(ddZ	d)ddZ
d*d d!Zd"S )+EchoingStdininput
t.BinaryIOoutputreturnNonec                 C  s   || _ || _d| _d S )NF)_input_output_paused)selfr   r    r   O/var/www/html/pro-man-master/venv/lib/python3.10/site-packages/click/testing.py__init__   s   
zEchoingStdin.__init__xstrt.Anyc                 C  s   t | j|S N)getattrr   )r   r   r   r   r   __getattr__   s   zEchoingStdin.__getattr__rvbytesc                 C  s   | j s	| j| |S r   )r   r   write)r   r   r   r   r   _echo"   s   zEchoingStdin._echonintc                 C     |  | j|S r   )r"   r   readr   r$   r   r   r   r'   (      zEchoingStdin.readc                 C  r&   r   )r"   r   read1r(   r   r   r   r*   +   r)   zEchoingStdin.read1c                 C  r&   r   )r"   r   readliner(   r   r   r   r+   .   r)   zEchoingStdin.readlinelist[bytes]c                   s    fdd j  D S )Nc                   s   g | ]}  |qS r   r"   .0r   r   r   r   
<listcomp>2   s    z*EchoingStdin.readlines.<locals>.<listcomp>)r   	readlinesr0   r   r0   r   r2   1      zEchoingStdin.readlinescabc.Iterator[bytes]c                   s   t  fdd jD S )Nc                 3  s    | ]}  |V  qd S r   r-   r.   r0   r   r   	<genexpr>5   s    z(EchoingStdin.__iter__.<locals>.<genexpr>)iterr   r0   r   r0   r   __iter__4   r3   zEchoingStdin.__iter__c                 C  s
   t | jS r   )reprr   r0   r   r   r   __repr__7   s   
zEchoingStdin.__repr__N)r   r   r   r   r   r   )r   r   r   r   )r   r    r   r    )r#   )r$   r%   r   r    )r   r,   )r   r4   r   r   )__name__
__module____qualname__r   r   r"   r'   r*   r+   r2   r7   r9   r   r   r   r   r      s    




r   streamEchoingStdin | Noner   cabc.Iterator[None]c                 c  s*    | d u r
d V  d S d| _ d V  d| _ d S )NTF)r   )r>   r   r   r   _pause_echo;   s   

rA   c                      s>   e Zd ZdZd fddZd fdd	Zd fddZ  ZS )BytesIOCopyzdPatch ``io.BytesIO`` to let the written stream be copied to another.

    .. versionadded:: 8.2
    copy_to
io.BytesIOr   r   c                   s   t    || _d S r   )superr   rC   )r   rC   	__class__r   r   r   K   s   

zBytesIOCopy.__init__c                   s   t    | j  d S r   )rE   flushrC   r0   rF   r   r   rH   O   s   
zBytesIOCopy.flushbr
   r%   c                   s   | j | t |S r   )rC   r!   rE   )r   rI   rF   r   r   r!   S   s   zBytesIOCopy.write)rC   rD   r   r   r   r   )rI   r
   r   r%   )r;   r<   r=   __doc__r   rH   r!   __classcell__r   r   rF   r   rB   E   s
    rB   c                   @  s$   e Zd ZdZd	ddZd	ddZdS )
StreamMixerzMixes `<stdout>` and `<stderr>` streams.

    The result is available in the ``output`` attribute.

    .. versionadded:: 8.2
    r   r   c                 C  s*   t  | _t| jd| _t| jd| _d S )N)rC   )ioBytesIOr   rB   stdoutstderrr0   r   r   r   r   `   s   
zStreamMixer.__init__c                 C  s"   | j   | j  | j  dS )z
        Guarantee that embedded file-like objects are closed in a
        predictable order, protecting against races between
        self.output being closed and other streams being flushed on close

        .. versionadded:: 8.2.2
        N)rQ   closerP   r   r0   r   r   r   __del__e   s   

zStreamMixer.__del__NrJ   )r;   r<   r=   rK   r   rS   r   r   r   r   rM   X   s    
rM   c                      s:   e Zd Zd fd
dZedddZedddZ  ZS )_NamedTextIOWrapperbufferr   namer   modekwargsr   r   r   c                   s$   t  j|fi | || _|| _d S r   )rE   r   _name_mode)r   rU   rV   rW   rX   rF   r   r   r   s   s   
z_NamedTextIOWrapper.__init__c                 C     | j S r   )rY   r0   r   r   r   rV   z      z_NamedTextIOWrapper.namec                 C  r[   r   )rZ   r0   r   r   r   rW   ~   r\   z_NamedTextIOWrapper.mode)
rU   r   rV   r   rW   r   rX   r   r   r   r:   )r;   r<   r=   r   propertyrV   rW   rL   r   r   rF   r   rT   r   s    rT   r    str | bytes | t.IO[t.Any] | Nonecharsetr   r   c                 C  sZ   t | drttd| }|d ur|S td| d u rd} n
t| tr(| |} t	| S )Nr'   zt.IO[t.Any]z.Could not find binary reader for input stream.    )
hasattrr	   tcast	TypeError
isinstancer   encoderN   rO   )r   r_   r   r   r   r   make_input_stream   s   



rg   c                   @  sR   e Zd ZdZ	ddddZedddZedddZedddZdddZ	dS ) ResultaC  Holds the captured result of an invoked CLI script.

    :param runner: The runner that created the result
    :param stdout_bytes: The standard output as bytes.
    :param stderr_bytes: The standard error as bytes.
    :param output_bytes: A mix of ``stdout_bytes`` and ``stderr_bytes``, as the
        user would see  it in its terminal.
    :param return_value: The value returned from the invoked command.
    :param exit_code: The exit code as integer.
    :param exception: The exception that happened if one did.
    :param exc_info: Exception information (exception type, exception instance,
        traceback type).

    .. versionchanged:: 8.2
        ``stderr_bytes`` no longer optional, ``output_bytes`` introduced and
        ``mix_stderr`` has been removed.

    .. versionadded:: 8.0
        Added ``return_value``.
    Nrunner	CliRunnerstdout_bytesr    stderr_bytesoutput_bytesreturn_valuer   	exit_coder%   	exceptionBaseException | Noneexc_info?tuple[type[BaseException], BaseException, TracebackType] | Nonec	           	      C  s4   || _ || _|| _|| _|| _|| _|| _|| _d S r   ri   rk   rl   rm   rn   ro   rp   rr   )	r   ri   rk   rl   rm   rn   ro   rp   rr   r   r   r   r      s   
zResult.__init__r   r   c                 C     | j | jjdddS )a  The terminal output as unicode string, as the user would see it.

        .. versionchanged:: 8.2
            No longer a proxy for ``self.stdout``. Now has its own independent stream
            that is mixing `<stdout>` and `<stderr>`, in the order they were written.
        replace

)rm   decoderi   r_   rv   r0   r   r   r   r      s   zResult.outputc                 C  ru   )z&The standard output as unicode string.rv   rw   rx   )rk   ry   ri   r_   rv   r0   r   r   r   rP      s   zResult.stdoutc                 C  ru   )zThe standard error as unicode string.

        .. versionchanged:: 8.2
            No longer raise an exception, always returns the `<stderr>` string.
        rv   rw   rx   )rl   ry   ri   r_   rv   r0   r   r   r   rQ      s   zResult.stderrc                 C  s,   | j rt| j nd}dt| j d| dS )Nokay< >)rp   r8   typer;   )r   exc_strr   r   r   r9      s   zResult.__repr__r   )ri   rj   rk   r    rl   r    rm   r    rn   r   ro   r%   rp   rq   rr   rs   r:   )
r;   r<   r=   rK   r   r]   r   rP   rQ   r9   r   r   r   r   rh      s    
rh   c                   @  s~   e Zd ZdZ				d,d-ddZd.ddZ	d/d0ddZej			d1d2ddZ						d3d4d%d&Z
ej	d/d5d*d+ZdS )6rj   a  The CLI runner provides functionality to invoke a Click command line
    script for unittesting purposes in a isolated environment.  This only
    works in single-threaded systems without any concurrency as it changes the
    global interpreter state.

    :param charset: the character set for the input and output data.
    :param env: a dictionary with environment variables for overriding.
    :param echo_stdin: if this is set to `True`, then reading from `<stdin>` writes
                       to `<stdout>`.  This is useful for showing examples in
                       some circumstances.  Note that regular prompts
                       will automatically echo the input.
    :param catch_exceptions: Whether to catch any exceptions other than
                             ``SystemExit`` when running :meth:`~CliRunner.invoke`.

    .. versionchanged:: 8.2
        Added the ``catch_exceptions`` parameter.

    .. versionchanged:: 8.2
        ``mix_stderr`` parameter has been removed.
    utf-8NFTr_   r   env$cabc.Mapping[str, str | None] | None
echo_stdinboolcatch_exceptionsr   r   c                 C  s    || _ |pi | _|| _|| _d S r   )r_   r   r   r   )r   r_   r   r   r   r   r   r   r      s   

zCliRunner.__init__clir   c                 C  s
   |j pdS )zGiven a command object it will return the default program name
        for it.  The default is the `name` attribute or ``"root"`` if not
        set.
        root)rV   )r   r   r   r   r   get_default_prog_name  s   
zCliRunner.get_default_prog_name	overridescabc.Mapping[str, str | None]c                 C  s   t | j}|r|| |S )z8Returns the environment overrides for invoking a script.)dictr   update)r   r   r   r   r   r   make_env  s   

zCliRunner.make_envr   r^   color8cabc.Iterator[tuple[io.BytesIO, io.BytesIO, io.BytesIO]]c                 #  s   t || j}d}tj}tj}tj}tj}	dt_| |}t	 }
| j
r0ttjt||
j }}t|| jddd t_| j
rBd_t|
j| jdddt_t|
j| jd	dd
dt_t|ddfdd}t|ddfdd}t|d dd}| 	d!d" fdd}tj}tj}tj}tj}tj}|t_|t_|t_|t_|t_i }zs| D ]%\}}tj|||< |du rztj|= W q ty   Y qw |tj|< q|
j|
j|
jfV  W | D ]\}}|du rztj|= W q ty   Y qw |tj|< q|t_|t_|t_|t_|t_|t_|t_|t_|	t_dS | D ]"\}}|du r9ztj|= W q ty8   Y qw |tj|< q|t_|t_|t_|t_|t_|t_|t_|t_|	t_w )#a\  A context manager that sets up the isolation for invoking of a
        command line tool.  This sets up `<stdin>` with the given input data
        and `os.environ` with the overrides from the given dictionary.
        This also rebinds some internals in Click to be mocked (like the
        prompt functionality).

        This is automatically done in the :meth:`invoke` method.

        :param input: the input stream to put into `sys.stdin`.
        :param env: the environment overrides as dictionary.
        :param color: whether the output should contain color codes. The
                      application can still override this explicitly.

        .. versionadded:: 8.2
            An additional output stream is returned, which is a mix of
            `<stdout>` and `<stderr>` streams.

        .. versionchanged:: 8.2
            Always returns the `<stderr>` stream.

        .. versionchanged:: 8.0
            `<stderr>` is opened with ``errors="backslashreplace"``
            instead of the default ``"strict"``.

        .. versionchanged:: 4.0
            Added the ``color`` parameter.
        NP   z<stdin>r)encodingrV   rW   r   z<stdout>wz<stderr>backslashreplace)r   rV   rW   errorsprompt
str | Noner   r   c              
     sd   t j| pd z	t d}W n ty! } zt |d }~ww t j| d t j  |S )N rw   rx   )sysrP   r!   nextrstripStopIterationEOFErrorrH   )r   vale
text_inputr   r   visible_input`  s   
z*CliRunner.isolation.<locals>.visible_inputc              
     sR   t j| pd d t j  zt dW S  ty( } zt |d }~ww )Nr   rx   rw   )r   rP   r!   rH   r   r   r   r   )r   r   r   r   r   hidden_inputk  s   
z)CliRunner.isolation.<locals>.hidden_inputechor   c                 S  s*   t jd}| rt j| t j  |S )Nr   )r   stdinr'   rP   r!   rH   )r   charr   r   r   _getchart  s
   
z%CliRunner.isolation.<locals>._getcharr>   t.IO[t.Any] | Noner   bool | Nonec                   s   |d u r  S | S r   r   )r>   r   )default_colorr   r   should_strip_ansi  s   z.CliRunner.isolation.<locals>.should_strip_ansir   )r   r   r   r   )r   r   r   r   )NN)r>   r   r   r   r   r   )rg   r_   r   r   rP   rQ   r   FORCED_WIDTHr   rM   r   rb   rc   BinaryIOr   rT   _CHUNK_SIZErA   r   visible_prompt_funchidden_prompt_funcr   r   r   r   itemsosenvironget	Exceptionr   )r   r   r   r   bytes_input
echo_input	old_stdin
old_stdout
old_stderrold_forced_widthstream_mixerr   r   r   r   old_visible_prompt_funcold_hidden_prompt_funcold__getchar_funcold_should_strip_ansiold__compat_should_strip_ansiold_envkeyvaluer   )r   r   r   	isolation  s   "


	

zCliRunner.isolationargsstr | cabc.Sequence[str] | Noner   extrar   rh   c                 K  s  d}|du r	| j }| j|||d}	d}
d}d}t|tr"t|}z|d}W n ty7   | |}Y nw zz|j	d|p@d|d|}
W n\ t
y } z4t }td|j}|du rad}|dkrg|}t|ts|tjt| tjd d	}|}W Y d}~n!d}~w ty } z|s |}d	}t }W Y d}~nd}~ww W tj  tj  |	d  }|	d	  }|	d
  }ntj  tj  |	d  }|	d	  }|	d
  }w W d   n1 sw   Y  t| ||||
|||dS )a{  Invokes a command in an isolated environment.  The arguments are
        forwarded directly to the command line script, the `extra` keyword
        arguments are passed to the :meth:`~clickpkg.Command.main` function of
        the command.

        This returns a :class:`Result` object.

        :param cli: the command to invoke
        :param args: the arguments to invoke. It may be given as an iterable
                     or a string. When given as string it will be interpreted
                     as a Unix shell command. More details at
                     :func:`shlex.split`.
        :param input: the input data for `sys.stdin`.
        :param env: the environment overrides.
        :param catch_exceptions: Whether to catch any other exceptions than
                                 ``SystemExit``. If :data:`None`, the value
                                 from :class:`CliRunner` is used.
        :param extra: the keyword arguments to pass to :meth:`main`.
        :param color: whether the output should contain color codes. The
                      application can still override this explicitly.

        .. versionadded:: 8.2
            The result object has the ``output_bytes`` attribute with
            the mix of ``stdout_bytes`` and ``stderr_bytes``, as the user would
            see it in its terminal.

        .. versionchanged:: 8.2
            The result object always returns the ``stderr_bytes`` stream.

        .. versionchanged:: 8.0
            The result object has the ``return_value`` attribute with
            the value returned from the invoked command.

        .. versionchanged:: 4.0
            Added the ``color`` parameter.

        .. versionchanged:: 3.0
            Added the ``catch_exceptions`` parameter.

        .. versionchanged:: 3.0
            The result object has the ``exc_info`` attribute with the
            traceback if available.
        N)r   r   r   r   	prog_namer   )r   r   zint | t.Any | Nonerx   r      rt   )r   r   re   r   shlexsplitpopKeyErrorr   main
SystemExitr   rr   rb   rc   coder%   rP   r!   r   rH   rQ   getvaluerh   )r   r   r   r   r   r   r   r   rr   
outstreamsrn   rp   ro   r   r   e_coderP   rQ   r   r   r   r   invoke  sv   5






-zCliRunner.invoketemp_dirstr | os.PathLike[str] | Nonecabc.Iterator[str]c                 c  s    t  }tj|d}t | z&|V  W t | |du r5ddl}z|| W dS  ty4   Y dS w dS t | |du rUddl}z|| W w  tyT   Y w w w )a  A context manager that creates a temporary directory and
        changes the current working directory to it. This isolates tests
        that affect the contents of the CWD to prevent them from
        interfering with each other.

        :param temp_dir: Create the temporary directory under this
            directory. If given, the created directory is not removed
            when exiting.

        .. versionchanged:: 8.0
            Added the ``temp_dir`` parameter.
        )dirNr   )r   getcwdtempfilemkdtempchdirshutilrmtreeOSError)r   r   cwddtr   r   r   r   isolated_filesystem"  s0   


zCliRunner.isolated_filesystem)r   NFT)
r_   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   )NNF)r   r^   r   r   r   r   r   r   )NNNNF)r   r   r   r   r   r^   r   r   r   r   r   r   r   r   r   rh   )r   r   r   r   )r;   r<   r=   rK   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   rj      s2    
	 qrj   )r>   r?   r   r@   )r   r^   r_   r   r   r   )%
__future__r   collections.abcabccabcr   rN   r   r   r   r   typingrb   typesr   r   r   r   r   r   r	   TYPE_CHECKING	_typeshedr
   corer   r   r   rA   rO   rB   rM   TextIOWrapperrT   rg   rh   rj   r   r   r   r   <module>   s6    "	
N