U
    
W[8.                     @   s   d 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	 ddl
mZmZ ddlmZ G dd	 d	ZG d
d dejZdZdZdZdZdZdZG dd dejZG dd dZdd ZG dd deZdS )aO  
Line-input oriented interactive interpreter loop.

Provides classes for handling Python source input and arbitrary output
interactively from a Twisted application.  Also included is syntax coloring
code with support for VT102 terminals, control code handling (^C, ^D, ^Q),
and reasonable handling of Deferreds.

@author: Jp Calderone
    N)BytesIO)recvline)defer)	_tokenize_get_async_param)TokenPrinterc                   @   s8   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dS )FileWrapperz
    Minimal write-file-like object.

    Writes are translated into addOutput calls on an object passed to
    __init__.  Newlines are also converted from network to local style.
    r   Znormalc                 C   s
   || _ d S N)o)selfr
    r   7/usr/lib/python3/dist-packages/twisted/conch/manhole.py__init__&   s    zFileWrapper.__init__c                 C   s   d S r	   r   r   r   r   r   flush*   s    zFileWrapper.flushc                 C   s   | j |dd d S )Nz

)r
   	addOutputreplace)r   datar   r   r   write.   s    zFileWrapper.writec                 C   s   |  d| d S )N )r   join)r   linesr   r   r   
writelines2   s    zFileWrapper.writelinesN)
__name__
__module____qualname____doc__Z	softspacestater   r   r   r   r   r   r   r   r      s   r   c                   @   sX   e Zd ZdZdZdddZdd Zd	d
 Zdd Zdd Z	dd Z
dd ZdddZdS )ManholeInterpreteraf  
    Interactive Interpreter with special output and Deferred support.

    Aside from the features provided by L{code.InteractiveInterpreter}, this
    class captures sys.stdout output and redirects it to the appropriate
    location (the Manhole protocol instance).  It also treats Deferreds
    which reach the top-level specially: each is formatted to the user with
    a unique identifier and a new callback and errback added to it, each of
    which will format the unique identifier and the result with which the
    Deferred fires and then pass it on to the next participant in the
    callback chain.
    r   N	<console>c                 C   s,   t j| | i | _|| _|| _|   d S r	   )codeInteractiveInterpreterr   _pendingDeferredshandlerfilenameresetBuffer)r   r$   localsr%   r   r   r   r   F   s
    zManholeInterpreter.__init__c                 C   s
   g | _ dS )z)
        Reset the input buffer.
        N)bufferr   r   r   r   r&   N   s    zManholeInterpreter.resetBufferc                 C   s@   | j | d| j }|d}| || j}|s<|   |S )a  
        Push a line to the interpreter.

        The line should not have a trailing newline; it may have
        internal newlines.  The line is appended to a buffer and the
        interpreter's runsource() method is called with the
        concatenated contents of the buffer as source.  If this
        indicates that the command was executed or invalid, the buffer
        is reset; otherwise, the command is incomplete, and the buffer
        is left as it was after the line was appended.  The return
        value is 1 if more input is required, 0 if the line was dealt
        with in some way (this is the same as runsource()).

        @param line: line of text
        @type line: L{bytes}
        @return: L{bool} from L{code.InteractiveInterpreter.runsource}
           
utf-8)r(   appendr   decodeZ	runsourcer%   r&   )r   linesourcemorer   r   r   pushU   s    
zManholeInterpreter.pushc                 O   sX   t j| j }t _z:t jt| j }t _ztjj| f|| W 5 |t _X W 5 |t _X d S r	   )sysdisplayhookstdoutr   r$   r!   r"   runcode)r   akwZorighookZorigoutr   r   r   r4   p   s    zManholeInterpreter.runcodec                 C   s   || j d< t|tjrt|dr0| t| qt|| jkr^| d| jt| d f  q| j}| j	}||f|t|< |  j	d7  _	|j
| j| j||f||fd | d|f  n|d k	r| t| d S )N_resultz<Deferred #%d>r      )ZcallbackArgsZerrbackArgs)r'   
isinstancer   ZDeferredhasattrr   repridr#   numDeferredsZaddCallbacks_cbDisplayDeferred_ebDisplayDeferred)r   objdkr   r   r   r2   |   s"    

  zManholeInterpreter.displayhookc                 C   s$   |  d||f d | jt|= |S )NzDeferred #%d called back: %rT)r   r#   r=   )r   r8   rC   rA   r   r   r   r?      s    z%ManholeInterpreter._cbDisplayDeferredc                 C   s(   |  d|| f d | jt|= |S )NzDeferred #%d failed: %rT)r   ZgetErrorMessager#   r=   )r   ZfailurerC   rA   r   r   r   r@      s    z%ManholeInterpreter._ebDisplayDeferredc                 K   s   t |f|}| j|| d S r	   )r   r$   r   )r   r   isAsynckwargsr   r   r   r      s    zManholeInterpreter.write)Nr    )N)r   r   r   r   r>   r   r&   r0   r4   r2   r?   r@   r   r   r   r   r   r   7   s   
r                     c                   @   s`   e Zd ZdZdZdddZdd Zdd Zd	d
 Zdd Z	dd Z
dd ZdddZdd ZdS )Manholea  
    Mediator between a fancy line source and an interactive interpreter.

    This accepts lines from its transport and passes them on to a
    L{ManholeInterpreter}.  Control commands (^C, ^D, ^\) are also handled
    with something approximating their normal terminal-mode behavior.  It
    can optionally be constructed with a dict which will be used as the
    local namespace for any code executed.
    Nc                 C   s"   t j|  |d k	r| | _d S r	   )r   HistoricRecvLiner   copy	namespace)r   rO   r   r   r   r      s    zManhole.__init__c                 C   sf   t j|  t| | j| _| j| jt< | j	| jt
< | j| jt< | j| jt< | j| jt< | j| jt< d S r	   )r   rM   connectionMader   rO   interpreter
handle_INTZkeyHandlersCTRL_C
handle_EOFCTRL_D	handle_FFCTRL_LZhandle_HOMECTRL_AZ
handle_ENDCTRL_Ehandle_QUITCTRL_BACKSLASHr   r   r   r   rP      s    zManhole.connectionMadec                 C   sT   d| _ g | _d| _| j  | j  | jd | j  | j| j| j   dS )z~
        Handle ^C as an interrupt keystroke by resetting the current input
        variables to their initial state.
        r   s   KeyboardInterruptN)	pn
lineBufferlineBufferIndexrQ   r&   terminalnextLiner   psr   r   r   r   rR      s    


zManhole.handle_INTc                 C   s    | j r| jd n|   d S )N   )r]   r_   r   rZ   r   r   r   r   rT      s    zManhole.handle_EOFc                 C   s    | j   | j   |   dS )zh
        Handle a 'form feed' byte - generally used to request a screen
        refresh/redraw.
        N)r_   ZeraseDisplayZ
cursorHomeZdrawInputLiner   r   r   r   rV      s    

zManhole.handle_FFc                 C   s   | j   d S r	   )r_   ZloseConnectionr   r   r   r   rZ      s    zManhole.handle_QUITc                 C   s    | j j}|d o|d S )Nr)   s   E)r_   Z	lastWriteendswith)r   wr   r   r   _needsNewline   s    zManhole._needsNewlinec                 K   s   t |f|}|r<| j  | jt| jt| j| j   | j| |r| 	 r^| j
  | j| j| j  | jr| j}g | _d| _| | d S )Nr   )r   r_   	eraseLinecursorBackwardlenr]   ra   r\   r   re   r`   r^   Z_deliverBuffer)r   r   rD   rE   Z	oldBufferr   r   r   r      s     

zManhole.addOutputc                 C   s@   | j |}t|| _|  r(| j  | j| j| j  d S r	   )	rQ   r0   boolr\   re   r_   r`   r   ra   )r   r-   r/   r   r   r   lineReceived
  s
    

zManhole.lineReceived)N)N)r   r   r   r   rO   r   rP   rR   rT   rV   rZ   re   r   rj   r   r   r   r   rL      s   



rL   c                   @   sV   e Zd ZdZdddddddd	Zd
Zdd Zdd ZdddZdd Z	e
ekrRe	ZdS )VT102Writerz
    Colorizer for Python tokens.

    A series of tokens are written to instances of this object.  Each is
    colored in a particular way.  The final line of the result of this is
    generally added to the output.
    s   [31ms   [32ms   [33ms   [1;33ms   [35ms   [36ms   [37m)Z
identifierkeywordZ	parameterZvariablestringZnumberops   [0mc                 C   s
   g | _ d S r	   )writtenr   r   r   r   r   '  s    zVT102Writer.__init__c                 C   s   | j |d}|S )N    )typeToColorget)r   typerr   r   r   color+  s    zVT102Writer.colorNc                 C   sH   |rD|dkrD|  |}|r&| j| | j| |rD| j| j d S )N   )ru   ro   r+   normalColor)r   tokenrs   cr   r   r   r   0  s    
zVT102Writer.writec                 C   s   d | j}|d d S )Nrp   r)   )r   ro   strip
splitlines)r   sr   r   r   	__bytes__:  s    zVT102Writer.__bytes__)N)r   r   r   r   rq   rw   r   ru   r   r~   bytesstr__str__r   r   r   r   rk     s    		

rk   c           
      C   sd   t | ts| d} t }t|jj}t| }t|j	D ]"}|\}}}}}	||||||	 q8t|S )z
    Tokenize and colorize the given Python source.

    Returns a VT102-format colorized version of the last line of C{source}.

    @param source: Python source code
    @type source: L{str} or L{bytes}
    @return: L{bytes} of colorized source
    r*   )
r:   r   encoderk   r   r   Z
printtokenr   r   readline)
r.   rd   pr}   rx   Z	tokenTyperm   startendr-   r   r   r   lastColorizedLineD  s    


r   c                   @   s    e Zd ZdZdd Zdd ZdS )ColoredManholez<
    A REPL which syntax colors input as users type it.
    c                 C   s   d | jjd d | j S )z
        Return a string containing the currently entered source.

        This is only the code which will be considered for execution
        next.
        r)   rp   )r   rQ   r(   r]   r   r   r   r   	getSourcea  s
    
zColoredManhole.getSourcec                 C   s  | j dkr| j| j| n|g| j| j| jd < |  jd7  _|rJd S |dkrb| j| d S |  }zt|}W n" tj	k
r   | j| Y nlX | j
  | jt| jt| j| j  d  | j| j| j |  t| j| j }|r| j| d S )Ninsertr9       )moder]   r   r^   r_   r   r   r   tokenize
TokenErrorrf   rg   rh   ra   r\   )r   ZchZmoreCharactersComingr.   ZcoloredLinenr   r   r   characterReceivedm  s(    

&z ColoredManhole.characterReceivedN)r   r   r   r   r   r   r   r   r   r   r   \  s   r   )r   r!   r1   r   ior   Ztwisted.conchr   Ztwisted.internetr   Ztwisted.python.compatr   r   Ztwisted.python.htmlizerr   r   r"   r   rS   rU   r[   rW   rX   rY   rM   rL   rk   r   r   r   r   r   r   <module>   s$   kh1