o
    ¯bÕ
  ã                   @   sÂ   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
mZmZmZ ddlmZmZmZ G dd„ deƒZG d	d
„ d
ejƒZG dd„ deƒZdd„ Zddd„Zedkr_eƒ  dS dS )zC
Asynchronous local terminal input handling

@author: Jp Calderone
é    N)ÚServerProtocol)ÚColoredManhole)ÚdeferÚprotocolÚreactorÚstdio)ÚfailureÚlogÚreflectc                   @   s   e Zd ZdS )ÚUnexpectedOutputErrorN)Ú__name__Ú
__module__Ú__qualname__© r   r   ú5/usr/lib/python3/dist-packages/twisted/conch/stdio.pyr      s    r   c                   @   sD   e 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 )ÚTerminalProcessProtocolc                 C   s   || _ t ¡ | _d S ©N)Úprotor   ÚDeferredÚonConnection)Úselfr   r   r   r   Ú__init__   s   z TerminalProcessProtocol.__init__c                 C   s"   | j  | ¡ | j d ¡ d | _d S r   )r   ÚmakeConnectionr   Úcallback)r   r   r   r   ÚconnectionMade   s   
z&TerminalProcessProtocol.connectionMadec                 C   ó   | j  |¡ dS )zj
        Write to the terminal.

        @param data: Data to write.
        @type data: L{bytes}
        N)Ú	transportÚwrite©r   Údatar   r   r   r   $   ó   zTerminalProcessProtocol.writec                 C   r   )zs
        Receive data from the terminal.

        @param data: Data received.
        @type data: L{bytes}
        N)r   ÚdataReceivedr   r   r   r   ÚoutReceived-   r    z#TerminalProcessProtocol.outReceivedc                 C   s8   | j  ¡  | jdur| j t t|ƒ¡¡ d| _dS dS )zt
        Report an error.

        @param data: Data to include in L{Failure}.
        @type data: L{bytes}
        N)r   ÚloseConnectionr   ÚconnectionLostr   ÚFailurer   r   r   r   r   ÚerrReceived6   s
   


þz#TerminalProcessProtocol.errReceivedc                 C   s   | j d ur| j  |¡ d S d S r   )r   ÚchildConnectionLost)r   ÚchildFDr   r   r   r'   B   s   
ÿz+TerminalProcessProtocol.childConnectionLostc                 C   s$   | j d ur| j  |¡ d | _ d S d S r   )r   r$   ©r   Úreasonr   r   r   ÚprocessEndedF   s   

þz$TerminalProcessProtocol.processEndedN)
r   r   r   r   r   r   r"   r&   r'   r+   r   r   r   r   r      s    		r   c                   @   s   e Zd ZdZdd„ ZdS )ÚConsoleManholezK
    A manhole protocol specifically for use with L{stdio.StandardIO}.
    c                 C   s   t  ¡  dS )zƒ
        When the connection is lost, there is nothing more to do.  Stop the
        reactor so that the process can exit.
        N)r   Ústopr)   r   r   r   r$   Q   s   zConsoleManhole.connectionLostN)r   r   r   Ú__doc__r$   r   r   r   r   r,   L   s    r,   c                 C   sv   t j ¡ }t |¡}t |¡ zt t	| ƒ¡ t
 ¡  W t |tj|¡ t |d¡ d S t |tj|¡ t |d¡ w )Ns   c)ÚsysÚ	__stdin__ÚfilenoÚtermiosÚ	tcgetattrÚttyÚsetrawr   Ú
StandardIOr   r   ÚrunÚ	tcsetattrÚTCSANOWÚosr   )ÚklassÚfdÚoldSettingsr   r   r   ÚrunWithProtocolY   s   



ÿr>   c                 C   sJ   t  tddƒ¡ | d u rtjdd … } | rt | d ¡}nt}t|ƒ d S )Nz	child.logÚwé   r   )	r	   ÚstartLoggingÚopenr/   Úargvr
   Ú
namedClassr,   r>   )rC   r;   r   r   r   Úmaine   s   rE   Ú__main__r   )r.   r:   r/   r2   r4   Útwisted.conch.insults.insultsr   Útwisted.conch.manholer   Útwisted.internetr   r   r   r   Útwisted.pythonr   r	   r
   Ú	Exceptionr   ÚProcessProtocolr   r,   r>   rE   r   r   r   r   r   Ú<module>   s"   2

ÿ