
    e!h7H                       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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      Zej8                  dd       Z G d dej<                        Z G d d      Z  G d dejB                        Z"	 	 	 	 	 	 ddZ# G d d      Z$ G d d      Z%y)    )annotationsN)TracebackType   )_compat)
formatting)termui)utils)_find_binary_reader)ReadableBuffer)Commandc                  Z    e Zd ZddZddZddZdddZdddZdddZddZ	ddZ
dd	Zy
)EchoingStdinc                .    || _         || _        d| _        y )NF)_input_output_paused)selfinputoutputs      U/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/click/testing.py__init__zEchoingStdin.__init__   s        c                .    t        | j                  |      S N)getattrr   r   xs     r   __getattr__zEchoingStdin.__getattr__    s    t{{A&&r   c                T    | j                   s| j                  j                  |       |S r   )r   r   write)r   rvs     r   _echozEchoingStdin._echo#   s     ||LLr"	r   c                V    | j                  | j                  j                  |            S r   )r"   r   readr   ns     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.readlines2   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>6   s     7aDJJqM7s   )iterr   r   s   `r   __iter__zEchoingStdin.__iter__5   s    74;;777r   c                ,    t        | j                        S r   )reprr   r2   s    r   __repr__zEchoingStdin.__repr__8   s    DKK  r   N)r   
t.BinaryIOr   r7   returnNone)r   strr8   t.Any)r!   bytesr8   r<   ))r&   intr8   r<   )r8   zlist[bytes])r8   zcabc.Iterator[bytes]r8   r:   )__name__
__module____qualname__r   r   r"   r$   r(   r*   r,   r3   r6    r   r   r   r      s0    
'/03@8!r   r   c              #  >   K   | d  y d| _         d  d| _         y w)NTF)r   )streams    r   _pause_echorF   <   s     ~s   c                  <     e Zd ZdZd fdZd fdZd fdZ xZS )BytesIOCopyzdPatch ``io.BytesIO`` to let the written stream be copied to another.

    .. versionadded:: 8.2
    c                0    t         |           || _        y r   )superr   copy_to)r   rK   	__class__s     r   r   zBytesIOCopy.__init__L   s    r   c                V    t         |           | j                  j                          y r   )rJ   flushrK   )r   rL   s    r   rN   zBytesIOCopy.flushP   s    r   c                X    | j                   j                  |       t        |   |      S r   )rK   r    rJ   )r   brL   s     r   r    zBytesIOCopy.writeT   s$    1w}Qr   )rK   z
io.BytesIOr8   r9   r8   r9   )rP   r   r8   r>   )r@   rA   rB   __doc__r   rN   r    __classcell__rL   s   @r   rH   rH   F   s    
   r   rH   c                      e Zd ZdZddZy)StreamMixerzMixes `<stdout>` and `<stderr>` streams.

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

    .. versionadded:: 8.2
    c                    t        j                         | _        t        | j                        | _        t        | j                        | _        y )N)rK   )ioBytesIOr   rH   stdoutstderrr2   s    r   r   zStreamMixer.__init__a   s/    "$**,"-dkk"B"-dkk"Br   NrQ   )r@   rA   rB   rR   r   rC   r   r   rV   rV   Y   s    Cr   rV   c                  X     e Zd Z	 	 	 	 	 	 	 	 	 	 d fdZedd       Zedd       Z xZS )_NamedTextIOWrapperc                B    t        |   |fi | || _        || _        y r   )rJ   r   _name_mode)r   buffernamemodekwargsrL   s        r   r   z_NamedTextIOWrapper.__init__h   s%     	*6*

r   c                    | j                   S r   )r_   r2   s    r   rb   z_NamedTextIOWrapper.nameo       zzr   c                    | j                   S r   )r`   r2   s    r   rc   z_NamedTextIOWrapper.modes   rf   r   )
ra   r7   rb   r:   rc   r:   rd   r;   r8   r9   r?   )r@   rA   rB   r   propertyrb   rc   rS   rT   s   @r   r]   r]   g   sX     (+36BG	    r   r]   c                    t        | d      r.t        t        j                  d|             }||S t	        d      | d} n!t        | t              r| j                  |      } t        j                  |       S )Nr$   zt.IO[t.Any]z.Could not find binary reader for input stream.r   )
hasattrr
   tcast	TypeError
isinstancer:   encoderX   rY   )r   charsetr!   s      r   make_input_streamrq   x   sj     uf u!=>>IHII}	E3	W%::er   c                  x    e Zd ZdZ	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	dZed
d       Zed
d       Zed
d       Zd
dZ	y)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``.
    Nc	                t    || _         || _        || _        || _        || _        || _        || _        || _        y r   runnerstdout_bytesstderr_bytesoutput_bytesreturn_value	exit_code	exceptionexc_info)	r   rv   rw   rx   ry   rz   r{   r|   r}   s	            r   r   zResult.__init__   s@     (((("" r   c                    | j                   j                  | j                  j                  d      j	                  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

)ry   decoderv   rp   r   r2   s    r   r   zResult.output   s9       ''(;(;YGOOD
 	
r   c                    | j                   j                  | j                  j                  d      j	                  dd      S )z&The standard output as unicode string.r   r   r   )rw   r   rv   rp   r   r2   s    r   rZ   zResult.stdout   s9       ''(;(;YGOOD
 	
r   c                    | j                   j                  | j                  j                  d      j	                  dd      S )zThe standard error as unicode string.

        .. versionchanged:: 8.2
            No longer raise an exception, always returns the `<stderr>` string.
        r   r   r   )rx   r   rv   rp   r   r2   s    r   r[   zResult.stderr   s9       ''(;(;YGOOD
 	
r   c                    | j                   rt        | j                         nd}dt        |       j                   d| dS )Nokay< >)r|   r5   typer@   )r   exc_strs     r   r6   zResult.__repr__   s8    *...$t~~&f4:&&'q	33r   r   )rv   	CliRunnerrw   r<   rx   r<   ry   r<   rz   r;   r{   r>   r|   zBaseException | Noner}   z?tuple[type[BaseException], BaseException, TracebackType] | Noner?   )
r@   rA   rB   rR   r   rh   r   rZ   r[   r6   rC   r   r   rs   rs      s    > !! ! 	!
 ! ! ! (!!* 	
 	
 
 
 
 
4r   rs   c                      e Zd ZdZ	 	 	 	 d		 	 	 	 	 	 	 	 	 d
dZddZ	 d	 	 	 ddZej                  	 	 	 d	 	 	 	 	 	 	 dd       Z		 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ
ej                  	 d	 	 	 dd       Zy)r   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.
    Nc                D    || _         |xs i | _        || _        || _        y r   )rp   env
echo_stdincatch_exceptions)r   rp   r   r   r   s        r   r   zCliRunner.__init__   s%     25)$ 0r   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)rb   )r   clis     r   get_default_prog_namezCliRunner.get_default_prog_name   s    
 xx!6!r   c                V    t        | j                        }|r|j                  |       |S )z8Returns the environment overrides for invoking a script.)dictr   update)r   	overridesr!   s      r   make_envzCliRunner.make_env  s%     $((^IIi 	r   c              #  b  K   t        || j                        }d}t        j                  }t        j                  }t        j
                  }t        j                  }	dt        _        | j                  |      }t               }
| j                  r:t        j                  t        j                  t        ||
j                              x}}t        || j                  dd      xt        _        | j                  rd_        t        |
j                  | j                  dd      t        _        t        |
j
                  | j                  d	dd
      t        _        t#        |      ddfd       }t#        |      ddfd       }t#        |      dd       }|	 d	 	 	 	 	 dfd}t$        j&                  }t$        j(                  }t$        j*                  }t,        j.                  }t0        j.                  }|t$        _        |t$        _        |t$        _        |t,        _        |t0        _        i }	 |j3                         D ]O  \  }}t4        j6                  j9                  |      ||<   |	 t4        j6                  |= =|t4        j6                  |<   Q |
j                  |
j
                  |
j<                  f |j3                         D ]-  \  }}|	 t4        j6                  |= |t4        j6                  |<   / |t        _        |t        _        |t        _        |t$        _        |t$        _        |t$        _        |t,        _        |t0        _        |	t        _        y# t:        $ r Y 'w xY w# t:        $ r Y w xY w# |j3                         D ]<  \  }}|"	 t4        j6                  |= # t:        $ r Y &w xY w|t4        j6                  |<   > |t        _        |t        _        |t        _        |t$        _        |t$        _        |t$        _        |t,        _        |t0        _        |	t        _        w xY w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)encodingrb   rc   r   z<stdout>wz<stderr>backslashreplace)r   rb   rc   errorsc                   t         j                  j                  | xs d       j                         j	                  d      }t         j                  j                  | d       t         j                  j                          |S )N r   r   )sysrZ   r    r*   rstriprN   )promptval
text_inputs     r   visible_inputz*CliRunner.isolation.<locals>.visible_inputU  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   r   r   )r   rZ   r    rN   r*   r   )r   r   s    r   hidden_inputz)CliRunner.isolation.<locals>.hidden_input]  sJ    JJ"~R01JJ&&(//77r   c                    t         j                  j                  d      }| rt         j                  j	                  |       t         j                  j                          |S )Nr   )r   stdinr$   rZ   r    rN   )echochars     r   _getcharz%CliRunner.isolation.<locals>._getcharc  s>    99>>!$D

  &JJKr   c                    | S | S r   rC   )rE   colordefault_colors     r   should_strip_ansiz.CliRunner.isolation.<locals>.should_strip_ansio  s     }(((9r   r   )r   z
str | Noner8   r:   )r   boolr8   r:   )NN)rE   zt.IO[t.Any] | Noner   bool | Noner8   r   )rq   rp   r   r   rZ   r[   r   FORCED_WIDTHr   rV   r   rk   rl   BinaryIOr   r]   _CHUNK_SIZErF   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   s                          @@r   	isolationzCliRunner.isolation  s    D (t||<
II	ZZ
ZZ
%22"$
mmC "}??'(vv

Ll6I6IJ( K* "5$,,YS"
 	
	J ?? &'J"($,,Zc

 )\\%

 
Z	 	 
!	 
Z	 	8 
!	8
 
Z	 	 
!	  EI	&	6A		 #)"<"<!'!:!:"OO % 7 7(/(A(A%%2"$0!""3$5!	7!iik ,
U!zz~~c2=JJsO ',BJJsO,  &&(;(;\=P=PQQ%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   H)P/.:M8 )M:;M8 5P/M)A:P/	M&"M8 %M&&M8 )	M52P/4M55P/8P,N$#P,$	N0	-P,/N0	0A<P,,P/c           
        d}|| j                   }| j                  |||      5 }	d}
d}d}t        |t              rt	        j
                  |      }	 |j                  d      }	  |j                  d|xs d|d|}
t        j"                  j)                          |	d   j+                         }|	d	   j+                         }|	d
   j+                         }	 ddd       t-        | 
|      S # t        $ r | j                  |      }Y w xY w# t        $ r}t        j                         }t        j                  d|j                        }|d}|dk7  r|}t        |t               sIt        j"                  j%                  t        |             t        j"                  j%                  d       d	}|}Y d}~<d}~wt&        $ r&}|s |}d	}t        j                         }Y d}~id}~ww xY w# t        j"                  j)                          |	d   j+                         }|	d	   j+                         }|	d
   j+                         }w xY w# 1 sw Y   wx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``. 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_namerC   )argsr   zint | t.Any | Noner   r      ru   )r   r   rn   r:   shlexsplitpopKeyErrorr   main
SystemExitr   r}   rk   rl   coder>   rZ   r    r   rN   getvaluers   )r   r   r   r   r   r   r   extrar}   
outstreamsrz   r|   r{   r   ee_coderZ   r[   r   s                      r   invokezCliRunner.invoke  s'   j ##44^^%S^> *	2*L.2II$${{4(<!IIk2	2'sxxVTZR9VPUV2 

  "#A//1#A//1#A//1U*	2X %	
 		
E  < 66s;	<
  #<<> 4aff=>FQ; !I!&#.JJ$$S[1JJ$$T*F"	 *'		<<>* 

  "#A//1#A//1#A//1U*	2 *	2st   ,I
C0$D=AI
0D
I
DI
	G+BF93G.9G+G& G.&G++G..AII

I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   temp_dir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-8NFT)
rp   r:   r   $cabc.Mapping[str, str | None] | Noner   r   r   r   r8   r9   )r   r   r8   r:   r   )r   r   r8   zcabc.Mapping[str, str | None])NNF)r    str | bytes | t.IO[t.Any] | Noner   r   r   r   r8   z8cabc.Iterator[tuple[io.BytesIO, io.BytesIO, io.BytesIO]])NNNNF)r   r   r   zstr | cabc.Sequence[str] | Noner   r   r   r   r   r   r   r   r   r;   r8   rs   )r   zstr | os.PathLike[str] | Noner8   zcabc.Iterator[str])r@   rA   rB   rR   r   r   r   
contextlibcontextmanagerr   r   r   rC   r   r   r   r      sY   . 48 !%
1
1 2
1 	
1
 
1 

1" AE=	&  3748	Q7/Q7 2Q7 	Q7
 
BQ7 Q7l 152648(,n
n
 .n
 0	n

 2n
 &n
 n
 n
 
n
` 8<5	 r   r   )rE   zEchoingStdin | Noner8   zcabc.Iterator[None])r   r   rp   r:   r8   r7   )&
__future__r   collections.abcabccabcr   rX   r   r   r   r   r   typingrk   typesr   r   r   r   r   r	   r
   TYPE_CHECKING	_typeshedr   corer   r   r   rF   rY   rH   rV   TextIOWrapperr]   rq   rs   r   rC   r   r   <module>r      s    "   	 	   
        (??(! !D   "**  &C C"** "+69(K4 K4\S Sr   