o bD.@sdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z Gdd d ZGd d d ejZd Zd ZdZdZdZdZGdddejZGdddZddZGdddeZdS)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)_get_async_param) TokenPrinterc@s8eZdZdZdZdZddZddZdd Zd 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. rnormalcCs ||_dSN)o)selfr r 7/usr/lib/python3/dist-packages/twisted/conch/manhole.py__init__& zFileWrapper.__init__cCsdSr r r r r r flush)szFileWrapper.flushcCs|j|dddS)Nz  )r addOutputreplace)r datar r r write,szFileWrapper.writecCs|d|dS)N)rjoin)r linesr r r writelines/szFileWrapper.writelinesN) __name__ __module__ __qualname____doc__ softspacestaterrrrr r r r rs rc@sXeZdZdZdZdddZddZd d Zd d Zd dZ ddZ ddZ dddZ dS)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. rN cCs,tj||i|_||_||_|dSr )codeInteractiveInterpreterr_pendingDeferredshandlerfilename resetBuffer)r r&localsr'r r r rCs  zManholeInterpreter.__init__cCs g|_dS)z) Reset the input buffer. N)bufferrr r r r(Js zManholeInterpreter.resetBuffercCs@|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*appendrdecode runsourcer'r()r linesourcemorer r r pushPs   zManholeInterpreter.pushcOshtj|j}t_z'tjt|j}t_ztjj|g|Ri|W|t_n|t_wW|t_dS|t_wr )sys displayhookstdoutrr&r#r$runcode)r akworighookorigoutr r r r7jszManholeInterpreter.runcodecCs||jd<t|tjr_t|dr|t|dSt||jvr1|d|jt|dfdS|j}|j }||f|t|<|j d7_ |j |j |j ||f||fd|d|fdS|durl|t|dSdS)N_resultzr) callbackArgs errbackArgs) r) isinstancerDeferredhasattrrrepridr% numDeferreds addCallbacks_cbDisplayDeferred_ebDisplayDeferred)r objdkr r r r5us(   "zManholeInterpreter.displayhookcCs$|d||fd|jt|=|S)NzDeferred #%d called back: %rT)rr%rE)r r=rLrJr r r rHs z%ManholeInterpreter._cbDisplayDeferredcCs(|d||fd|jt|=|S)NzDeferred #%d failed: %rT)rgetErrorMessager%rE)r failurerLrJr r r rIs z%ManholeInterpreter._ebDisplayDeferredcKs"t|fi|}|j||dSr )rr&r)r risAsynckwargsr r r rszManholeInterpreter.write)Nr"r ) rrrrrFrr(r3r7r5rHrIrr r r r r!3s  r! c@s`eZdZdZdZdddZddZddZd d Zd d Z d dZ ddZ dddZ ddZ dS)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. NcCs&tj||dur||_dSdSr )rHistoricRecvLinercopy namespace)r rZr r r rs zManhole.__init__cCsftj|t||j|_|j|jt<|j |jt <|j |jt <|j |jt<|j|jt<|j|jt<dSr )rrXconnectionMader!rZ interpreter handle_INT keyHandlersCTRL_C handle_EOFCTRL_D handle_FFCTRL_L handle_HOMECTRL_A handle_ENDCTRL_E handle_QUITCTRL_BACKSLASHrr r r r[s      zManhole.connectionMadecCsTd|_g|_d|_|j|j|jd|j|j|j|jdS)z~ Handle ^C as an interrupt keystroke by resetting the current input variables to their initial state. rsKeyboardInterruptN) pn lineBufferlineBufferIndexr\r(terminalnextLinerpsrr r r r]s    zManhole.handle_INTcCs"|jr |jddS|dS)N)rkrmrrhrr r r r`s zManhole.handle_EOFcCs |j|j|dS)zh Handle a 'form feed' byte - generally used to request a screen refresh/redraw. N)rm eraseDisplay cursorHome drawInputLinerr r r rbs   zManhole.handle_FFcCs|jdSr )rmloseConnectionrr r r rhszManhole.handle_QUITcCs |jj}|d o|d S)Nr+sE)rm lastWriteendswith)r wr r r _needsNewlineszManhole._needsNewlinecKst|fi|}|r |j|jt|jt|j|j|j||rN| r1|j |j|j|j|jrP|j}g|_d|_ | |dSdSdS)Nr) rrm eraseLinecursorBackwardlenrkrorjrrxrnrl_deliverBuffer)r rrOrP oldBufferr r r rs  "  zManhole.addOutputcCs@|j|}t||_|r|j|j|j|jdSr ) r\r3boolrjrxrmrnrro)r r0r2r r r lineReceiveds   zManhole.lineReceivedr )rrrrrZrr[r]r`rbrhrxrrr r r r rWs     rWc@sZeZdZdZdddddddd Zd Zd d Zd dZdddZddZ e e kr+e Z dSdS) 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. sssssss) identifierkeyword parametervariablestringnumberopscCs g|_dSr )writtenrr r r rrzVT102Writer.__init__cCs|j|d}|S)N) typeToColorget)r typerr r r colorszVT102Writer.colorNcCsT|r$|dkr&||}|r|j||j||r(|j|jdSdSdSdS)N )rrr- normalColor)r tokenrcr r r rs    zVT102Writer.writecCsd|j}|ddS)Nrr+)rrstrip splitlines)r sr r r __bytes__&s zVT102Writer.__bytes__r ) rrrrrrrrrrbytesstr__str__r r r r rs$    rc Csft|ts |d}t}t|jj}t|}t|j D]}|\}}}}} |||||| qt|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,) rArencoderrr printtokenrtokenizereadline) r1rwprr tokenTyperstartendr0r r r lastColorizedLine/s  rc@s eZdZdZddZddZdS)ColoredManholez< A REPL which syntax colors input as users type it. cCsd|jjdd|jS)z Return a string containing the currently entered source. This is only the code which will be considered for execution next. r+r)rr\r*rkrr r r getSourceKszColoredManhole.getSourcecCs |jdkr|j|j|n |g|j|j|jd<|jd7_|r%dS|dkr1|j|dS|}zt|}Wntj yL|j|YdSw|j |j t |jt |j |jd|j|j |j|t |j|j}|r|j |dSdS)Ninsertr> )moderkrrlrmrrrr TokenErrorryrzr{rorj)r chmoreCharactersComingr1 coloredLinenr r r characterReceivedTs0    z ColoredManhole.characterReceivedN)rrrrrrr r r r rFs r)rr#r4rior twisted.conchrtwisted.internetrtwisted.python.compatrtwisted.python.htmlizerrrr$r!r_rarircrergrXrWrrrr r r r s*      h^.