o
    ¯bÛJ  ã                   @   sÈ   U d Z ddlZddlmZ ddlmZ ddlmZmZ ddl	m
Z
 ddlmZ ddlmZ i Zeeef ed	< G d
d„ dƒZeejƒG dd„ dƒƒZG dd„ dƒ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)ÚDict)Úimplementer)ÚhelperÚinsults)ÚLogger)Úreflect)Ú	iterbytesÚ	_countersc                       sL   e Zd ZdZdd„ Zdef‡ fdd„Zdef‡ 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   ÚqualÚ	__class__r	   ÚgetÚopenÚstrÚ_logFile)Úselfr   ÚkeyÚcount© r   ú8/usr/lib/python3/dist-packages/twisted/conch/recvline.pyÚ__init__!   s
   zLogging.__init__Úreturnc                    ó   t tƒ  d¡ƒS ©Nr   )r   ÚsuperÚ__getattribute__©r   ©r   r   r   Ú__str__(   ó   zLogging.__str__c                    r   r   )Úreprr   r   r    r!   r   r   Ú__repr__+   r#   zLogging.__repr__c                    s0   t ƒ  d¡}t ƒ  d¡}| |d ¡ t||ƒS )Nr   r   Ú
)r   r   ÚwriteÚgetattr)r   Únamer   ÚlogFiler!   r   r   r   .   s   
zLogging.__getattribute__)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   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 ƒ q#d
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zdidd„Zdidd„Zdidd„Zdid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/d0„ Z!d1d2„ Z"d3d4„ Z#d5d6„ Z$d7d8„ Z%d9d:„ Z&d;d<„ Z'd=d>„ Z(d?d@„ Z)dAdB„ Z*dCdD„ Z+dEdF„ Z,dGdH„ Z-djdJdK„Z.dLdM„ Z/dNdO„ Z0dPdQ„ Z1dRdS„ Z2dTdU„ Z3dVdW„ Z4dXdY„ Z5dZd[„ Z6did\d]„Z7did^d_„Z8did`da„Z9dbdc„ Z:ddde„ Z;dfdg„ Z<dhS )kÚ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J dƒ‚|| _ d S )Nz7Cannot construct a TransportSequence with no transports)Ú
transports)r   r4   r   r   r   r   `   s   
zTransportSequence.__init__zldef %s(self, *a, **kw):
    for tpt in self.transports:
        result = tpt.%s(*a, **kw)
    return result
c                 C   ó   t dƒ‚)Nz(Unimplemented: TransportSequence.getHost©ÚNotImplementedErrorr    r   r   r   ÚgetHosto   ó   zTransportSequence.getHostc                 C   r5   )Nz(Unimplemented: TransportSequence.getPeerr6   r    r   r   r   ÚgetPeers   r9   zTransportSequence.getPeerc                 C   r5   )Nz/Unimplemented: TransportSequence.loseConnectionr6   r    r   r   r   ÚloseConnectionw   r9   z TransportSequence.loseConnectionc                 C   r5   )Nz&Unimplemented: TransportSequence.writer6   ©r   Údatar   r   r   r'   {   r9   zTransportSequence.writec                 C   r5   )Nz.Unimplemented: TransportSequence.writeSequencer6   r<   r   r   r   ÚwriteSequence   r9   zTransportSequence.writeSequencer   c                 C   r5   )Nz)Unimplemented: TransportSequence.cursorUpr6   ©r   Únr   r   r   ÚcursorUpƒ   r9   zTransportSequence.cursorUpc                 C   r5   )Nz+Unimplemented: TransportSequence.cursorDownr6   r?   r   r   r   Ú
cursorDown‡   r9   zTransportSequence.cursorDownc                 C   r5   )Nz.Unimplemented: TransportSequence.cursorForwardr6   r?   r   r   r   ÚcursorForward‹   r9   zTransportSequence.cursorForwardc                 C   r5   )Nz/Unimplemented: TransportSequence.cursorBackwardr6   r?   r   r   r   ÚcursorBackward�   r9   z TransportSequence.cursorBackwardc                 C   r5   )Nz/Unimplemented: TransportSequence.cursorPositionr6   )r   ÚcolumnÚliner   r   r   ÚcursorPosition“   r9   z TransportSequence.cursorPositionc                 C   r5   )Nz+Unimplemented: TransportSequence.cursorHomer6   r    r   r   r   Ú
cursorHome—   r9   zTransportSequence.cursorHomec                 C   r5   )Nz&Unimplemented: TransportSequence.indexr6   r    r   r   r   Úindex›   r9   zTransportSequence.indexc                 C   r5   )Nz-Unimplemented: TransportSequence.reverseIndexr6   r    r   r   r   ÚreverseIndexŸ   r9   zTransportSequence.reverseIndexc                 C   r5   )Nz)Unimplemented: TransportSequence.nextLiner6   r    r   r   r   ÚnextLine£   r9   zTransportSequence.nextLinec                 C   r5   )Nz+Unimplemented: TransportSequence.saveCursorr6   r    r   r   r   Ú
saveCursor§   r9   zTransportSequence.saveCursorc                 C   r5   )Nz.Unimplemented: TransportSequence.restoreCursorr6   r    r   r   r   ÚrestoreCursor«   r9   zTransportSequence.restoreCursorc                 C   r5   )Nz)Unimplemented: TransportSequence.setModesr6   ©r   Úmodesr   r   r   ÚsetModes¯   r9   zTransportSequence.setModesc                 C   r5   )Nz+Unimplemented: TransportSequence.resetModesr6   )r   Úmoder   r   r   Ú
resetModes³   r9   zTransportSequence.resetModesc                 C   r5   )Nz0Unimplemented: TransportSequence.setPrivateModesr6   rN   r   r   r   ÚsetPrivateModes·   r9   z!TransportSequence.setPrivateModesc                 C   r5   )Nz2Unimplemented: TransportSequence.resetPrivateModesr6   rN   r   r   r   ÚresetPrivateModes»   r9   z#TransportSequence.resetPrivateModesc                 C   r5   )Nz6Unimplemented: TransportSequence.applicationKeypadModer6   r    r   r   r   ÚapplicationKeypadMode¿   ó   ÿz'TransportSequence.applicationKeypadModec                 C   r5   )Nz2Unimplemented: TransportSequence.numericKeypadModer6   r    r   r   r   ÚnumericKeypadModeÅ   r9   z#TransportSequence.numericKeypadModec                 C   r5   )Nz3Unimplemented: TransportSequence.selectCharacterSetr6   )r   ÚcharSetÚwhichr   r   r   ÚselectCharacterSetÉ   r9   z$TransportSequence.selectCharacterSetc                 C   r5   )Nz(Unimplemented: TransportSequence.shiftInr6   r    r   r   r   ÚshiftInÍ   r9   zTransportSequence.shiftInc                 C   r5   )Nz)Unimplemented: TransportSequence.shiftOutr6   r    r   r   r   ÚshiftOutÑ   r9   zTransportSequence.shiftOutc                 C   r5   )Nz-Unimplemented: TransportSequence.singleShift2r6   r    r   r   r   ÚsingleShift2Õ   r9   zTransportSequence.singleShift2c                 C   r5   )Nz-Unimplemented: TransportSequence.singleShift3r6   r    r   r   r   ÚsingleShift3Ù   r9   zTransportSequence.singleShift3c                 G   r5   )Nz7Unimplemented: TransportSequence.selectGraphicRenditionr6   )r   Ú
attributesr   r   r   ÚselectGraphicRenditionÝ   rV   z(TransportSequence.selectGraphicRenditionc                 C   r5   )Nz8Unimplemented: TransportSequence.horizontalTabulationSetr6   r    r   r   r   ÚhorizontalTabulationSetã   rV   z)TransportSequence.horizontalTabulationSetc                 C   r5   )Nz0Unimplemented: TransportSequence.tabulationClearr6   r    r   r   r   ÚtabulationClearé   r9   z!TransportSequence.tabulationClearc                 C   r5   )Nz3Unimplemented: TransportSequence.tabulationClearAllr6   r    r   r   r   ÚtabulationClearAllí   r9   z$TransportSequence.tabulationClearAllTc                 C   r5   )Nz1Unimplemented: TransportSequence.doubleHeightLiner6   )r   Útopr   r   r   ÚdoubleHeightLineñ   r9   z"TransportSequence.doubleHeightLinec                 C   r5   )Nz0Unimplemented: TransportSequence.singleWidthLiner6   r    r   r   r   ÚsingleWidthLineõ   r9   z!TransportSequence.singleWidthLinec                 C   r5   )Nz0Unimplemented: TransportSequence.doubleWidthLiner6   r    r   r   r   ÚdoubleWidthLineù   r9   z!TransportSequence.doubleWidthLinec                 C   r5   )Nz/Unimplemented: TransportSequence.eraseToLineEndr6   r    r   r   r   ÚeraseToLineEndý   r9   z TransportSequence.eraseToLineEndc                 C   r5   )Nz5Unimplemented: TransportSequence.eraseToLineBeginningr6   r    r   r   r   ÚeraseToLineBeginning  rV   z&TransportSequence.eraseToLineBeginningc                 C   r5   )Nz*Unimplemented: TransportSequence.eraseLiner6   r    r   r   r   Ú	eraseLine  r9   zTransportSequence.eraseLinec                 C   r5   )Nz2Unimplemented: TransportSequence.eraseToDisplayEndr6   r    r   r   r   ÚeraseToDisplayEnd  r9   z#TransportSequence.eraseToDisplayEndc                 C   r5   )Nz8Unimplemented: TransportSequence.eraseToDisplayBeginningr6   r    r   r   r   ÚeraseToDisplayBeginning  rV   z)TransportSequence.eraseToDisplayBeginningc                 C   r5   )Nz-Unimplemented: TransportSequence.eraseDisplayr6   r    r   r   r   ÚeraseDisplay  r9   zTransportSequence.eraseDisplayc                 C   r5   )Nz0Unimplemented: TransportSequence.deleteCharacterr6   r?   r   r   r   ÚdeleteCharacter  r9   z!TransportSequence.deleteCharacterc                 C   r5   )Nz+Unimplemented: TransportSequence.insertLiner6   r?   r   r   r   Ú
insertLine  r9   zTransportSequence.insertLinec                 C   r5   )Nz+Unimplemented: TransportSequence.deleteLiner6   r?   r   r   r   Ú
deleteLine!  r9   zTransportSequence.deleteLinec                 C   r5   )Nz5Unimplemented: TransportSequence.reportCursorPositionr6   r    r   r   r   ÚreportCursorPosition%  rV   z&TransportSequence.reportCursorPositionc                 C   r5   )Nz&Unimplemented: TransportSequence.resetr6   r    r   r   r   Úreset+  r9   zTransportSequence.resetc                 C   r5   )Nz9Unimplemented: TransportSequence.unhandledControlSequencer6   ©r   Úseqr   r   r   ÚunhandledControlSequence/  rV   z*TransportSequence.unhandledControlSequenceN)r   )T)=r+   r,   r-   r.   ÚkeyIDÚ	execBytesÚdecodeÚexecStrÚexecÚTABÚ	BACKSPACEr   r   ÚITerminalTransportÚmethodr8   r:   r;   r'   r>   rA   rB   rC   rD   rG   rH   rI   rJ   rK   rL   rM   rP   rR   rS   rT   rU   rW   rZ   r[   r\   r]   r^   r`   ra   rb   rc   re   rf   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   ru   r   r   r   r   r0   5   sz    



úÿ








r0   c                       s.   e Zd ZdZ‡ fdd„Zdef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|| jƒ¡S ©N)r   ÚTerminalBufferÚterminalCopyÚconnectionMader   ÚmakeConnectionr0   )r   Ú	transportr!   r   r   r„   B  s   

z'LocalTerminalBufferMixin.makeConnectionr   c                 C   s
   t | jƒS r€   )r   r‚   r    r   r   r   r"   G  s   
z LocalTerminalBufferMixin.__str__)r+   r,   r-   r.   r„   r   r"   r/   r   r   r!   r   r   6  s    r   c                   @   sÖ   e Zd ZdZdZdZdZdZdZe	j
 d¡Zeƒ 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   r1   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ÚterminalÚ
LEFT_ARROWÚhandle_LEFTÚRIGHT_ARROWÚhandle_RIGHTr{   Ú
handle_TABÚhandle_RETURNr|   Úhandle_BACKSPACEÚDELETEÚhandle_DELETEÚINSERTÚhandle_INSERTÚHOMEÚhandle_HOMEÚENDÚ
handle_ENDÚkeyHandlersÚinitializeScreen©r   Útr   r   r   rƒ   f  s   ôzRecvLine.connectionMadec                 C   s*   | j  ¡  | j  | j| j ¡ |  ¡  d S r€   )rŽ   rr   r'   ÚpsÚpnÚsetInsertModer    r   r   r   rŸ   �  s   

zRecvLine.initializeScreenc                 C   s(   d  | j¡}|d | j… || jd … fS ©Nó    )ÚjoinrŒ   r�   )r   Úsr   r   r   ÚcurrentLineBuffer‘  s   zRecvLine.currentLineBufferc                 C   ó   d| _ | j tjjg¡ d S )NÚinsert)rQ   rŽ   rP   r   rO   ÚIRMr    r   r   r   r¤   •  ó   zRecvLine.setInsertModec                 C   rª   )NÚtypeover)rQ   rŽ   rR   r   rO   r¬   r    r   r   r   ÚsetTypeoverMode™  r­   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.
        r¦   N)rŽ   r'   r¢   r£   r§   rŒ   r    r   r   r   ÚdrawInputLine�  s   $zRecvLine.drawInputLinec                 C   s,   | j  ¡  | j  ¡  || _|| _|  ¡  d S r€   )rŽ   rm   rH   ÚwidthÚheightr°   )r   r±   r²   r   r   r   ÚterminalSize¤  s
   

zRecvLine.terminalSizec                 C   ó   d S r€   r   rs   r   r   r   ru   ­  ó   z!RecvLine.unhandledControlSequencec                 C   sL   | j  |¡}|d ur|ƒ  d S || jv r|  |d¡ d S | jjd|d� d S )NFz#Received unhandled keyID: {keyID!r})rv   )rž   r   Ú_printableCharsÚcharacterReceivedÚ_logÚwarn)r   rv   Ú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   )rQ   rŒ   r«   r�   rŽ   r'   )r   ÚchÚmoreCharactersComingr   r   r   r·   ¹  s
   
zRecvLine.characterReceivedc                 C   sH   | j t| jƒ| j   }| j |¡ |  j|7  _| j td| ƒ¡ d S )Nó    )ÚTABSTOPÚlenrŒ   rŽ   rC   r�   Úextendr   r?   r   r   r   r“   Á  s   zRecvLine.handle_TABc                 C   s*   | j dkr|  j d8  _ | j ¡  d S d S ©Nr   r   ©r�   rŽ   rD   r    r   r   r   r�   Ç  s   
þzRecvLine.handle_LEFTc                 C   s0   | j t| jƒk r|  j d7  _ | j ¡  d S d S )Nr   )r�   rÁ   rŒ   rŽ   rC   r    r   r   r   r’   Ì  s   þzRecvLine.handle_RIGHTc                 C   s"   | j r| j | j ¡ d| _ d S d S ©Nr   rÄ   r    r   r   r   r›   Ñ  s   
þzRecvLine.handle_HOMEc                 C   s4   t | jƒ| j }|r| j |¡ t | jƒ| _d S d S r€   )rÁ   rŒ   r�   rŽ   rC   )r   Úoffsetr   r   r   r�   Ö  s
   þzRecvLine.handle_ENDc                 C   s>   | j dkr|  j d8  _ | j| j = | j ¡  | j ¡  d S d S rÃ   )r�   rŒ   rŽ   rD   rn   r    r   r   r   r•   Ü  s   


üzRecvLine.handle_BACKSPACEc                 C   s,   | j t| jƒk r| j| j = | j ¡  d S d S r€   )r�   rÁ   rŒ   rŽ   rn   r    r   r   r   r—   ã  s   
þzRecvLine.handle_DELETEc                 C   s0   d  | j¡}g | _d| _| j ¡  |  |¡ d S )Nr¦   r   )r§   rŒ   r�   rŽ   rK   ÚlineReceived©r   rF   r   r   r   r”   è  s
   
zRecvLine.handle_RETURNc                 C   s0   | j dv sJ ‚| j dkr|  ¡  d S |  ¡  d S )N)r®   r«   r®   )rQ   r¤   r¯   r    r   r   r   r™   ï  s   
zRecvLine.handle_INSERTc                 C   r´   r€   r   rÈ   r   r   r   rÇ   ö  rµ   zRecvLine.lineReceivedN)#r+   r,   r-   r.   r±   r²   rÀ   r¢   r£   ÚstringÚ	printableÚencoder¶   r   r¸   rƒ   rŸ   r©   r¤   r¯   r°   r³   ru   r¼   r·   r“   r�   r’   r›   r�   r•   r—   r”   r™   rÇ   r   r   r   r   r†   K  s:    		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 rÅ   )r†   rƒ   ÚhistoryLinesÚhistoryPositionrŽ   rž   ÚupdateÚUP_ARROWÚ	handle_UPÚ
DOWN_ARROWÚhandle_DOWNr    r   r   r   rƒ     s   
ÿzHistoricRecvLine.connectionMadec                 C   s&   t | jƒ}|d | j… || jd … fS r€   )ÚtuplerÍ   rÎ   )r   Úbr   r   r   ÚcurrentHistoryBuffer  s   
z%HistoricRecvLine.currentHistoryBufferc                 C   sB   |rt |d d… ƒD ]}|  |d¡ q
|  |dd … d¡ d S d S )NéÿÿÿÿTF)r   r·   )r   Úbufr½   r   r   r   Ú_deliverBuffer  s
   ýzHistoricRecvLine._deliverBufferc                 C   st   | j r| jt| jƒkr| j d | j ¡¡ | jdkr8|  ¡  | j ¡  |  jd8  _g | _ |  	| j| j ¡ d S d S )Nr¦   r   r   )
rŒ   rÎ   rÁ   rÍ   Úappendr§   r›   rŽ   rh   rÙ   r    r   r   r   rÑ     s   

ùzHistoricRecvLine.handle_UPc                 C   s~   | j t| jƒd k r(|  ¡  | j ¡  |  j d7  _ g | _|  | j| j  ¡ d S |  ¡  | j ¡  t| jƒ| _ g | _d| _d S )Nr   r   )	rÎ   rÁ   rÍ   r›   rŽ   rh   rŒ   rÙ   r�   r    r   r   r   rÓ   $  s   


zHistoricRecvLine.handle_DOWNc                 C   s0   | j r| j d | j ¡¡ t| jƒ| _t | ¡S r¥   )rŒ   rÍ   rÚ   r§   rÁ   rÎ   r†   r”   r    r   r   r   r”   5  s   
zHistoricRecvLine.handle_RETURNN)
r+   r,   r-   r.   rƒ   rÖ   rÙ   rÑ   rÓ   r”   r   r   r   r   rÌ   ú  s    rÌ   )r.   rÉ   Útypingr   Úzope.interfacer   Útwisted.conch.insultsr   r   Útwisted.loggerr   Útwisted.pythonr   Útwisted.python.compatr   r	   r   ÚintÚ__annotations__r
   r}   r0   r   ÚTerminalProtocolr†   rÌ   r   r   r   r   Ú<module>   s$      0