U
    
W[Ú
  ã                   @   s¾   d Z ddlZddlZddlZddlZddlmZmZmZm	Z	 ddl
mZmZmZ ddlmZ ddl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 )zC
Asynchronous local terminal input handling

@author: Jp Calderone
é    N)ÚreactorÚstdioÚprotocolÚdefer)ÚfailureÚreflectÚlog)ÚServerProtocol)ÚColoredManholec                   @   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   ZDeferredÚonConnection)Úselfr   r   r   r   Ú__init__   s    z TerminalProcessProtocol.__init__c                 C   s"   | j  | ¡ | j d ¡ d | _d S r   )r   ZmakeConnectionr   Úcallback)r   r   r   r   ÚconnectionMade   s    z&TerminalProcessProtocol.connectionMadec                 C   s   | 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   $   s    zTerminalProcessProtocol.writec                 C   s   | j  |¡ dS )zs
        Receive data from the terminal.

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

        @param data: Data to include in L{Failure}.
        @type data: L{bytes}
        N)r   ZloseConnectionr   ÚconnectionLostr   ZFailurer   r   r   r   r   ÚerrReceived8   s    

z#TerminalProcessProtocol.errReceivedc                 C   s   | j d k	r| j  |¡ d S r   )r   ÚchildConnectionLost)r   ZchildFDr   r   r   r    E   s    
z+TerminalProcessProtocol.childConnectionLostc                 C   s    | j d k	r| j  |¡ d | _ d S r   )r   r   ©r   Úreasonr   r   r   ÚprocessEndedJ   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   U   s    zConsoleManhole.connectionLostN)r   r   r   Ú__doc__r   r   r   r   r   r$   Q   s   r$   c                 C   s`   t j ¡ }t |¡}t |¡ zt| ƒ}t |¡ t ¡  W 5 t |tj|¡ t	 
|d¡ X d S )Ns   c)ÚsysÚ	__stdin__ÚfilenoÚtermiosZ	tcgetattrÚttyZsetrawZ	tcsetattrZTCSANOWÚosr   r	   r   Z
StandardIOr   Úrun)ÚklassÚfdZoldSettingsÚpr   r   r   ÚrunWithProtocol^   s    



r1   c                 C   sJ   t  tddƒ¡ | d kr&tjdd … } | r:t | d ¡}nt}t|ƒ d S )Nz	child.logÚwé   r   )	r   ZstartLoggingÚopenr'   Úargvr   Z
namedClassr$   r1   )r5   r.   r   r   r   Úmainl   s    r6   Ú__main__)N)r&   r,   r+   r'   r*   Ztwisted.internetr   r   r   r   Ztwisted.pythonr   r   r   Ztwisted.conch.insults.insultsr	   Ztwisted.conch.manholer
   Ú	Exceptionr   ZProcessProtocolr   r$   r1   r6   r   r   r   r   r   Ú<module>   s    9
