o
    [ÞhžJ  ã                   @  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   úM/var/www/html/premium_crap/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 )
ÚStreamMixerz‚Mixes `<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   )zŸThe 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 )zžGiven 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^   Úcolorú8cabc.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 rÊztj|= W qª tyÉ   Y qªw |tj|< qª|
j|
j|
jfV  W | ¡ D ]\}}|du röztj|= W qÞ tyõ   Y qÞw |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.
        NéP   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 zŠz|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 sêw   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