3
pXú*  ã               @   s°   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mZ eje	j
ddƒd   Zer`d dlmZ nd dlZddlmZ G dd„ deƒZd	d
„ ZG dd„ deƒZG dd„ deƒZdS )é    Né   )Ú	iteritemsÚPY2Ú.)ÚStringIO)Ú_find_binary_readerc               @   sP   e Zd Zdd„ Zdd„ Zdd„ Zddd	„Zdd
d„Zdd„ Zdd„ Z	dd„ Z
dS )ÚEchoingStdinc             C   s   || _ || _d S )N)Ú_inputÚ_output)ÚselfÚinputÚoutput© r   ú//usr/lib/python3/dist-packages/click/testing.pyÚ__init__   s    zEchoingStdin.__init__c             C   s   t | j|ƒS )N)Úgetattrr	   )r   Úxr   r   r   Ú__getattr__   s    zEchoingStdin.__getattr__c             C   s   | j j|ƒ |S )N)r
   Úwrite)r   Úrvr   r   r   Ú_echo!   s    zEchoingStdin._echor   c             C   s   | j | jj|ƒƒS )N)r   r	   Úread)r   Únr   r   r   r   %   s    zEchoingStdin.readc             C   s   | j | jj|ƒƒS )N)r   r	   Úreadline)r   r   r   r   r   r   (   s    zEchoingStdin.readlinec                s   ‡ fdd„ˆ j jƒ D ƒS )Nc                s   g | ]}ˆ j |ƒ‘qS r   )r   )Ú.0r   )r   r   r   ú
<listcomp>,   s    z*EchoingStdin.readlines.<locals>.<listcomp>)r	   Ú	readlines)r   r   )r   r   r   +   s    zEchoingStdin.readlinesc                s   t ‡ fdd„ˆ jD ƒƒS )Nc             3   s   | ]}ˆ j |ƒV  qd S )N)r   )r   r   )r   r   r   ú	<genexpr>/   s    z(EchoingStdin.__iter__.<locals>.<genexpr>)Úiterr	   )r   r   )r   r   Ú__iter__.   s    zEchoingStdin.__iter__c             C   s
   t | jƒS )N)Úreprr	   )r   r   r   r   Ú__repr__1   s    zEchoingStdin.__repr__Néÿÿÿÿ)r"   r"   )r"   )Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r   r   r   r!   r   r   r   r   r      s   

r   c             C   sf   t | dƒr.tr| S t| ƒ}|d k	r&|S tdƒ‚| d kr<d} nt| tƒsP| j|ƒ} tr\t| ƒS tj	| ƒS )Nr   z.Could not find binary reader for input stream.ó    )
Úhasattrr   r   Ú	TypeErrorÚ
isinstanceÚbytesÚencoder   ÚioÚBytesIO)r   Úcharsetr   r   r   r   Úmake_input_stream5   s    


r/   c               @   s.   e Zd ZdZd	dd„Zedd„ ƒZdd„ ZdS )
ÚResultz3Holds the captured result of an invoked CLI script.Nc             C   s"   || _ || _|| _|| _|| _d S )N)ÚrunnerÚoutput_bytesÚ	exit_codeÚ	exceptionÚexc_info)r   r1   r2   r3   r4   r5   r   r   r   r   K   s
    zResult.__init__c             C   s   | j j| jjdƒjddƒS )zThe output as unicode string.Úreplacez
Ú
)r2   Údecoder1   r.   r6   )r   r   r   r   r   X   s    zResult.outputc             C   s   d| j rt| j ƒpdf S )Nz<Result %s>Zokay)r4   r    )r   r   r   r   r!   ^   s    zResult.__repr__)N)r#   r$   r%   Ú__doc__r   Úpropertyr   r!   r   r   r   r   r0   H   s   
r0   c               @   sT   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„ ƒZdS )Ú	CliRunneraI  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.  This is
                    UTF-8 by default and should not be changed currently as
                    the reporting to Click only works in Python 2 properly.
    :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.
    NFc             C   s&   |d krd}|| _ |pi | _|| _d S )Nzutf-8)r.   ÚenvÚ
echo_stdin)r   r.   r<   r=   r   r   r   r   t   s
    
zCliRunner.__init__c             C   s
   |j pdS )zžGiven a command object it will return the default program name
        for it.  The default is the `name` attribute or ``"root"`` if not
        set.
        Úroot)Úname)r   Úclir   r   r   Úget_default_prog_name{   s    zCliRunner.get_default_prog_namec             C   s   t | jƒ}|r|j|ƒ |S )z8Returns the environment overrides for invoking a script.)Údictr<   Úupdate)r   Z	overridesr   r   r   r   Úmake_env‚   s    

zCliRunner.make_envc             #   s   t ˆ| jƒ‰tj}tj}tj}tjj}dtj_| j	|ƒ}t
r`tƒ  t_ t_}| jr tˆ|ƒ‰n@tjƒ }| jrxtˆ|ƒ‰tjˆ| jd‰tj|| jd t_t_ˆt_d‡fdd„	}	d‡fdd„	}
dd	„ }|‰ d‡ fd
d„	}tjj}tjj}tjj}tjj}|	tj_|
tj_|tj_|tj_i }zlx`t|ƒD ]T\}}tjj|ƒ||< |dkrtytj|= W n tk
rp   Y nX n
|tj|< q,W |V  W dxPt|ƒD ]D\}}|dkrÐytj|= W n tk
rÌ   Y nX n
|tj|< q˜W |t_|t_|t_|tj_|tj_|tj_|tj_|tj_X dS )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.

        .. versionadded:: 4.0
           The ``color`` parameter was added.

        :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.
        éP   )ÚencodingNc                s<   t jj| pdƒ ˆ jƒ jdƒ}t jj|d ƒ t jjƒ  |S )NÚ z
r7   )ÚsysÚstdoutr   r   ÚrstripÚflush)ÚpromptÚval)r   r   r   Úvisible_input³   s
    
z*CliRunner.isolation.<locals>.visible_inputc                s,   t jj| pdd ƒ t jjƒ  ˆ jƒ jdƒS )NrG   r7   z
)rH   rI   r   rK   r   rJ   )rL   )r   r   r   Úhidden_inputº   s    
z)CliRunner.isolation.<locals>.hidden_inputc             S   s*   t jjdƒ}| r&t jj|ƒ t jjƒ  |S )Nr   )rH   Ústdinr   rI   r   rK   )ZechoÚcharr   r   r   Ú_getchar¿   s
    
z%CliRunner.isolation.<locals>._getcharc                s   |d krˆ  S | S )Nr   )ÚstreamÚcolor)Údefault_colorr   r   Úshould_strip_ansiÇ   s    z.CliRunner.isolation.<locals>.should_strip_ansi)N)N)NN)r/   r.   rH   rP   rI   ÚstderrÚclickpkgZ
formattingZFORCED_WIDTHrD   r   r   r=   r   r,   r-   ÚTextIOWrapperZtermuiZvisible_prompt_funcZhidden_prompt_funcrR   ZutilsrV   r   ÚosÚenvironÚgetÚ	Exception)r   r   r<   rT   Z	old_stdinZ
old_stdoutZ
old_stderrZold_forced_widthZbytes_outputrN   rO   rR   rV   Zold_visible_prompt_funcZold_hidden_prompt_funcZold__getchar_funcZold_should_strip_ansiZold_envÚkeyÚvaluer   )rU   r   r   Ú	isolation‰   sr    




zCliRunner.isolationTc          "   K   s  d}| j |||dì}	d}
d}zÈy$|jf |p.f | j|ƒdœ|—Ž W nž tk
r¬ } zL|jdkrd|}
tjƒ }|j}t|tƒsœtj	j
t|ƒƒ tj	j
dƒ d}W Y dd}~X n8 tk
râ } z|sÂ‚ |}
d}tjƒ }W Y dd}~X nX W dtj	jƒ  |	jƒ }X W dQ R X t| |||
|dS )	ae  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.

        .. versionadded:: 3.0
           The ``catch_exceptions`` parameter was added.

        .. versionchanged:: 3.0
           The result object now has an `exc_info` attribute with the
           traceback if available.

        .. versionadded:: 4.0
           The ``color`` parameter was added.

        :param cli: the command to invoke
        :param args: the arguments to invoke
        :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.
        N)r   r<   rT   r   )ÚargsZ	prog_namer7   r   )r1   r2   r3   r4   r5   r"   )r`   ÚmainrA   Ú
SystemExitÚcoderH   r5   r)   ÚintrI   r   Ústrr]   rK   Úgetvaluer0   )r   r@   ra   r   r<   Zcatch_exceptionsrT   Zextrar5   Úoutr4   r3   Úer   r   r   r   Úinvokeó   s:    


zCliRunner.invokec             c   s^   t jƒ }tjƒ }t j|ƒ z
|V  W dt j|ƒ ytj|ƒ W n ttfk
rV   Y nX X dS )z‘A context manager that creates a temporary folder and changes
        the current working directory to it for isolated filesystem tests.
        N)	rZ   ÚgetcwdÚtempfileZmkdtempÚchdirÚshutilZrmtreeÚOSErrorÚIOError)r   ÚcwdÚtr   r   r   Úisolated_filesystem3  s    


zCliRunner.isolated_filesystem)NNF)N)NNF)NNNTF)r#   r$   r%   r9   r   rA   rD   Ú
contextlibÚcontextmanagerr`   rj   rs   r   r   r   r   r;   d   s   

i 
?r;   )rZ   rH   rn   rl   rt   Z_compatr   r   Úmodulesr#   ÚrsplitrX   Ú	cStringIOr   r,   r   Úobjectr   r/   r0   r;   r   r   r   r   Ú<module>   s   