o
    g?                     @   s0  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rMdd	lmZ G d
d dZe jdeje dejd fddZG dd dejZdejejeeejej f  dedej fddZ!G dd dZ"G dd dZ#dS )    N)TracebackType   )_compat)
formatting)termui)utils)_find_binary_reader)BaseCommandc                   @   s   e Zd ZdejdejddfddZdedejfdd	Zd
e	de	fddZ
ddede	fddZddede	fddZddede	fddZdeje	 fddZdeje	 fddZdefddZdS )EchoingStdininputoutputreturnNc                 C   s   || _ || _d| _d S )NF)_input_output_paused)selfr   r    r   O/var/www/html/ecg_monitoring/venv/lib/python3.10/site-packages/click/testing.py__init__   s   
zEchoingStdin.__init__xc                 C   s   t | j|S N)getattrr   )r   r   r   r   r   __getattr__   s   zEchoingStdin.__getattr__rvc                 C   s   | j s	| j| |S r   )r   r   write)r   r   r   r   r   _echo   s   zEchoingStdin._echonc                 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.readlinec                    s    fdd j  D S )Nc                    s   g | ]}  |qS r   r   .0r   r   r   r   
<listcomp>.   s    z*EchoingStdin.readlines.<locals>.<listcomp>)r   	readlinesr'   r   r'   r   r)   -      zEchoingStdin.readlinesc                    s   t  fdd jD S )Nc                 3   s    | ]}  |V  qd S r   r$   r%   r'   r   r   	<genexpr>1   s    z(EchoingStdin.__iter__.<locals>.<genexpr>)iterr   r'   r   r'   r   __iter__0   r*   zEchoingStdin.__iter__c                 C   s
   t | jS r   )reprr   r'   r   r   r   __repr__3   s   
zEchoingStdin.__repr__)r   )__name__
__module____qualname__tBinaryIOr   strAnyr   bytesr   intr   r"   r#   Listr)   Iteratorr-   r/   r   r   r   r   r
      s    r
   streamr   c                 c   s*    | d u r
d V  d S d| _ d V  d| _ d S )NTF)r   )r;   r   r   r   _pause_echo7   s   

r<   c                
       sZ   e Zd Zdejdededejddf
 fddZedefd	d
Z	edefddZ
  ZS )_NamedTextIOWrapperbuffernamemodekwargsr   Nc                    s$   t  j|fi | || _|| _d S r   )superr   _name_mode)r   r>   r?   r@   rA   	__class__r   r   r   B   s   
z_NamedTextIOWrapper.__init__c                 C      | j S r   )rC   r'   r   r   r   r?   I      z_NamedTextIOWrapper.namec                 C   rG   r   )rD   r'   r   r   r   r@   M   rH   z_NamedTextIOWrapper.mode)r0   r1   r2   r3   r4   r5   r6   r   propertyr?   r@   __classcell__r   r   rE   r   r=   A   s     r=   r   charsetc                 C   sb   t | drtttjtj | }|d ur|S td| d u r"d} n
t| tr,| 	|} t
| S )Nr   z.Could not find binary reader for input stream.    )hasattrr   r3   castIOr6   	TypeError
isinstancer5   encodeioBytesIO)r   rK   r   r   r   r   make_input_streamR   s   



rU   c                   @   s   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dZedefddZedefddZedefddZdefddZdS )Resultz3Holds the captured result of an invoked CLI script.Nrunner	CliRunnerstdout_bytesstderr_bytesreturn_value	exit_code	exceptionexc_infoc                 C   s.   || _ || _|| _|| _|| _|| _|| _d S r   rW   rY   rZ   r[   r\   r]   r^   )r   rW   rY   rZ   r[   r\   r]   r^   r   r   r   r   i   s   
zResult.__init__r   c                 C   rG   )z(The (standard) output as unicode string.)stdoutr'   r   r   r   r      s   zResult.outputc                 C   s   | j | jjdddS )z&The standard output as unicode string.replace

)rY   decoderW   rK   ra   r'   r   r   r   r`      s   zResult.stdoutc                 C   s,   | j du r	td| j | jjdddS )z%The standard error as unicode string.Nzstderr not separately capturedra   rb   rc   )rZ   
ValueErrorrd   rW   rK   ra   r'   r   r   r   stderr   s
   
zResult.stderrc                 C   s,   | j rt| j nd}dt| j d| dS )Nokay< >)r]   r.   typer0   )r   exc_strr   r   r   r/      s   zResult.__repr__r   )r0   r1   r2   __doc__r7   r3   Optionalr6   r8   BaseExceptionTupleTyper   r   rI   r5   r   r`   rf   r/   r   r   r   r   rV   f   s6    
rV   c                   @   s  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dZ	ddd
efddZ
	d"dejejeeje f  d
ejeeje f fd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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dZej	d"dejejedf  d
eje fdd ZdS )%rX   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
    utf-8NFTrK   env
echo_stdin
mix_stderrr   c                 C   s    || _ |pi | _|| _|| _d S r   )rK   rs   rt   ru   )r   rK   rs   rt   ru   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)r?   )r   rv   r   r   r   get_default_prog_name   s   
zCliRunner.get_default_prog_name	overridesc                 C   s   t | j}|r|| |S )z8Returns the environment overrides for invoking a script.)dictrs   update)r   ry   r   r   r   r   make_env   s   

zCliRunner.make_envr   colorc                 #   s   t || j}d}tj}tj}tj}tj}	dt_| |}t	
 }
| jr0ttjt||
 }}t|| jddd t_| jrBd_t|
| jdddt_d}| jrVtjt_nt	
 }t|| jd	dd
dt_t|ddtjt dtffdd}t|ddtjt dtffdd}t|dtdtfdd}| 	ddtjtjtj  dtjt dtf fdd}tj}tj}tj}tj}tj}|t_|t_|t_|t_|t_i }zt|  D ]%\}}t!j"#|||< |du rzt!j"|= W q t$y   Y qw |t!j"|< q|
|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_dS |  D ]"\}}|du rhzt!j"|= W qL t$yg   Y qLw |t!j"|< qL|t_|t_|t_|t_|t_|t_|t_|t_|	t_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)encodingr?   r@   r   z<stdout>wz<stderr>backslashreplace)r   r?   r@   errorspromptr   c                    s>   t j| pd   d}t j| d t j  |S )N rb   rc   )sysr`   r   r#   rstripflush)r   val
text_inputr   r   visible_input  s
   
z*CliRunner.isolation.<locals>.visible_inputc                    s.   t j| pd d t j    dS )Nr   rc   rb   )r   r`   r   r   r#   r   )r   r   r   r   hidden_input  s   
z)CliRunner.isolation.<locals>.hidden_inputechoc                 S   s*   t jd}| rt j| t j  |S )Nr   )r   stdinr   r`   r   r   )r   charr   r   r   _getchar$  s
   
z%CliRunner.isolation.<locals>._getcharr;   r}   c                    s   |d u r  S | S r   r   )r;   r}   )default_colorr   r   should_strip_ansi0  s   z.CliRunner.isolation.<locals>.should_strip_ansir   )NN)%rU   rK   r   r   r`   rf   r   FORCED_WIDTHr|   rS   rT   rt   r3   rN   r4   r
   r=   _CHUNK_SIZEru   r<   rn   r5   boolrO   r6   r   visible_prompt_funchidden_prompt_funcr   r   r   r   itemsosenvironget	Exception)r   r   rs   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   r   r   	isolation   s   



  	


zCliRunner.isolationargscatch_exceptionsextrac                 K   s  d}| j |||d}	d}
d}d}t|trt|}z|d}W n ty0   | |}Y nw zz|jd|p9d|d|}
W ne t	y } z=t
 }ttjtjttjf  |j}|du rcd}|dkri|}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  }| jrd}n|	d  }nt
j  |	d  }| jrd}w |	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``.
        :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   rs   r}   r   	prog_namer   )r   r   rc   r   r_   )r   rQ   r5   shlexsplitpopKeyErrorrx   main
SystemExitr   r^   r3   rN   rn   Unionr8   r6   coder`   r   r   r   getvalueru   rV   )r   rv   r   r   rs   r   r}   r   r^   
outstreamsr[   r]   r\   r   ee_coder`   rf   r   r   r   invokea  sr   ,

 


.zCliRunner.invoketemp_dirzos.PathLike[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)rr   NFTr   )NNF)NNNTF)r0   r1   r2   rm   r5   r3   rn   Mappingr   r   rx   r|   
contextlibcontextmanagerr   r7   rO   r6   r:   rp   rS   rT   r   SequencerV   r   r   r   r   r   r   rX      s    

	 	
erX   )$r   rS   r   r   r   r   r   typingr3   typesr   r   r   r   r   r   r   TYPE_CHECKINGcorer	   r
   r   rn   r:   r<   TextIOWrapperr=   r   r5   r7   rO   r6   r4   rU   rV   rX   r   r   r   r   <module>   s:    " 	
: