U
    
W[-                     @   s   d Z ddlZddlmZ ddlmZmZ ddlmZm	Z	 ddl
mZ i ZG dd deZeejG d	d
 d
eZG dd deZG dd dejZG dd deZdS )z4
Basic line editing support.

@author: Jp Calderone
    N)implementer)insultshelper)logreflect)	iterbytesc                       s@   e Zd ZdZdd Z fddZ fddZ fdd	Z  ZS )
Loggingz
    Wrapper which logs attribute lookups.

    This was useful in debugging something, I guess.  I forget what.
    It can probably be deleted or moved somewhere more appropriate.
    Nothing special going on here, really.
    c                 C   sF   || _ t|j}t|d}|d t|< t|d t| d| _d S )Nr      -w)	originalr   Zqual	__class__	_countersgetopenstr_logFile)selfr   keycount r   8/usr/lib/python3/dist-packages/twisted/conch/recvline.py__init__   s
    zLogging.__init__c                    s   t tt| dS Nr   )r   superr   __getattribute__r   r   r   r   __str__%   s    zLogging.__str__c                    s   t tt| dS r   )reprr   r   r   r   r   r   r   __repr__)   s    zLogging.__repr__c                    s8   t t| d}t t| d}||d  t||S )Nr   r   
)r   r   r   writegetattr)r   namer   ZlogFiler   r   r   r   -   s    zLogging.__getattribute__)	__name__
__module____qualname____doc__r   r   r    r   __classcell__r   r   r   r   r      s
   r   c                   @   s`   e Zd ZdZdD ]Zed ZedZee qdZ	dZ
dd ZejD ]Zed	eef  qFd
S )TransportSequencea_  
    An L{ITerminalTransport} implementation which forwards calls to
    one or more other L{ITerminalTransport}s.

    This is a cheap way for servers to keep track of the state they
    expect the client to see, since all terminal manipulations can be
    send to the real client and to a terminal emulator that lives in
    the server process.
    )s   UP_ARROWs
   DOWN_ARROWs   RIGHT_ARROWs
   LEFT_ARROWs   HOMEs   INSERTs   DELETEs   ENDs   PGUPs   PGDNs   F1s   F2s   F3s   F4s   F5s   F6s   F7s   F8s   F9s   F10s   F11s   F12s    = object()ascii   	   c                 G   s   |st d|| _d S )Nz7Cannot construct a TransportSequence with no transports)AssertionError
transports)r   r/   r   r   r   r   L   s    zTransportSequence.__init__zldef %s(self, *a, **kw):
    for tpt in self.transports:
        result = tpt.%s(*a, **kw)
    return result
N)r%   r&   r'   r(   keyIDZ	execBytesdecodeZexecStrexecTAB	BACKSPACEr   r   ITerminalTransportmethodr   r   r   r   r*   5   s   



r*   c                       s(   e Zd ZdZ fddZdd Z  ZS )LocalTerminalBufferMixina  
    A mixin for RecvLine subclasses which records the state of the terminal.

    This is accomplished by performing all L{ITerminalTransport} operations on both
    the transport passed to makeConnection and an instance of helper.TerminalBuffer.

    @ivar terminalCopy: A L{helper.TerminalBuffer} instance which efforts
    will be made to keep up to date with the actual terminal
    associated with this protocol instance.
    c                    s,   t  | _| j  tt| t|| jS N)r   ZTerminalBufferterminalCopyconnectionMader   r7   makeConnectionr*   )r   Z	transportr   r   r   r;   g   s
    



z'LocalTerminalBufferMixin.makeConnectionc                 C   s
   t | jS r8   )r   r9   r   r   r   r   r   n   s    z LocalTerminalBufferMixin.__str__)r%   r&   r'   r(   r;   r   r)   r   r   r   r   r7   [   s   r7   c                   @   s   e Zd ZdZdZdZdZdZdZe	j
dZdd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Z d0S )1RecvLinea  
    L{TerminalProtocol} which adds line editing features.

    Clients will be prompted for lines of input with all the usual
    features: character echoing, left and right arrow support for
    moving the cursor to different areas of the line buffer, backspace
    and delete for removing characters, and insert for toggling
    between typeover and insert mode.  Tabs will be expanded to enough
    spaces to move the cursor to the next tabstop (every four
    characters by default).  Enter causes the line buffer to be
    cleared and the line to be passed to the lineReceived() method
    which, by default, does nothing.  Subclasses are responsible for
    redrawing the input prompt (this will probably change).
    P         )s   >>> s   ... r   r+   c                 C   sp   g | _ d| _| j}|j| j|j| j|j| jd| j	d| j	|j
| j|j| j|j| j|j| j|j| ji
| _|   d S )Nr         
)
lineBufferlineBufferIndexterminalZ
LEFT_ARROWhandle_LEFTZRIGHT_ARROWhandle_RIGHTr3   
handle_TABhandle_RETURNr4   handle_BACKSPACEZDELETEhandle_DELETEZINSERThandle_INSERTHOMEhandle_HOMEZEND
handle_ENDkeyHandlersinitializeScreenr   tr   r   r   r:      s2              zRecvLine.connectionMadec                 C   s*   | j   | j | j| j  |   d S r8   )rD   resetr"   pspnsetInsertModer   r   r   r   rP      s    

zRecvLine.initializeScreenc                 C   s(   d | j}|d | j || jd  fS N    )joinrB   rC   )r   sr   r   r   currentLineBuffer   s    zRecvLine.currentLineBufferc                 C   s   d| _ | jtjjg d S )Ninsert)moderD   ZsetModesr   modesIRMr   r   r   r   rV      s    zRecvLine.setInsertModec                 C   s   d| _ | jtjjg d S )Ntypeover)r]   rD   Z
resetModesr   r^   r_   r   r   r   r   setTypeoverMode   s    zRecvLine.setTypeoverModec                 C   s$   | j | j| j d| j  dS )z
        Write a line containing the current input prompt and the current line
        buffer at the current cursor position.
        rX   N)rD   r"   rT   rU   rY   rB   r   r   r   r   drawInputLine   s    zRecvLine.drawInputLinec                 C   s,   | j   | j   || _|| _|   d S r8   )rD   ZeraseDisplayZ
cursorHomewidthheightrb   )r   rc   rd   r   r   r   terminalSize   s
    

zRecvLine.terminalSizec                 C   s   d S r8   r   )r   seqr   r   r   unhandledControlSequence   s    z!RecvLine.unhandledControlSequencec                 C   sH   | j |}|d k	r|  n(|| jkr4| |d ntd|f  d S )NFzReceived unhandled keyID: %r)rO   r   _printableCharscharacterReceivedr   msg)r   r0   Zmodifiermr   r   r   keystrokeReceived   s    
zRecvLine.keystrokeReceivedc                 C   sR   | j dkr| j| j| n|g| j| j| jd < |  jd7  _| j| d S )Nr\   r	   )r]   rB   r\   rC   rD   r"   )r   chZmoreCharactersComingr   r   r   ri      s
    
zRecvLine.characterReceivedc                 C   sH   | j t| j| j   }| j| |  j|7  _| jtd|  d S )N    )TABSTOPlenrB   rD   cursorForwardrC   extendr   )r   nr   r   r   rG      s    zRecvLine.handle_TABc                 C   s&   | j dkr"|  j d8  _ | j  d S Nr   r	   rC   rD   cursorBackwardr   r   r   r   rE      s    
zRecvLine.handle_LEFTc                 C   s,   | j t| jk r(|  j d7  _ | j  d S )Nr	   )rC   rp   rB   rD   rq   r   r   r   r   rF      s    zRecvLine.handle_RIGHTc                 C   s   | j r| j| j  d| _ d S Nr   ru   r   r   r   r   rM     s    zRecvLine.handle_HOMEc                 C   s0   t | j| j }|r,| j| t | j| _d S r8   )rp   rB   rC   rD   rq   )r   offsetr   r   r   rN   
  s    zRecvLine.handle_ENDc                 C   s:   | j dkr6|  j d8  _ | j| j = | j  | j  d S rt   )rC   rB   rD   rv   deleteCharacterr   r   r   r   rI     s
    


zRecvLine.handle_BACKSPACEc                 C   s(   | j t| jk r$| j| j = | j  d S r8   )rC   rp   rB   rD   ry   r   r   r   r   rJ     s    
zRecvLine.handle_DELETEc                 C   s0   d | j}g | _d| _| j  | | d S )NrX   r   )rY   rB   rC   rD   ZnextLinelineReceivedr   liner   r   r   rH     s
    
zRecvLine.handle_RETURNc                 C   s.   | j dkst| j dkr"|   n|   d S )N)r`   r\   r`   )r]   r.   rV   ra   r   r   r   r   rK   '  s    

zRecvLine.handle_INSERTc                 C   s   d S r8   r   r{   r   r   r   rz   /  s    zRecvLine.lineReceivedN)!r%   r&   r'   r(   rc   rd   ro   rT   rU   stringZ	printableencoderh   r:   rP   r[   rV   ra   rb   re   rg   rl   ri   rG   rE   rF   rM   rN   rI   rJ   rH   rK   rz   r   r   r   r   r<   s   s6   

	r<   c                   @   s@   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S )HistoricRecvLinea;  
    L{TerminalProtocol} which adds both basic line-editing features and input history.

    Everything supported by L{RecvLine} is also supported by this class.  In addition, the
    up and down arrows traverse the input history.  Each received line is automatically
    added to the end of the input history.
    c                 C   s<   t |  g | _d| _| j}| j|j| j|j	| j
i d S rw   )r<   r:   historyLineshistoryPositionrD   rO   updateZUP_ARROW	handle_UPZ
DOWN_ARROWhandle_DOWNrQ   r   r   r   r:   <  s    
 zHistoricRecvLine.connectionMadec                 C   s&   t | j}|d | j || jd  fS r8   )tupler   r   )r   br   r   r   currentHistoryBufferG  s    
z%HistoricRecvLine.currentHistoryBufferc                 C   s>   |r:t |d d D ]}| |d q| |dd  d d S )NTF)r   ri   )r   Zbufrm   r   r   r   _deliverBufferL  s    zHistoricRecvLine._deliverBufferc                 C   sp   | j r*| jt| jkr*| jd| j  | jdkrl|   | j  |  jd8  _g | _ | 	| j| j  d S )NrX   r   r	   )
rB   r   rp   r   appendrY   rM   rD   eraseToLineEndr   r   r   r   r   r   S  s    

zHistoricRecvLine.handle_UPc                 C   s|   | j t| jd k rN|   | j  |  j d7  _ g | _| | j| j   n*|   | j  t| j| _ g | _d| _d S )Nr	   r   )	r   rp   r   rM   rD   r   rB   r   rC   r   r   r   r   r   `  s    

zHistoricRecvLine.handle_DOWNc                 C   s0   | j r| jd| j  t| j| _t| S rW   )rB   r   r   rY   rp   r   r<   rH   r   r   r   r   rH   r  s    zHistoricRecvLine.handle_RETURNN)
r%   r&   r'   r(   r:   r   r   r   r   rH   r   r   r   r   r   4  s   r   )r(   r}   Zzope.interfacer   Ztwisted.conch.insultsr   r   Ztwisted.pythonr   r   Ztwisted.python.compatr   r   objectr   r5   r*   r7   ZTerminalProtocolr<   r   r   r   r   r   <module>   s    % B