o
    Õ­jš?  ã                   @   s0  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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rMdd	lmZ G d
d„ dƒZe jdeje dejd fdd„ƒZG dd„ dejƒZdejejeeejej f  dedej fdd„Z!G dd„ dƒZ"G dd„ dƒZ#dS )é    N)ÚTracebackTypeé   )Ú_compat)Ú
formatting)Útermui)Úutils)Ú_find_binary_reader)ÚBaseCommandc                   @   s¾   e Zd Zdejdejddfdd„Zdedejfdd	„Zd
e	de	fdd„Z
ddede	fdd„Zddede	fdd„Zddede	fdd„Zdeje	 fdd„Zdeje	 fdd„Zdefdd„ZdS )ÚEchoingStdinÚinputÚoutputÚreturnNc                 C   s   || _ || _d| _d S )NF)Ú_inputÚ_outputÚ_paused)Úselfr   r   © r   úJ/var/www/html/CropPilot/venv/lib/python3.10/site-packages/click/testing.pyÚ__init__   s   
zEchoingStdin.__init__Úxc                 C   s   t | j|ƒS ©N)Úgetattrr   )r   r   r   r   r   Ú__getattr__   s   zEchoingStdin.__getattr__Úrvc                 C   s   | j s	| j |¡ |S r   )r   r   Úwrite)r   r   r   r   r   Ú_echo   s   zEchoingStdin._echoéÿÿÿÿÚnc                 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.readlinec                    s   ‡ fdd„ˆ j  ¡ D ƒS )Nc                    s   g | ]}ˆ   |¡‘qS r   ©r   ©Ú.0r   ©r   r   r   Ú
<listcomp>.   s    z*EchoingStdin.readlines.<locals>.<listcomp>)r   Ú	readlinesr'   r   r'   r   r)   -   ó   zEchoingStdin.readlinesc                    s   t ‡ fdd„ˆ jD ƒƒS )Nc                 3   s   � | ]}ˆ   |¡V  qd S r   r$   r%   r'   r   r   Ú	<genexpr>1   s   € z(EchoingStdin.__iter__.<locals>.<genexpr>)Úiterr   r'   r   r'   r   Ú__iter__0   r*   zEchoingStdin.__iter__c                 C   s
   t | jƒS r   )Úreprr   r'   r   r   r   Ú__repr__3   s   
zEchoingStdin.__repr__)r   )Ú__name__Ú
__module__Ú__qualname__ÚtÚBinaryIOr   ÚstrÚAnyr   Úbytesr   Úintr   r"   r#   ÚListr)   ÚIteratorr-   r/   r   r   r   r   r
      s    r
   Ústreamr   c                 c   s*   � | d u r
d V  d S d| _ d V  d| _ d S )NTF)r   )r;   r   r   r   Ú_pause_echo7   s   €

r<   c                
       sZ   e Zd Zdejdededejddf
‡ fdd„Zedefd	d
„ƒZ	edefdd„ƒZ
‡  ZS )Ú_NamedTextIOWrapperÚbufferÚnameÚmodeÚkwargsr   Nc                    s$   t ƒ j|fi |¤Ž || _|| _d S r   )Úsuperr   Ú_nameÚ_mode)r   r>   r?   r@   rA   ©Ú	__class__r   r   r   B   s   
z_NamedTextIOWrapper.__init__c                 C   ó   | j S r   )rC   r'   r   r   r   r?   I   ó   z_NamedTextIOWrapper.namec                 C   rG   r   )rD   r'   r   r   r   r@   M   rH   z_NamedTextIOWrapper.mode)r0   r1   r2   r3   r4   r5   r6   r   Úpropertyr?   r@   Ú__classcell__r   r   rE   r   r=   A   s     ÿÿÿÿþr=   r   Úcharsetc                 C   sb   t | dƒrtt tjtj | ¡ƒ}|d ur|S tdƒ‚| d u r"d} n
t| tƒr,|  	|¡} t
 | ¡S )Nr   z.Could not find binary reader for input stream.ó    )Úhasattrr   r3   ÚcastÚIOr6   Ú	TypeErrorÚ
isinstancer5   ÚencodeÚioÚBytesIO)r   rK   r   r   r   r   Úmake_input_streamR   s   



rU   c                   @   s¤   e Zd ZdZ	ddddedeje dejded	eje	 d
ejej
eje	 e	ef  fdd„Zedefdd„ƒZedefdd„ƒZedefdd„ƒZdefdd„ZdS )ÚResultz3Holds the captured result of an invoked CLI script.NÚrunnerÚ	CliRunnerÚstdout_bytesÚstderr_bytesÚreturn_valueÚ	exit_codeÚ	exceptionÚexc_infoc                 C   s.   || _ || _|| _|| _|| _|| _|| _d S r   ©rW   rY   rZ   r[   r\   r]   r^   )r   rW   rY   rZ   r[   r\   r]   r^   r   r   r   r   i   s   
zResult.__init__r   c                 C   rG   )z(The (standard) output as unicode string.)Ústdoutr'   r   r   r   r   †   s   zResult.outputc                 C   s   | j  | jjd¡ dd¡S )z&The standard output as unicode string.Úreplaceú
Ú
)rY   ÚdecoderW   rK   ra   r'   r   r   r   r`   ‹   s   ÿzResult.stdoutc                 C   s,   | j du r	tdƒ‚| j  | jjd¡ dd¡S )z%The standard error as unicode string.Nzstderr not separately capturedra   rb   rc   )rZ   Ú
ValueErrorrd   rW   rK   ra   r'   r   r   r   Ústderr’   s
   
ÿzResult.stderrc                 C   s,   | j rt| j ƒnd}dt| ƒj› d|› d�S )NÚokayú<ú ú>)r]   r.   Útyper0   )r   Úexc_strr   r   r   r/   ›   s   zResult.__repr__r   )r0   r1   r2   Ú__doc__r7   r3   ÚOptionalr6   r8   ÚBaseExceptionÚTupleÚTyper   r   rI   r5   r   r`   rf   r/   r   r   r   r   rV   f   s6    öþýüûúùÿ
ørV   c                   @   s¾  e Zd ZdZ				d!dedejejeeje f  ded	ed
df
dd„Z	ddd
efdd„Z
	d"dejejeeje f  d
ejeeje f fdd„Zej			d#dejejeeejej f  dejejeeje f  ded
ejejejejej f  fdd„ƒZ					d$dddejejeeje f  dejejeeejej f  dejejeeje f  dededejd
efdd„Zej	d"dejejedf  d
eje fdd „ƒZdS )%rX   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 mix_stderr: if this is set to `False`, then stdout and stderr are
                       preserved as independent streams.  This is useful for
                       Unix-philosophy apps that have predictable stdout and
                       noisy stderr, such that each may be measured
                       independently
    úutf-8NFTrK   ÚenvÚ
echo_stdinÚ
mix_stderrr   c                 C   s    || _ |pi | _|| _|| _d S r   )rK   rs   rt   ru   )r   rK   rs   rt   ru   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)r?   )r   rv   r   r   r   Úget_default_prog_name¿   s   
zCliRunner.get_default_prog_nameÚ	overridesc                 C   s   t | jƒ}|r| |¡ |S )z8Returns the environment overrides for invoking a script.)Údictrs   Úupdate)r   ry   r   r   r   r   Úmake_envÆ   s   

zCliRunner.make_envr   Úcolorc                 #   s  � t || jƒ}d}tj}tj}tj}tj}	dt_|  |¡}t	 
¡ }
| jr0t tjt||
ƒ¡ }}t|| jddd� t_‰| jrBdˆ_t|
| jddd�t_d}| jrVtjt_nt	 
¡ }t|| jd	dd
d�t_t|ƒddtjt dtf‡fdd„ƒ}t|ƒddtjt dtf‡fdd„ƒ}t|ƒdtdtfdd„ƒ}|‰ 	ddtjtjtj  dtjt dtf‡ fdd„}tj}tj}tj}tj}tj}|t_|t_|t_|t_|t_i }zt|  ¡ D ]%\}}t!j" #|¡||< |du røzt!j"|= W qØ t$y÷   Y qØw |t!j"|< qØ|
|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 �rhzt!j"|= W �qL t$�yg   Y �qLw |t!j"|< �qL|t_|t_|t_|t_|t_|t_|t_|t_|	t_w )an  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.

        .. 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)Úencodingr?   r@   r   z<stdout>Úwz<stderr>Úbackslashreplace)r€   r?   r@   ÚerrorsÚpromptr   c                    s>   t j | pd¡ ˆ  ¡  d¡}t j |› d�¡ t j ¡  |S )NÚ rb   rc   )Úsysr`   r   r#   ÚrstripÚflush)r„   Úval©Ú
text_inputr   r   Úvisible_input  s
   
z*CliRunner.isolation.<locals>.visible_inputc                    s.   t j | pd› d�¡ t j ¡  ˆ  ¡  d¡S )Nr…   rc   rb   )r†   r`   r   rˆ   r#   r‡   )r„   rŠ   r   r   Úhidden_input  s   
z)CliRunner.isolation.<locals>.hidden_inputÚechoc                 S   s*   t j d¡}| rt j |¡ t j ¡  |S )Nr   )r†   Ústdinr   r`   r   rˆ   )rŽ   Úcharr   r   r   Ú_getchar$  s
   
z%CliRunner.isolation.<locals>._getcharr;   r}   c                    s   |d u rˆ  S | S r   r   )r;   r}   )Údefault_colorr   r   Úshould_strip_ansi0  s   z.CliRunner.isolation.<locals>.should_strip_ansir   )NN)%rU   rK   r†   r�   r`   rf   r   ÚFORCED_WIDTHr|   rS   rT   rt   r3   rN   r4   r
   r=   Ú_CHUNK_SIZEru   r<   rn   r5   ÚboolrO   r6   r   Úvisible_prompt_funcÚhidden_prompt_funcr‘   r   r“   r   ÚitemsÚosÚenvironÚgetÚ	Exception)r   r   rs   r}   Úbytes_inputÚ
echo_inputÚ	old_stdinÚ
old_stdoutÚ
old_stderrÚold_forced_widthÚbytes_outputÚbytes_errorrŒ   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Úcatch_exceptionsÚextrac                 K   sä  d}| j |||d��Õ}	d}
d}d}t|tƒrt |¡}z| d¡}W n ty0   |  |¡}Y nw zŽz|jd|p9d|dœ|¤Ž}
W ne t	yŠ } z=t
 ¡ }t tjtjttjf  |j¡}|du rcd}|dkri|}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 ¡  |	d  ¡ }| jr¹d}n|	d  ¡ }nt
j ¡  |	d  ¡ }| jrÑd}w |	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``.
        :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.

        .. 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   rs   r}   r   Ú	prog_namer   )r¯   r²   rc   r   r_   )r®   rQ   r5   ÚshlexÚsplitÚpopÚKeyErrorrx   ÚmainÚ
SystemExitr†   r^   r3   rN   rn   ÚUnionr8   r6   Úcoder`   r   r�   rˆ   Úgetvalueru   rV   )r   rv   r¯   r   rs   r°   r}   r±   r^   Ú
outstreamsr[   r]   r\   r²   ÚeÚe_coder`   rf   r   r   r   Úinvokea  sr   ,

ÿ 
€€û€

û€Ô.ùzCliRunner.invokeÚtemp_dirzos.PathLike[str]c                 c   sœ   � t  ¡ }tj|d�}t  |¡ z"|V  W t  |¡ |du r1zt |¡ W dS  ty0   Y dS w dS t  |¡ |du rMzt |¡ W w  tyL   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.
        )ÚdirN)rš   ÚgetcwdÚtempfileÚmkdtempÚchdirÚshutilÚrmtreeÚOSError)r   rÀ   ÚcwdÚdtr   r   r   Úisolated_filesystemÆ  s,   €

ÿý
þÿýzCliRunner.isolated_filesystem)rr   NFTr   )NNF)NNNTF)r0   r1   r2   rm   r5   r3   rn   ÚMappingr–   r   rx   r|   Ú
contextlibÚcontextmanagerr¹   r7   rO   r6   r:   rp   rS   rT   r®   ÚSequencerV   r¿   rË   r   r   r   r   rX       s†    ûþýüû
úÿÿ
þ	üþýüû ùþýüûúùø	
÷eÿÿþrX   )$rÍ   rS   rš   r³   rÆ   r†   rÃ   Útypingr3   Útypesr   r…   r   r   r   r   r   ÚTYPE_CHECKINGÚcorer	   r
   rÎ   rn   r:   r<   ÚTextIOWrapperr=   r¹   r5   r7   rO   r6   r4   rU   rV   rX   r   r   r   r   Ú<module>   s:    " 	ÿÿ
þ: