o
    !^                     @   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 ur|S | jjs0t   |  t  fdd| jE d H \}  |f| j_n| jj\ } |  |  zt	 j
|E d H W S  tjyf } z|  | |W  Y d }~S d }~ww )Nc                      s    S N r   pwr   0/usr/lib/python3/dist-packages/pexpect/_async.py<lambda>   s    zexpect_async.<locals>.<lambda>)existing_dataspawn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  |dd  D ]}| j|ddE d H  || j j | j | q| j|ddE d H }|dkrN| j tj | jdddE d H  tdd	|| j jg S )Nr      T)r   async_z1Continuation prompt found - input was incomplete: )
childsendline_expect_promptappendbeforekillsignalSIGINT
ValueErrorjoin)replcmdlinesr   resline
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 r   )r   r   Futurer   )selfr   r   r   r   r   3   s   zPatternWaiter.set_expecterc                 C   (   | j  s| j | | j  d S d S r   )r   done
set_resultr   r   )r1   resultr   r   r   found7      
zPatternWaiter.foundc                 C   r2   r   )r   r3   set_exceptionr   r   r1   excr   r   r   error<   r7   zPatternWaiter.errorc                 C   s
   || _ d S r   )r   )r1   r   r   r   r   connection_madeA   s   
zPatternWaiter.connection_madec              
   C   s   | j j}|j|}||d | j r#|j| |j	| d S z| j 
|}|d ur6| | W d S W d S  tyU } z| j   | | W Y d }~d S d }~ww )Nread)r   r
   _decoderdecode_logr   r3   _beforewrite_buffernew_datar6   	Exceptionerroredr;   )r1   datar
   sindexr   r   r   r   data_receivedD   s"   

zPatternWaiter.data_receivedc              
   C   sX   zd| j j_| j  }W n ty$ } z| | W Y d }~d S d }~ww | | d S )NT)r   r
   flag_eofeofr   r;   r6   )r1   rI   r   r   r   r   eof_receivedW   s   
zPatternWaiter.eof_receivedc                 C   s<   t |tr|jtjkr|   d S |d ur| | d S d S r   )
isinstanceOSErrorerrnoEIOrM   r;   r9   r   r   r   connection_lostb   s
   zPatternWaiter.connection_lost)__name__
__module____qualname__r   r   r6   r;   r<   rJ   rM   rR   r   r   r   r   r   0   s    r   r   )r   )
r   rP   r&   pexpectr   	coroutiner   r/   Protocolr   r   r   r   r   <module>   s    