
    h?                        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"                  rdd	lmZ  G d
 d      Ze j*                  dej,                  e   dej.                  d   fd       Z G d dej2                        Zdej,                  ej6                  eeej<                  ej>                     f      dedej@                  fdZ! G d d      Z" G d d      Z#y)    N)TracebackType   )_compat)
formatting)termui)utils)_find_binary_reader)BaseCommandc                   
   e Zd Zdej                  dej                  ddfdZdedej                  fdZde	de	fd	Z
dd
ede	fdZdd
ede	fdZdd
ede	fdZdej                  e	   fdZdej"                  e	   fdZdefdZy)EchoingStdininputoutputreturnNc                 .    || _         || _        d| _        y )NF)_input_output_paused)selfr   r   s      6D:\EasyAligner\venv\Lib\site-packages\click/testing.py__init__zEchoingStdin.__init__   s        xc                 .    t        | j                  |      S N)getattrr   r   r   s     r   __getattr__zEchoingStdin.__getattr__   s    t{{A&&r   rvc                 T    | j                   s| j                  j                  |       |S r   )r   r   write)r   r   s     r   _echozEchoingStdin._echo   s     ||LLr"	r   nc                 V    | j                  | j                  j                  |            S r   )r!   r   readr   r"   s     r   r$   zEchoingStdin.read$   s     zz$++**1-..r   c                 V    | j                  | j                  j                  |            S r   )r!   r   read1r%   s     r   r'   zEchoingStdin.read1'   s     zz$++++A.//r   c                 V    | j                  | j                  j                  |            S r   )r!   r   readliner%   s     r   r)   zEchoingStdin.readline*   s     zz$++..q122r   c                 z    | j                   j                         D cg c]  }| j                  |       c}S c c}w r   )r   	readlinesr!   r   s     r   r+   zEchoingStdin.readlines-   s1    '+{{'<'<'>?'>!

1'>???s   8c                 @     t         fd j                  D              S )Nc              3   @   K   | ]  }j                  |        y wr   )r!   ).0r   r   s     r   	<genexpr>z(EchoingStdin.__iter__.<locals>.<genexpr>1   s     7;aDJJqM;s   )iterr   r   s   `r   __iter__zEchoingStdin.__iter__0   s    74;;777r   c                 ,    t        | j                        S r   )reprr   r1   s    r   __repr__zEchoingStdin.__repr__3   s    DKK  r   ))__name__
__module____qualname__tBinaryIOr   strAnyr   bytesr!   intr$   r'   r)   Listr+   Iteratorr2   r5    r   r   r   r      s    ajj !**  
'S 'QUU ' % /c /5 /0s 0E 03# 3u 3@166%= @8!**U+ 8!# !r   r   streamr   c              #   >   K   | d  y d| _         d  d| _         y w)NTF)r   )rC   s    r   _pause_echorE   7   s     ~s   c            
            e Zd Zdej                  dededej                  ddf
 fdZedefd       Z	edefd	       Z
 xZS )
_NamedTextIOWrapperbuffernamemodekwargsr   Nc                 B    t        |   |fi | || _        || _        y r   )superr   _name_mode)r   rH   rI   rJ   rK   	__class__s        r   r   z_NamedTextIOWrapper.__init__B   s%     	*6*

r   c                     | j                   S r   )rN   r1   s    r   rI   z_NamedTextIOWrapper.nameI       zzr   c                     | j                   S r   )rO   r1   s    r   rJ   z_NamedTextIOWrapper.modeM   rR   r   )r7   r8   r9   r:   r;   r<   r=   r   propertyrI   rJ   __classcell__)rP   s   @r   rG   rG   A   sn    jj(+36BC%%	 c   c  r   rG   r   charsetc                 *   t        | d      rMt        t        j                  t        j                  t        j
                     |             }||S t        d      | d} n!t        | t              r| j                  |      } t        j                  |       S )Nr$   z.Could not find binary reader for input stream.r   )hasattrr	   r:   castIOr=   	TypeError
isinstancer<   encodeioBytesIO)r   rV   r   s      r   make_input_streamr`   R   sv     uf QUUU!;<>IHII}	E3	W%::er   c                   "   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Zedefd       Zedefd       Zedefd       ZdefdZy)Resultz3Holds the captured result of an invoked CLI script.Nrunner	CliRunnerstdout_bytesstderr_bytesreturn_value	exit_code	exceptionexc_infoc                 f    || _         || _        || _        || _        || _        || _        || _        y r   rc   re   rf   rg   rh   ri   rj   )r   rc   re   rf   rg   rh   ri   rj   s           r   r   zResult.__init__i   s:     (( )"" r   r   c                     | j                   S )z(The (standard) output as unicode string.)stdoutr1   s    r   r   zResult.output   s     {{r   c                     | j                   j                  | j                  j                  d      j	                  dd      S )z&The standard output as unicode string.replace

)re   decoderc   rV   rp   r1   s    r   rn   zResult.stdout   s9       ''(;(;YGOOD
 	
r   c                     | j                   t        d      | j                   j                  | j                  j                  d      j                  dd      S )z%The standard error as unicode string.zstderr not separately capturedrp   rq   rr   )rf   
ValueErrorrs   rc   rV   rp   r1   s    r   stderrzResult.stderr   sQ     $=>>  ''(;(;YGOOD
 	
r   c                     | j                   rt        | j                         nd}dt        |       j                   d| dS )Nokay< >)ri   r4   typer7   )r   exc_strs     r   r5   zResult.__repr__   s8    *...$t~~&f4:&&'q	33r   r   )r7   r8   r9   __doc__r>   r:   Optionalr=   r?   BaseExceptionTupleTyper   r   rT   r<   r   rn   rv   r5   rB   r   r   rb   rb   f   s    = !! ! jj'	!
 ee! ! ::m,! **GGAFF=)=-GH
!:    
 
 
 
 
 
4# 4r   rb   c                      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Z	d	d
defdZ
	 ddej                  ej                  eej                  e   f      dej                  eej                  e   f   f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       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Zej                  	 ddej                  ej                  edf      dej$                  e   fd       Zy)rd   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
    NrV   env
echo_stdin
mix_stderrr   c                 D    || _         |xs i | _        || _        || _        y r   )rV   r   r   r   )r   rV   r   r   r   s        r   r   zCliRunner.__init__   s$     47I2$$r   clir
   c                 "    |j                   xs 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)rI   )r   r   s     r   get_default_prog_namezCliRunner.get_default_prog_name   s    
 xx!6!r   	overridesc                 V    t        | j                        }|r|j                  |       |S )z8Returns the environment overrides for invoking a script.)dictr   update)r   r   r   s      r   make_envzCliRunner.make_env   s%     $((^IIi 	r   r   colorc              #   	  K   t        || j                        }d}t        j                  }t        j                  }t        j
                  }t        j                  }	dt        _        | j                  |      }t        j                         }
| j                  r0t        j                  t        j                  t        ||
            x}}t!        || j                  dd      xt        _        | j                  rd_        t!        |
| j                  dd      t        _        d}| j$                  rt        j                  t        _        n7t        j                         }t!        || j                  d	dd
      t        _        t'        |      ddt        j(                  t*           dt*        ffd       }t'        |      ddt        j(                  t*           dt*        ffd       }t'        |      dt,        dt*        fd       }|	 ddt        j(                  t        j.                  t        j0                        dt        j(                  t,           dt,        ffd}t2        j4                  }t2        j6                  }t2        j8                  }t:        j<                  }t>        j<                  }|t2        _        |t2        _        |t2        _        |t:        _        |t>        _        i }	 |jA                         D ]O  \  }}tB        jD                  jG                  |      ||<   |	 tB        jD                  |= =|tB        jD                  |<   Q |
|f |jA                         D ]-  \  }}|	 tB        jD                  |= |tB        jD                  |<   / |t        _        |t        _        |t        _        |t2        _        |t2        _        |t2        _        |t:        _        |t>        _        |	t        _        y# tH        $ r Y w xY w# tH        $ r Y w xY w# |jA                         D ]<  \  }}|"	 tB        jD                  |= # tH        $ r Y &w xY w|tB        jD                  |<   > |t        _        |t        _        |t        _        |t2        _        |t2        _        |t2        _        |t:        _        |t>        _        |	t        _        w xY w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.
        NP   z<stdin>r)encodingrI   rJ   r   z<stdout>wz<stderr>backslashreplace)r   rI   rJ   errorspromptr   c                    t         j                  j                  | xs d       j                         j	                  d      }t         j                  j                  | d       t         j                  j                          |S )N rq   rr   )sysrn   r    r)   rstripflush)r   val
text_inputs     r   visible_inputz*CliRunner.isolation.<locals>.visible_input  s_    JJV\r*%%'..v6CJJuBZ(JJJr   c                     t         j                  j                  | xs d d       t         j                  j                          j	                         j                  d      S )Nr   rr   rq   )r   rn   r    r   r)   r   )r   r   s    r   hidden_inputz)CliRunner.isolation.<locals>.hidden_input  sJ    JJ"~R01JJ&&(//77r   echoc                     t         j                  j                  d      }| rt         j                  j	                  |       t         j                  j                          |S )Nr   )r   stdinr$   rn   r    r   )r   chars     r   _getcharz%CliRunner.isolation.<locals>._getchar$  s>    99>>!$D

  &JJKr   rC   r   c                     | S | S r   rB   )rC   r   default_colors     r   should_strip_ansiz.CliRunner.isolation.<locals>.should_strip_ansi0  s     }(((9r   r   )NN)%r`   rV   r   r   rn   rv   r   FORCED_WIDTHr   r^   r_   r   r:   rY   r;   r   rG   _CHUNK_SIZEr   rE   r   r<   boolrZ   r=   r   visible_prompt_funchidden_prompt_funcr   r   r   r   itemsosenvironget	Exception)r   r   r   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   s                           @@r   	isolationzCliRunner.isolation   s    6 (t||<
II	ZZ
ZZ
%22"$
mmC zz|??'(vv

LlC( K* "5$,,YS"
 	
	J ?? &'J"(4<<js

 ??CJ**,K,)CJ 
Z	 	!**S/ 	S 	 
!	 
Z	 	8C 	8C 	8 
!	8
 
Z	 	4 	C 	 
!	  OS	JJqttAEE{+	;<::d;K		 #)"<"<!'!:!:"OO % 7 7(/(A(A%%2"$0!""3$5!	7!iik
U!zz~~c2=JJsO ',BJJsO *  --%mmo
U=JJsO ',BJJsO . $CJ#CJ!CI)@F&(>F%/FO&;E#(EG%&6J#- %  % 	 &mmo
U=JJsO$  ',BJJsO . $CJ#CJ!CI)@F&(>F%/FO&;E#(EG%&6J#s   K!S&:P !O22P S'P8A:S2	O?;P >O??P 	PSPSS+P=<S=	Q		SQ			A<SSargscatch_exceptionsextrac           	         d}| j                  |||      5 }	d}
d}d}t        |t              rt        j                  |      }	 |j                  d      }	  |j                  d|xs d|d|}
t        j&                  j-                          |	d   j/                         }| j0                  rd}n|	d   j/                         }	 ddd       t3        | 
|	      S # t        $ r | j                  |      }Y w xY w# t        $ r}t        j                         }t        j                  t        j                  t        j                  t         t        j"                  f      |j$                        }|d}|dk7  r|}t        |t               sIt        j&                  j)                  t        |             t        j&                  j)                  d       d}|}Y d}~md}~wt*        $ r&}|s |}d}t        j                         }Y d}~d}~ww xY w# t        j&                  j-                          |	d   j/                         }| j0                  rd}w |	d   j/                         }w xY w# 1 sw Y   xY w)
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   r   r   r   	prog_namerB   )r   r   rr   r   rl   )r   r\   r<   shlexsplitpopKeyErrorr   main
SystemExitr   rj   r:   rY   r   Unionr?   r=   codern   r    r   r   getvaluer   rb   )r   r   r   r   r   r   r   r   rj   
outstreamsrg   ri   rh   r   ee_codern   rv   s                     r   invokezCliRunner.invokea  s   X ^^%S^>*L37II$${{4(<!IIk2	6'sxxVTZR9VPUV2 

  "#A//1??!F']335FY ?\ %
 	
I  < 66s;	<
  #<<>

1773:+> ?H>FQ; !I!&#.JJ$$S[1JJ$$T*F"	 *'		<<>* 

  "#A//1??!F']335FY ?>st   ,I)CC=/AI)C:7I)9C::I)=	HCGHH(H	H	HHAI&&I))I3temp_dirzos.PathLike[str]c              #     K   t        j                         }t        j                  |      }t        j                  |       	 | t        j                  |       |	 t        j                  |       yy# t        $ r Y yw xY w# t        j                  |       |&	 t        j                  |       w # t        $ r Y w w xY ww xY 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dts       r   isolated_filesystemzCliRunner.isolated_filesystem  s       iik(+
		HHHSMMM"%     HHSMMM"%   sf   A CB CA6 4C6	B?CBCCB43C4	C =C?C  CC)zutf-8NFTr   )NNF)NNNTF)r7   r8   r9   r~   r<   r:   r   Mappingr   r   r   r   
contextlibcontextmanagerr   r>   rZ   r=   rA   r   r^   r_   r   Sequencerb   r   r   rB   r   r   rd   rd      s   ( ;? 
%
% ZZ		#qzz#"678
% 	
%
 
% 

%" "3 " HLAIIc1::c?.B$CD	
3

3'	(  ?C;?	O7zz!''#uadd155k"9:;O7 ZZ		#qzz#"678O7 	O7
 
AGGBJJ

2::(>>?	@O7 O7h ;?>B;?!%c
c
 jjajjo!567c
 zz!''#uadd155k"9:;	c

 ZZ		#qzz#"678c
 c
 c
 c
 
c
J GK

17730B+B#CD	
C r   rd   )$r   r^   r   r   r   r   r   typingr:   typesr   r   r   r   r   r   r	   TYPE_CHECKINGcorer
   r   r   r   rA   rE   TextIOWrapperrG   r   r<   r>   rZ   r=   r;   r`   rb   rd   rB   r   r   <module>r      s     	 	   
        (??!! !D 

<0 QZZ5E  "** "::aggc5!$$quu+567BEZZ(74 74tC Cr   