B
    +‹dè  ã               @   sZ   d dl Z d dlZd dlZd dlmZ e jd
dd„ƒZe jddd„ƒZG dd	„ d	e jƒZ	dS )é    N)ÚEOFc          
   #   sÈ   |   ¡ }|d k	r|S | jjs^tƒ ‰ ˆ  | ¡ t ¡  ‡ fdd„| j¡E d H \}‰ ˆ |f| j_n| jj\‰ }ˆ  | ¡ | ¡  yt 	ˆ j
|¡E d H S  tjk
rÂ } z| ¡  |  |¡S d }~X Y nX d S )Nc                  s   ˆ S )N© r   )Úpwr   ú[/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/pexpect/_async.pyÚ<lambda>   ó    zexpect_async.<locals>.<lambda>)Zexisting_dataÚspawnZasync_pw_transportÚPatternWaiterÚset_expecterÚasyncioÚget_event_loopÚconnect_read_pipeÚresume_readingÚwait_forÚfutÚTimeoutErrorÚpause_readingÚtimeout)Úexpecterr   ÚidxÚ	transportÚer   )r   r   Úexpect_async   s"    

r   éÿÿÿÿc             c   s²   g }| j  |d ¡ xB|dd … D ]2}| j|ddE d H  | | j j¡ | j  |¡ q"W | j|ddE d H }|dkrž| j  tj¡ | jdddE d H  tdƒ‚d 	|| j jg ¡S )Nr   é   T)r   Zasync_z1Continuation prompt found - input was incomplete:Ú )
ÚchildZsendlineZ_expect_promptÚappendÚbeforeÚkillÚsignalÚSIGINTÚ
ValueErrorÚjoin)ÚreplZcmdlinesr   ÚresÚlineZ
prompt_idxr   r   r   Úrepl_run_command_async   s    r'   c               @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )r	   Nc             C   s   || _ t ¡ | _d S )N)r   r   ÚFuturer   )Úselfr   r   r   r   r
   3   s    zPatternWaiter.set_expecterc             C   s$   | j  ¡ s | j  |¡ | j ¡  d S )N)r   ÚdoneÚ
set_resultr   r   )r)   Úresultr   r   r   Úfound7   s    
zPatternWaiter.foundc             C   s$   | j  ¡ s | j  |¡ | j ¡  d S )N)r   r*   Úset_exceptionr   r   )r)   Úexcr   r   r   Úerror<   s    
zPatternWaiter.errorc             C   s
   || _ d S )N)r   )r)   r   r   r   r   Úconnection_madeA   s    zPatternWaiter.connection_madec          
   C   s¤   | j j}|j |¡}| |d¡ | j ¡ rF|j |¡ |j	 |¡ d S y"| j  
|¡}|d k	rf|  |¡ W n6 tk
rž } z| j  ¡  |  |¡ W d d }~X Y nX d S )NÚread)r   r   Ú_decoderÚdecodeÚ_logr   r*   Z_beforeÚwriteÚ_bufferZnew_datar-   Ú	ExceptionZerroredr0   )r)   Údatar   ÚsÚindexr   r   r   r   Údata_receivedD   s    

zPatternWaiter.data_receivedc          
   C   sT   yd| j j_| j  ¡ }W n, tk
rD } z|  |¡ W d d }~X Y nX |  |¡ d S )NT)r   r   Zflag_eofÚeofr   r0   r-   )r)   r;   r   r   r   r   Úeof_receivedW   s    
zPatternWaiter.eof_receivedc             C   s6   t |tƒr |jtjkr |  ¡  n|d k	r2|  |¡ d S )N)Ú
isinstanceÚOSErrorÚerrnoÚEIOr>   r0   )r)   r/   r   r   r   Úconnection_lostb   s    
zPatternWaiter.connection_lost)Ú__name__Ú
__module__Ú__qualname__r   r
   r-   r0   r1   r<   r>   rC   r   r   r   r   r	   0   s   r	   )N)r   )
r   rA   r    Zpexpectr   Ú	coroutiner   r'   ÚProtocolr	   r   r   r   r   Ú<module>   s   