3
moW-                 @   sj   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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jd | jj  |d krR|| _n| j	||j
|| || _|| _| j  |d k	r| j| d S )NFz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   2/usr/lib/python3/dist-packages/pexpect/replwrap.py__init__!   s    

zREPLWrapper.__init__c             C   s   | j j| | j j| d S )N)r
   Zexpectsendline)r   r   r   r   r   r   r   <   s    zREPLWrapper.set_prompt   c             C   s   | j j| j| jg|dS )N)timeout)r
   Zexpect_exactr   r   )r   r   r   r   r   r   @   s    zREPLWrapper._expect_promptc             C   s   |j  }|jdr|jd |s(tdg }| jj|d  x:|dd D ]*}| j|d |j| jj | jj| qJW | j|ddkr| jjt	j
 | jdd td| dj|| jjg S )	a  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.
        
 zNo command was givenr   r   N)r   z2Continuation prompt found - input was incomplete:
)
splitlinesendswithappend
ValueErrorr
   r   r   ZbeforekillsignalSIGINTjoin)r   commandr   Zcmdlinesresliner   r   r   r   D   s"    

zREPLWrapper.run_command)r(   r(   )r(   )
__name__
__module____qualname____doc__PEXPECT_PROMPTPEXPECT_CONTINUATION_PROMPTr   r   r   r   r   r   r   r   r      s   
r   pythonc             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   r/   g   s    bashc             C   s   t jjt jjtd}tj| d|gddd}tdd d tdd  }tdd d tdd  }d	j	||}t
|d
|ddS )z<Start a bash shell and return a :class:`REPLWrapper` object.z	bashrc.shz--rcfileFzutf-8)r   r   N   z\[\]z%PS1='{0}' PS2='{1}' PROMPT_COMMAND=''z\$zexport PAGER=cat)r   )ospathr$   dirname__file__r   r	   r-   r.   r   r   )r%   Zbashrcr
   Zps1Zps2r   r   r   r   r0   k   s    )r/   )r0   )r,   os.pathr2   r"   sysr   version_infoZPY3strr   r-   r.   objectr   r/   r0   r   r   r   r   <module>   s   V
