o
    jX                     @  s8  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 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   S/var/www/html/fyndo/pharma/fyndo/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>3   s    z*EchoingStdin.readlines.<locals>.<listcomp>)r   	readlinesr0   r   r0   r   r2   2      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>6   s    z(EchoingStdin.__iter__.<locals>.<genexpr>)iterr   r0   r   r0   r   __iter__5   r3   zEchoingStdin.__iter__c                 C  s
   t | jS r   )reprr   r0   r   r   r   __repr__8   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   

rB   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   rD   )r   rD   	__class__r   r   r   L   s   

zBytesIOCopy.__init__c                   s   t    | j  d S r   )rF   flushrD   r0   rG   r   r   rI   P   s   
zBytesIOCopy.flushbr
   r%   c                   s   | j | t |S r   )rD   r!   rF   )r   rJ   rG   r   r   r!   T   s   zBytesIOCopy.write)rD   rE   r   r   r   r   )rJ   r
   r   r%   )r;   r<   r=   __doc__r   rI   r!   __classcell__r   r   rG   r   rC   F   s
    rC   c                   @  s   e Zd ZdZdddZdS )StreamMixerzMixes `<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)rD   )ioBytesIOr   rC   stdoutstderrr0   r   r   r   r   a   s   
zStreamMixer.__init__NrK   )r;   r<   r=   rL   r   r   r   r   r   rN   Y   s    rN   c                      s\   e Zd ZdZddd fddZdddZd fddZedddZedddZ	  Z
S )_NamedTextIOWrappera  A :class:`~io.TextIOWrapper` with custom ``name`` and ``mode``
    that does not close its underlying buffer.

    An optional ``original_fd`` preserves the file descriptor of the
    stream being replaced, so that C-level consumers that call
    :meth:`fileno` (``faulthandler``, ``subprocess``, ...) still work.
    Inspired by pytest's ``capsys``/``capfd`` split: see :doc:`/testing`
    for details.

    .. versionchanged:: 8.3.3
        Added ``original_fd`` parameter and :meth:`fileno` override.
    r#   )original_fdbufferr   namer   moderT   r%   kwargsr   r   r   c                  s*   t  j|fi | || _|| _|| _d S r   )rF   r   _name_mode_original_fd)r   rU   rV   rW   rT   rX   rG   r   r   r   u   s   	
z_NamedTextIOWrapper.__init__c                 C  s   dS )zThe buffer this object contains belongs to some other object,
        so prevent the default ``__del__`` implementation from closing
        that buffer.

        .. versionadded:: 8.3.2
        Nr   r0   r   r   r   close   s    z_NamedTextIOWrapper.closec                   s   | j dkr| j S t  S )ax  Return the file descriptor of the original stream, if one was
        provided at construction time.

        This allows C-level consumers (``faulthandler``, ``subprocess``,
        signal handlers, ...) to obtain a valid fd without crashing, even
        though the Python-level writes are redirected to an in-memory
        buffer.

        .. versionadded:: 8.3.3
        r   )r[   rF   filenor0   rG   r   r   r]      s   

z_NamedTextIOWrapper.filenoc                 C     | j S r   )rY   r0   r   r   r   rV         z_NamedTextIOWrapper.namec                 C  r^   r   )rZ   r0   r   r   r   rW      r_   z_NamedTextIOWrapper.mode)rU   r   rV   r   rW   r   rT   r%   rX   r   r   r   rK   )r   r%   r:   )r;   r<   r=   rL   r   r\   r]   propertyrV   rW   rM   r   r   rG   r   rS   g   s    
rS   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'   t.IO[t.Any]z.Could not find binary reader for input stream.    )
hasattrr	   tcast	TypeError
isinstancer   encoderO   rP   )r   rb   r   r   r   r   make_input_stream   s   



rk   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   rm   ro   rp   rq   rr   rs   rt   rv   )	r   rm   ro   rp   rq   rr   rs   rt   rv   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

)rq   decoderm   rb   rz   r0   r   r   r   r      s   zResult.outputc                 C  ry   )z&The standard output as unicode string.rz   r{   r|   )ro   r}   rm   rb   rz   r0   r   r   r   rQ      s   zResult.stdoutc                 C  ry   )zThe standard error as unicode string.

        .. versionchanged:: 8.2
            No longer raise an exception, always returns the `<stderr>` string.
        rz   r{   r|   )rp   r}   rm   rb   rz   r0   r   r   r   rR      s   zResult.stderrc                 C  s,   | j rt| j nd}dt| j d| dS )Nokay< >)rt   r8   typer;   )r   exc_strr   r   r   r9      s   zResult.__repr__r   )rm   rn   ro   r    rp   r    rq   r    rr   r   rs   r%   rt   ru   rv   rw   r:   )
r;   r<   r=   rL   r   r`   r   rQ   rR   r9   r   r   r   r   rl      s    
rl   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 )6rn   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-8NFTrb   r   env$cabc.Mapping[str, str | None] | None
echo_stdinboolcatch_exceptionsr   r   c                 C  s    || _ |pi | _|| _|| _d S r   )rb   r   r   r   )r   rb   r   r   r   r   r   r   r     s   

zCliRunner.__init__clir   c                 C  s
   |j p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)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   ra   color8cabc.Iterator[tuple[io.BytesIO, io.BytesIO, io.BytesIO]]c                 #  s   t || j}d}tj}tj}tj}tj}	dt_| |}t	 }
d/dd}||}||}| j
r=ttjt||
j }}t|| jd	d
d t_| j
rOd_t|
j| jdd|dt_t|
j| jddd|dt_t|d0d1fdd}t|d0d1fdd}t|d2dd}| 	d3d4 fd!d"}tj}tj}tj}tj}tj}tjj|t_|t_|t_|t_|t_	#		d5d6fd-d.}|tj_i }z|| D ]%\}}tj|||< |du rztj|= W q t y   Y qw |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_tj_dS | D ]"\}}|du rdztj|= W qH t yc   Y qHw |tj|< qH|t_|t_|t_|t_|t_|t_|t_|t_|	t_tj_w )7a\  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   r>   rc   r   r%   c              	   S  s&   z|   W S  ttjfy   Y dS w )Nr#   )r]   AttributeErrorrO   UnsupportedOperationrA   r   r   r   _safe_filenok  s
   
z)CliRunner.isolation.<locals>._safe_filenoz<stdin>r)encodingrV   rW   r   z<stdout>w)r   rV   rW   rT   z<stderr>backslashreplace)r   rV   rW   errorsrT   prompt
str | Noner   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 r{   r|   )sysrQ   r!   nextrstripStopIterationEOFErrorrI   )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   r|   r{   )r   rQ   r!   rI   r   r   r   r   )r   r   r   r   r   hidden_input  s   
z)CliRunner.isolation.<locals>.hidden_inputechor   c                 S  s*   t jd}| rt j| t j  |S )Nr   )r   stdinr'   rQ   r!   rI   )r   charr   r   r   _getchar  s
   
z%CliRunner.isolation.<locals>._getchar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_ansitabr   pdb.Pdbcompletekeyr   t.IO[str] | NonerQ   rX   r   r   c                   s8   |du rt j}|du rt j} | f|||d| dS )ab  Default ``pdb.Pdb`` to real terminal streams during
            ``CliRunner`` isolation.

            Without this patch, ``pdb.Pdb.__init__`` inherits from
            ``cmd.Cmd`` which falls back to ``sys.stdin``/``sys.stdout``
            when no explicit streams are provided. During isolation
            those are ``BytesIO``-backed wrappers, so the debugger
            reads from an empty buffer and writes to captured output,
            making interactive debugging impossible.

            By defaulting to ``sys.__stdin__``/``sys.__stdout__`` (the
            original terminal streams Python preserves regardless of
            redirection), debuggers can interact with the user while
            ``click.echo`` output is still captured normally.

            This covers ``pdb.set_trace()``, ``breakpoint()``,
            ``pdb.post_mortem()``, and debuggers that subclass
            ``pdb.Pdb`` (ipdb, pdbpp). Explicit ``stdin``/``stdout``
            arguments are honored and not overridden. Debuggers that
            do not subclass ``pdb.Pdb`` (pudb, debugpy) are not
            covered.
            N)r   r   rQ   )r   	__stdin__
__stdout__)r   r   r   rQ   rX   )old_pdb_initr   r   _patched_pdb_init  s   
z.CliRunner.isolation.<locals>._patched_pdb_init)r>   rc   r   r%   r   )r   r   r   r   )r   r   r   r   )NN)r>   r   r   r   r   r   )r   NN)r   r   r   r   r   r   rQ   r   rX   r   r   r   )"rk   rb   r   r   rQ   rR   r   FORCED_WIDTHr   rN   r   rf   rg   BinaryIOr   rS   _CHUNK_SIZErB   r   visible_prompt_funchidden_prompt_funcr   r   r   r   pdbPdbr   itemsosenvironget	Exceptionr   )r   r   r   r   bytes_input
echo_input	old_stdin
old_stdout
old_stderrold_forced_widthstream_mixerr   old_stdout_fdold_stderr_fdr   r   r   r   old_visible_prompt_funcold_hidden_prompt_funcold__getchar_funcold_should_strip_ansiold__compat_should_strip_ansir   old_envkeyvaluer   )r   r   r   r   	isolation7  s   "


	
	%


zCliRunner.isolationargsstr | cabc.Sequence[str] | Noner   extrar   rl   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 zz|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 sw   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 | Noner|   r      rx   )r   r   ri   r   shlexsplitpopKeyErrorr   main
SystemExitr   rv   rf   rg   coder%   rQ   r!   r   rI   rR   getvaluerl   )r   r   r   r   r   r   r   r   rv   
outstreamsrr   rt   rs   r   r   e_coderQ   rR   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)
rb   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   )NNF)r   ra   r   r   r   r   r   r   )NNNNF)r   r   r   r   r   ra   r   r   r   r   r   r   r   r   r   rl   )r   r   r   r   )r;   r<   r=   rL   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   rn     s2    
	 Yqrn   )r>   r?   r   r@   )r   ra   rb   r   r   r   )&
__future__r   collections.abcabccabcr   rO   r   r   r   r   r   typingrf   typesr   r   r   r   r   r   r	   TYPE_CHECKING	_typeshedr
   corer   r   r   rB   rP   rC   rN   TextIOWrapperrS   rk   rl   rn   r   r   r   r   <module>   s8    "	
<N