o
    1)h7H                     @  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   LC:\Users\User\Downloads\Proyecto_IoT\venv\Lib\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   

rA   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   rC   )r   rC   	__class__r   r   r   L   s   

zBytesIOCopy.__init__c                   s   t    | j  d S r   )rE   flushrC   r0   rF   r   r   rH   P   s   
zBytesIOCopy.flushbr
   r%   c                   s   | j | t |S r   )rC   r!   rE   )r   rI   rF   r   r   r!   T   s   zBytesIOCopy.write)rC   rD   r   r   r   r   )rI   r
   r   r%   )r;   r<   r=   __doc__r   rH   r!   __classcell__r   r   rF   r   rB   F   s
    rB   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)rC   )ioBytesIOr   rB   stdoutstderrr0   r   r   r   r   a   s   
zStreamMixer.__init__NrJ   )r;   r<   r=   rK   r   r   r   r   r   rM   Y   s    rM   c                      s:   e Zd Zd fd
dZedddZedddZ  ZS )_NamedTextIOWrapperbufferr   namer   modekwargsr   r   r   c                   s$   t  j|fi | || _|| _d S r   )rE   r   _name_mode)r   rS   rT   rU   rV   rF   r   r   r   h   s   
z_NamedTextIOWrapper.__init__c                 C     | j S r   )rW   r0   r   r   r   rT   o      z_NamedTextIOWrapper.namec                 C  rY   r   )rX   r0   r   r   r   rU   s   rZ   z_NamedTextIOWrapper.mode)
rS   r   rT   r   rU   r   rV   r   r   r   r:   )r;   r<   r=   r   propertyrT   rU   rL   r   r   rF   r   rR   g   s    rR   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'   zt.IO[t.Any]z.Could not find binary reader for input stream.    )
hasattrr	   tcast	TypeError
isinstancer   encoderN   rO   )r   r]   r   r   r   r   make_input_streamx   s   



re   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   rg   ri   rj   rk   rl   rm   rn   rp   )	r   rg   ri   rj   rk   rl   rm   rn   rp   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

)rk   decoderg   r]   rt   r0   r   r   r   r      s   zResult.outputc                 C  rs   )z&The standard output as unicode string.rt   ru   rv   )ri   rw   rg   r]   rt   r0   r   r   r   rP      s   zResult.stdoutc                 C  rs   )zThe standard error as unicode string.

        .. versionchanged:: 8.2
            No longer raise an exception, always returns the `<stderr>` string.
        rt   ru   rv   )rj   rw   rg   r]   rt   r0   r   r   r   rQ      s   zResult.stderrc                 C  s,   | j rt| j nd}dt| j d| dS )Nokay< >)rn   r8   typer;   )r   Zexc_strr   r   r   r9      s   zResult.__repr__r   )rg   rh   ri   r    rj   r    rk   r    rl   r   rm   r%   rn   ro   rp   rq   r:   )
r;   r<   r=   rK   r   r[   r   rP   rQ   r9   r   r   r   r   rf      s    
rf   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 )6rh   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-8NFTr]   r   env$cabc.Mapping[str, str | None] | None
echo_stdinboolcatch_exceptionsr   r   c                 C  s    || _ |pi | _|| _|| _d S r   )r]   r~   r   r   )r   r]   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)rT   )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   r\   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	 }
| j
r0ttjt||
j }}t|| jddd t_| j
rBd_t|
j| jdddt_t|
j| jd	dd
dt_t|ddfdd}t|ddfdd}t|d dd}| 	d!d" fdd}tj}tj}tj}tj}tj}|t_|t_|t_|t_|t_i }zs| 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 rztj|= W q ty   Y qw |tj|< q|t_|t_|t_|t_|t_|t_|t_|t_|	t_dS | D ]"\}}|du r9ztj|= W q ty8   Y qw |tj|< q|t_|t_|t_|t_|t_|t_|t_|t_|	t_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)encodingrT   rU   r   z<stdout>wz<stderr>backslashreplace)r   rT   rU   errorsprompt
str | Noner   r   c                   s>   t j| pd   d}t j| d t j  |S )N ru   rv   )sysrP   r!   r+   rstriprH   )r   val
text_inputr   r   visible_inputU  s
   
z*CliRunner.isolation.<locals>.visible_inputc                   s.   t j| pd d t j    dS )Nr   rv   ru   )r   rP   r!   rH   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'   rP   r!   rH   )r   charr   r   r   _getcharc  s
   
z%CliRunner.isolation.<locals>._getcharr>   t.IO[t.Any] | Noner   bool | Nonec                   s   |d u r  S | S r   r   )r>   r   )default_colorr   r   should_strip_ansio  s   z.CliRunner.isolation.<locals>.should_strip_ansir   )r   r   r   r   )r   r   r   r   )NN)r>   r   r   r   r   r   )re   r]   r   r   rP   rQ   r   ZFORCED_WIDTHr   rM   r   r`   ra   BinaryIOr   rR   _CHUNK_SIZErA   r   Zvisible_prompt_funcZhidden_prompt_funcr   r   r   r   itemsosenvironget	Exceptionr   )r   r   r~   r   Zbytes_inputZ
echo_inputZ	old_stdinZ
old_stdoutZ
old_stderrZold_forced_widthZstream_mixerr   r   r   r   Zold_visible_prompt_funcZold_hidden_prompt_funcZold__getchar_funcZold_should_strip_ansiZold__compat_should_strip_ansiZold_envkeyvaluer   )r   r   r   	isolation  s   "

	

zCliRunner.isolationargsstr | cabc.Sequence[str] | Noner   extrar   rf   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  |	d  }|	d	  }|	d
  }n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 | Nonerv   r      rr   )r   r   rc   r   shlexsplitpopKeyErrorr   main
SystemExitr   rp   r`   ra   coder%   rP   r!   r   rH   getvaluerf   )r   r   r   r   r~   r   r   r   rp   Z
outstreamsrl   rn   rm   r   eZe_coderP   rQ   r   r   r   r   invoke  sr   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 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)r}   NFT)
r]   r   r~   r   r   r   r   r   r   r   )r   r   r   r   r   )r   r   r   r   )NNF)r   r\   r~   r   r   r   r   r   )NNNNF)r   r   r   r   r   r\   r~   r   r   r   r   r   r   r   r   rf   )r   r   r   r   )r;   r<   r=   rK   r   r   r   
contextlibcontextmanagerr   r   r   r   r   r   r   rh      s2    
	 prh   )r>   r?   r   r@   )r   r\   r]   r   r   r   )&
__future__r   collections.abcabcZcabcr   rN   r   r   r   r   r   typingr`   typesr   r   r   r   r   r   r	   TYPE_CHECKINGZ	_typeshedr
   corer   r   r   rA   rO   rB   rM   TextIOWrapperrR   re   rf   rh   r   r   r   r   <module>   s8    "	
N