
    s<g>                        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 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   )
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      K/home/kevstigneev/proxy/myenv/lib/python3.12/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,   s+    '+{{'<'<'>?!

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>0   s     7aDJJqM7s   )iterr   r   s   `r   __iter__zEchoingStdin.__iter__/   s    74;;777r   c                 ,    t        | j                        S r   )reprr   r0   s    r   __repr__zEchoingStdin.__repr__2   s    DKK  r   ))__name__
__module____qualname__tBinaryIOr   strAnyr   bytesr    intr#   r&   r(   Listr*   Iteratorr1   r4    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   )rB   s    r   _pause_echorD   6   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   rG   rH   rI   rJ   	__class__s        r   r   z_NamedTextIOWrapper.__init__A   s%     	*6*

r   c                     | j                   S r   )rM   r0   s    r   rH   z_NamedTextIOWrapper.nameH       zzr   c                     | j                   S r   )rN   r0   s    r   rI   z_NamedTextIOWrapper.modeL   rQ   r   )r6   r7   r8   r9   r:   r;   r<   r   propertyrH   rI   __classcell__)rO   s   @r   rF   rF   @   sn    jj(+36BC%%	 c   c  r   rF   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   r9   castIOr<   	TypeError
isinstancer;   encodeioBytesIO)r   rU   r   s      r   make_input_streamr_   Q   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   rb   rd   re   rf   rg   rh   ri   )r   rb   rd   re   rf   rg   rh   ri   s           r   r   zResult.__init__h   s:     (( )"" r   r   c                     | j                   S )z(The (standard) output as unicode string.)stdoutr0   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

)rd   decoderb   rU   ro   r0   s    r   rm   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 capturedro   rp   rq   )re   
ValueErrorrr   rb   rU   ro   r0   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< >)rh   r3   typer6   )r   exc_strs     r   r4   zResult.__repr__   s8    *...$t~~&f4:&&'q	33r   r   )r6   r7   r8   __doc__r=   r9   Optionalr<   r>   BaseExceptionTupleTyper   r   rS   r;   r   rm   ru   r4   rA   r   r   ra   ra   e   s    = !! ! jj'	!
 ee! ! ::m,! **GGAFF=)=-GH
!:    
 
 
 
 
 
4# 4r   ra   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)rc   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
    NrU   env
echo_stdin
mix_stderrr   c                 D    || _         |xs i | _        || _        || _        y r   )rU   r   r   r   )r   rU   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)rH   )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              #   0	  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<                  }|t2        _        |t2        _        |t2        _        |t:        _        i }	 |j?                         D ]O  \  }}t@        jB                  jE                  |      ||<   |	 t@        jB                  |= =|t@        jB                  |<   Q |
|f |j?                         D ]-  \  }}|	 t@        jB                  |= |t@        jB                  |<   / |t        _        |t        _        |t        _        |t2        _        |t2        _        |t2        _        |t:        _        |	t        _        y# tF        $ r Y w xY w# tF        $ r Y w xY w# |j?                         D ]<  \  }}|"	 t@        jB                  |= # tF        $ r Y &w xY w|t@        jB                  |<   > |t        _        |t        _        |t        _        |t2        _        |t2        _        |t2        _        |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)encodingrH   rI   r   z<stdout>wz<stderr>backslashreplace)r   rH   rI   errorspromptr   c                    t         j                  j                  | xs d       j                         j	                  d      }t         j                  j                  | d       t         j                  j                          |S )N rp   rq   )sysrm   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   rq   rp   )r   rm   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#   rm   r   r   )r   chars     r   _getcharz%CliRunner.isolation.<locals>._getchar#  s>    99>>!$D

  &JJKr   rB   r   c                     | S | S r   rA   )rB   r   default_colors     r   should_strip_ansiz.CliRunner.isolation.<locals>.should_strip_ansi/  s     }(((9r   r   )NN)$r_   rU   r   r   rm   ru   r   FORCED_WIDTHr   r]   r^   r   r9   rX   r:   r   rF   _CHUNK_SIZEr   rD   r~   r;   boolrY   r<   r   visible_prompt_funchidden_prompt_funcr   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_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%2"$0!""3	7!iik ,
U!zz~~c2=JJsO ',BJJsO,  --%mmo ,
U=JJsO ',BJJsO, $CJ#CJ!CI)@F&(>F%/FO&;E#&6J#+ %  % 	 &mmo ,
U=JJsO$  ',BJJsO, $CJ#CJ!CI)@F&(>F%/FO&;E#&6J#s   KR:O* OO* 3ROA/R	OO* OO* 	O'$R&O''R*RPR	P"	R!P"	"A1RR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_namerA   )r   r   rq   r   rk   )r   r[   r;   shlexsplitpopKeyErrorr   main
SystemExitr   ri   r9   rX   r~   Unionr>   r<   coderm   r   r   r   getvaluer   ra   )r   r   r   r   r   r   r   r   ri   
outstreamsrf   rh   rg   r   ee_coderm   ru   s                     r   invokezCliRunner.invoke]  s   X ^^%S^> ,	6*L37II$${{4(<!IIk2	6'sxxVTZR9VPUV2 

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

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

  "#A//1??!F']335FY,	6 ,	6st   ,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)r6   r7   r8   r}   r;   r9   r~   Mappingr   r   r   r   
contextlibcontextmanagerr   r=   rY   r<   r@   r   r]   r^   r   Sequencera   r   r   rA   r   r   rc   rc      s   ( ;? 
%
% ZZ		#qzz#"678
% 	
%
 
% 

%" "3 " HLAIIc1::c?.B$CD	
3

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

2::(>>?	@L7 L7b ;?>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   rc   )$r   r]   r   r   r   r   r   typingr9   typesr   r   r   r   r   _compatr   TYPE_CHECKINGcorer	   r   r   r~   r@   rD   TextIOWrapperrF   r   r;   r=   rY   r<   r:   r_   ra   rc   rA   r   r   <module>r      s     	 	   
       (??!! !D 

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