ó
Lñ¦[c           @   s  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 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 d e f d	 „  ƒ  YZ d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿNi   (   t	   iteritemst   PY2t   string_typest   .i    (   t   StringIO(   t   _find_binary_readert   EchoingStdinc           B   sV   e  Z d  „  Z d „  Z d „  Z d d „ Z d d „ Z d „  Z d „  Z d „  Z	 RS(	   c         C   s   | |  _  | |  _ d  S(   N(   t   _inputt   _output(   t   selft   inputt   output(    (    s,   lib/python2.7/site-packages/click/testing.pyt   __init__   s    	c         C   s   t  |  j | ƒ S(   N(   t   getattrR   (   R	   t   x(    (    s,   lib/python2.7/site-packages/click/testing.pyt   __getattr__   s    c         C   s   |  j  j | ƒ | S(   N(   R   t   write(   R	   t   rv(    (    s,   lib/python2.7/site-packages/click/testing.pyt   _echo"   s    iÿÿÿÿc         C   s   |  j  |  j j | ƒ ƒ S(   N(   R   R   t   read(   R	   t   n(    (    s,   lib/python2.7/site-packages/click/testing.pyR   &   s    c         C   s   |  j  |  j j | ƒ ƒ S(   N(   R   R   t   readline(   R	   R   (    (    s,   lib/python2.7/site-packages/click/testing.pyR   )   s    c         C   s)   g  |  j  j ƒ  D] } |  j | ƒ ^ q S(   N(   R   t	   readlinesR   (   R	   R   (    (    s,   lib/python2.7/site-packages/click/testing.pyR   ,   s    c            s   t  ‡  f d †  ˆ  j Dƒ ƒ S(   Nc         3   s   |  ] } ˆ  j  | ƒ Vq d  S(   N(   R   (   t   .0R   (   R	   (    s,   lib/python2.7/site-packages/click/testing.pys	   <genexpr>0   s    (   t   iterR   (   R	   (    (   R	   s,   lib/python2.7/site-packages/click/testing.pyt   __iter__/   s    c         C   s   t  |  j ƒ S(   N(   t   reprR   (   R	   (    (    s,   lib/python2.7/site-packages/click/testing.pyt   __repr__2   s    (
   t   __name__t
   __module__R   R   R   R   R   R   R   R   (    (    (    s,   lib/python2.7/site-packages/click/testing.pyR      s   					c         C   s—   t  |  d ƒ rD t r |  St |  ƒ } | d  k	 r5 | St d ƒ ‚ n  |  d  k rY d }  n! t |  t ƒ sz |  j | ƒ }  n  t rŠ t |  ƒ St	 j
 |  ƒ S(   NR   s.   Could not find binary reader for input stream.t    (   t   hasattrR   R   t   Nonet	   TypeErrort
   isinstancet   bytest   encodeR   t   iot   BytesIO(   R
   t   charsetR   (    (    s,   lib/python2.7/site-packages/click/testing.pyt   make_input_stream6   s    	
t   Resultc           B   sP   e  Z d  Z d d „ Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z d „  Z	 RS(   s3   Holds the captured result of an invoked CLI script.c         C   s:   | |  _  | |  _ | |  _ | |  _ | |  _ | |  _ d  S(   N(   t   runnert   stdout_bytest   stderr_bytest	   exit_codet	   exceptiont   exc_info(   R	   R*   R+   R,   R-   R.   R/   (    (    s,   lib/python2.7/site-packages/click/testing.pyR   L   s    					c         C   s   |  j  S(   s(   The (standard) output as unicode string.(   t   stdout(   R	   (    (    s,   lib/python2.7/site-packages/click/testing.pyR   [   s    c         C   s%   |  j  j |  j j d ƒ j d d ƒ S(   s&   The standard output as unicode string.t   replaces   
s   
(   R+   t   decodeR*   R'   R1   (   R	   (    (    s,   lib/python2.7/site-packages/click/testing.pyR0   `   s    c         C   s=   |  j  s t d ƒ ‚ n  |  j  j |  j j d ƒ j d d ƒ S(   s%   The standard error as unicode string.s   stderr not separately capturedR1   s   
s   
(   R,   t
   ValueErrorR2   R*   R'   R1   (   R	   (    (    s,   lib/python2.7/site-packages/click/testing.pyt   stderrf   s    	c         C   s/   d t  |  ƒ j |  j r' t |  j ƒ p* d f S(   Ns   <%s %s>t   okay(   t   typeR   R.   R   (   R	   (    (    s,   lib/python2.7/site-packages/click/testing.pyR   o   s    N(
   R   R   t   __doc__R    R   t   propertyR   R0   R4   R   (    (    (    s,   lib/python2.7/site-packages/click/testing.pyR)   I   s   	t	   CliRunnerc           B   s€   e  Z d  Z d d e e d „ Z d „  Z d d „ Z e	 j
 d d e d „ ƒ Z d d d e e e d „ Z e	 j
 d „  ƒ Z RS(   s™  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.
    :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
    c         C   sC   | d  k r d } n  | |  _ | p' i  |  _ | |  _ | |  _ d  S(   Ns   utf-8(   R    R'   t   envt
   echo_stdint
   mix_stderr(   R	   R'   R:   R;   R<   (    (    s,   lib/python2.7/site-packages/click/testing.pyR   ‹   s    			c         C   s   | j  p d S(   sž   Given a command object it will return the default program name
        for it.  The default is the `name` attribute or ``"root"`` if not
        set.
        t   root(   t   name(   R	   t   cli(    (    s,   lib/python2.7/site-packages/click/testing.pyt   get_default_prog_name”   s    c         C   s)   t  |  j ƒ } | r% | j | ƒ n  | S(   s8   Returns the environment overrides for invoking a script.(   t   dictR:   t   update(   R	   t	   overridesR   (    (    s,   lib/python2.7/site-packages/click/testing.pyt   make_env›   s    c      	   #   sH  t  ˆ |  j ƒ ‰ t j } t j } t j } t j j } d t j _ |  j	 | ƒ } t
 r¨ t ƒ  } |  j r~ t ˆ | ƒ ‰ n  | t _ |  j s5t ƒ  }	 |	 t _ q5n t j ƒ  } |  j rÏ t ˆ | ƒ ‰ n  t j ˆ d |  j ƒ‰ t j | d |  j ƒt _ |  j s5t j ƒ  }	 t j |	 d |  j ƒt _ n  |  j rMt j t _ n  ˆ t _ d ‡ f d † }
 d ‡ f d † } d „  } | ‰  d d ‡  f d † } t j j } t j j } t j j } t j j } |
 t j _ | t j _ | t j _ | t j _ i  } zŠ xn t | ƒ D]` \ } } t j j | ƒ | | <| d k rgy t j | =Wqtt k
 rcqtXq| t j | <qW| |  j oˆ|	 f VWd xX t | ƒ D]J \ } } | d k rÛy t j | =Wqèt k
 r×qèXqž| t j | <qžW| t _ | t _ | t _ | t j _ | t j _ | t j _ | t j _ | t j _ Xd S(   sÙ  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.
        iP   t   encodingc            sP   t  j j |  p d ƒ ˆ  j ƒ  j d ƒ } t  j j | d ƒ t  j j ƒ  | S(   NR   s   
s   
(   t   sysR0   R   R   t   rstript   flush(   t   promptt   val(   R
   (    s,   lib/python2.7/site-packages/click/testing.pyt   visible_input×   s
    c            s:   t  j j |  p d d ƒ t  j j ƒ  ˆ  j ƒ  j d ƒ S(   NR   s   
s   
(   RF   R0   R   RH   R   RG   (   RI   (   R
   (    s,   lib/python2.7/site-packages/click/testing.pyt   hidden_inputÞ   s    c         S   s<   t  j j d ƒ } |  r8 t  j j | ƒ t  j j ƒ  n  | S(   Ni   (   RF   t   stdinR   R0   R   RH   (   t   echot   char(    (    s,   lib/python2.7/site-packages/click/testing.pyt   _getcharã   s
    c            s   | d  k r ˆ  S| S(   N(   R    (   t   streamt   color(   t   default_color(    s,   lib/python2.7/site-packages/click/testing.pyt   should_strip_ansiì   s    N(   R(   R'   RF   RM   R0   R4   t   clickpkgt
   formattingt   FORCED_WIDTHRD   R   R   R;   R   R<   R%   R&   t   TextIOWrapperR    t   termuit   visible_prompt_funct   hidden_prompt_funcRP   t   utilsRT   R    t   ost   environt   gett	   Exception(   R	   R
   R:   RR   t	   old_stdint
   old_stdoutt
   old_stderrt   old_forced_widtht   bytes_outputt   bytes_errorRK   RL   RP   RT   t   old_visible_prompt_funct   old_hidden_prompt_funct   old__getchar_funct   old_should_strip_ansit   old_envt   keyt   value(    (   RS   R
   s,   lib/python2.7/site-packages/click/testing.pyt	   isolation¢   s†    																c         K   sæ  d	 }	 |  j d | d | d | ƒ •}
 d	 } d } t | t ƒ rT t j | ƒ } n  y | j d ƒ } Wn  t k
 r‰ |  j | ƒ } n Xzð y# | j	 d | p¢ d d | |  WnÆ t
 k
 rB} t j ƒ  }	 | j } | d	 k rì d } n  | d k r| } n  t | t ƒ syt j j t | ƒ ƒ t j j d ƒ d } qyn7 t k
 rx} | s]‚  n  | } d } t j ƒ  }	 n XWd	 t j j ƒ  |
 d j ƒ  } |
 d o±|
 d j ƒ  } XWd	 QXt d
 |  d | d | d | d | d |	 ƒ S(   s;  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. 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.
        R
   R:   RR   i    t	   prog_namet   argss   
i   NR*   R+   R,   R-   R.   R/   (    (   R    Rn   R"   R   t   shlext   splitt   popt   KeyErrorR@   t   maint
   SystemExitRF   R/   t   codet   intR0   R   t   strR`   RH   t   getvalueR)   (   R	   R?   Rp   R
   R:   t   catch_exceptionsRR   R<   t   extraR/   t
   outstreamsR.   R-   Ro   t   eR0   R4   (    (    s,   lib/python2.7/site-packages/click/testing.pyt   invoke  sN     ! #			!c      	   c   sn   t  j ƒ  } t j ƒ  } t  j | ƒ z	 | VWd t  j | ƒ y t j | ƒ Wn t t f k
 rh n XXd S(   s‘   A context manager that creates a temporary folder and changes
        the current working directory to it for isolated filesystem tests.
        N(	   R]   t   getcwdt   tempfilet   mkdtempt   chdirt   shutilt   rmtreet   OSErrort   IOError(   R	   t   cwdt   t(    (    s,   lib/python2.7/site-packages/click/testing.pyt   isolated_filesystemg  s    	N(   R   R   R7   R    t   Falset   TrueR   R@   RD   t
   contextlibt   contextmanagerRn   R   RŠ   (    (    (    s,   lib/python2.7/site-packages/click/testing.pyR9   v   s   		u	N(   R]   RF   R„   R   R   Rq   t   _compatR    R   R   t   modulesR   t   rsplitRU   t	   cStringIOR   R%   R   t   objectR   R(   R)   R9   (    (    (    s,   lib/python2.7/site-packages/click/testing.pyt   <module>   s   	-