U
    puf?  ã                   @   s€   d Z ddlZddlZddlZddlZejd dkZer:eZ	dZ
dZG dd„ deƒZejfdd	„Zd
d„ Zddd„Zddd„ZdS )zEGeneric wrapper for read-eval-print-loops, a.k.a. interactive shells
é    Né   z[PEXPECT_PROMPT>z[PEXPECT_PROMPT+c                   @   s<   e Zd ZdZeedfdd„Zdd„ Zdd	d
„Zddd„Z	dS )ÚREPLWrapperaÿ  Wrapper for a REPL.

    :param cmd_or_spawn: This can either be an instance of :class:`pexpect.spawn`
      in which a REPL has already been started, or a str command to start a new
      REPL process.
    :param str orig_prompt: The prompt to expect at first.
    :param str prompt_change: A command to change the prompt to something more
      unique. If this is ``None``, the prompt will not be changed. This will
      be formatted with the new and continuation prompts as positional
      parameters, so you can use ``{}`` style formatting to insert them into
      the command.
    :param str new_prompt: The more unique prompt to expect after the change.
    :param str extra_init_cmd: Commands to do extra initialisation, such as
      disabling pagers.
    Nc                 C   s   t |tƒrtj|ddd| _n|| _| jjrB| j d¡ | j ¡  |d krR|| _n|  	|| 
||¡¡ || _|| _|  ¡  |d k	rŒ|  |¡ d S )NFúutf-8©ÚechoÚencoding)Ú
isinstanceÚ
basestringÚpexpectÚspawnÚchildr   ZsetechoZ
waitnoechoÚpromptÚ
set_promptÚformatÚcontinuation_promptÚ_expect_promptÚrun_command)ÚselfZcmd_or_spawnÚorig_promptÚprompt_changeZ
new_promptr   Úextra_init_cmd© r   ú4/tmp/pip-unpacked-wheel-v5brguv_/pexpect/replwrap.pyÚ__init__!   s     


ÿzREPLWrapper.__init__c                 C   s   | j  |¡ | j  |¡ d S )N)r   ÚexpectÚsendline)r   r   r   r   r   r   r   <   s    zREPLWrapper.set_promptéÿÿÿÿFc                 C   s   | j j| j| jg||dS )N)ÚtimeoutÚasync_)r   Zexpect_exactr   r   )r   r   r   r   r   r   r   @   s     ÿzREPLWrapper._expect_promptc                 C   sÚ   |  ¡ }| d¡r| d¡ |s(tdƒ‚|rDddlm} || ||ƒS g }| j |d ¡ |dd… D ]*}| j|d | | jj	¡ | j |¡ qd| j|ddkrÆ| j 
tj¡ | jdd td	| ƒ‚d || jj	g ¡S )
a0  Send a command to the REPL, wait for and return output.

        :param str command: The command to send. Trailing newlines are not needed.
          This should be a complete block of input that will trigger execution;
          if a continuation prompt is found after sending input, :exc:`ValueError`
          will be raised.
        :param int timeout: How long to wait for the next prompt. -1 means the
          default from the :class:`pexpect.spawn` object (default 30 seconds).
          None means to wait indefinitely.
        :param bool async_: On Python 3.4, or Python 3.3 with asyncio
          installed, passing ``async_=True`` will make this return an
          :mod:`asyncio` Future, which you can yield from to get the same
          result that this method would normally give directly.
        Ú
Ú zNo command was givené   )Úrepl_run_command_asyncr   N)r   z2Continuation prompt found - input was incomplete:
)Ú
splitlinesÚendswithÚappendÚ
ValueErrorZ_asyncr"   r   r   r   ÚbeforeÚkillÚsignalÚSIGINTÚjoin)r   Úcommandr   r   Zcmdlinesr"   ÚresÚliner   r   r   r   D   s*    

ÿzREPLWrapper.run_command)r   F)r   F)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚPEXPECT_PROMPTÚPEXPECT_CONTINUATION_PROMPTr   r   r   r   r   r   r   r   r      s   ý

r   c                 C   s   t | ddƒS )z>Start a Python shell and return a :class:`REPLWrapper` object.z>>> z(import sys; sys.ps1={0!r}; sys.ps2={1!r})r   )r,   r   r   r   Úpythono   s    r5   c                 C   sf   t j| |ddd}td d… | tdd …  }td d… | tdd …  }d ||¡}t|d|ddS )	NFr   r   é   z%PS1='{0}' PS2='{1}' PROMPT_COMMAND=''z\$zexport PAGER=cat)r   )r
   r   r3   r4   r   r   )r,   ÚargsÚnon_printable_insertr   Zps1Zps2r   r   r   r   Ú_repl_shs   s    ÿr9   Úbashc                 C   s(   t j t j t¡d¡}t| d|gddS )z<Start a bash shell and return a :class:`REPLWrapper` object.z	bashrc.shz--rcfilez\[\]©r8   )ÚosÚpathr+   ÚdirnameÚ__file__r9   )r,   Zbashrcr   r   r   r:      s    Úzsh©z--no-rcsz-Vz+Zc                 C   s   t | t|ƒddS )z;Start a zsh shell and return a :class:`REPLWrapper` object.z%(!..)r;   )r9   Úlist)r,   r7   r   r   r   r@   †   s    )r:   )r@   rA   )r2   Úos.pathr<   r)   Úsysr
   Úversion_infoÚPY3Ústrr	   r3   r4   Úobjectr   Ú
executabler5   r9   r:   r@   r   r   r   r   Ú<module>   s   ^
