ϪfM0<dZddlZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z ddlmZdd lmZdd lmZdd lmZGd d ZGddej.ZdZdZdZdZdZdZGdde j>Z GddZ!dZ"Gdde Z#y)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)format_exception) TracebackType)Type)recvline)defer)_get_async_param) TokenPrinter) MonkeyPatcherc0eZdZdZdZdZdZdZdZdZ y) 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. rnormalc||_yN)o)selfrs 7/usr/lib/python3/dist-packages/twisted/conch/manhole.py__init__zFileWrapper.__init__*s cyrrs rflushzFileWrapper.flush-s rcZ|jj|jddy)Nz  )r addOutputreplace)rdatas rwritezFileWrapper.write0s fd34rcD|jdj|y)N)rjoin)rliness r writelineszFileWrapper.writelines3s 2775>"rN) __name__ __module__ __qualname____doc__ softspacestaterrrr$rrrr r s'I E 5#rr cfeZdZdZdZddZdZdZdZde e d e d e d dfd Z d Z dZdZddZy)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. rNctjj||i|_||_||_|j t|_|jjtd|j|jjtd|j|jjtdt|jy)N displayhook excepthookstdout)codeInteractiveInterpreterr_pendingDeferredshandlerfilename resetBufferr monkeyPatcheraddPatchsysr.r/r )rr4localsr5s rrzManholeInterpreter.__init__Gs ##,,T6:!#    *_ ##C8H8HI ##CtG ##C;t||3LMrcg|_y)z) Reset the input buffer. N)bufferrs rr6zManholeInterpreter.resetBufferSs  rc|jj|dj|j}|jd}|j ||j }|s|j |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"decode runsourcer5r6)rlinesourcemores rpushzManholeInterpreter.pushYs^$ 4 DKK(w'~~fdmm4     rc|j5tjj|g|i|dddy#1swYyxYwr)r7r1r2runcode)rakws rrHzManholeInterpreter.runcodessB    @  ' ' / / ?q ?B ? @ @ @s $:AexcTypeexcValue excTracebackreturncrt|||j}|jdj|y)zS Format exception tracebacks and write them to the output handler. r!N)rtb_nextrr")rrKrLrMr#s rr/zManholeInterpreter.excepthookws,!(L4H4HI 2775>"rcj||jd<t|tjrt |dr|j t |yt||jvr/|j d|jt|dfzy|j}|j}||f|t|<|xjdz c_ |j|j|j||f||f|j d|fzy||j t |yy)N_resultzr) callbackArgs errbackArgs) r: isinstancerDeferredhasattrrrepridr3 numDeferreds addCallbacks_cbDisplayDeferred_ebDisplayDeferred)robjdks rr.zManholeInterpreter.displayhooks C c5>> *sH% 49%CD222 +t/E/Ebg/Nq/Q.SST**%%X"S' !!Q&!  ++++"#S!"C !  +qd23 _ JJtCy !rc`|jd||fzd|jt|=|S)NzDeferred #%d called back: %rT)rr3r[)rrSrbr`s rr^z%ManholeInterpreter._cbDisplayDeferreds1 1QK?F  " "2c7 + rc||jd||jfzd|jt|=|S)NzDeferred #%d failed: %rT)rgetErrorMessager3r[)rfailurerbr`s rr_z%ManholeInterpreter._ebDisplayDeferreds; ,73J3J3L/MMtT  " "2c7 +rc Tt|fi|}|jj||yr)r r4r)rrisAsynckwargss rrzManholeInterpreter.writes%"75f5 tW-r)Nz r)r%r&r'r(r\rr6rFrHr BaseExceptionrr/r.r^r_rrrrr,r,7sh L N 4@ #m$ #  #$ #  #".  .rr, cNeZdZdZdZd dZdZdZdZdZ dZ d Z d d Z d Z y) 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. Ncrtjj|||j|_yyr)rHistoricRecvLinercopy namespace)rrvs rrzManhole.__init__s/!!**40  &^^-DN !rctjj|t||j|_|j |jt<|j|jt<|j|jt<|j|jt<|j|jt <|j"|jt$<yr)rrtconnectionMader,rv interpreter handle_INT keyHandlersCTRL_C handle_EOFCTRL_D handle_FFCTRL_L handle_HOMECTRL_A handle_ENDCTRL_E handle_QUITCTRL_BACKSLASHrs rrxzManhole.connectionMades!!006-dDNNC#'?? #'?? #'>> #'#3#3 #'?? +/+;+;(rcdd|_g|_d|_|jj |j j |j jd|j j |j j|j|jy)z~ Handle ^C as an interrupt keystroke by resetting the current input variables to their initial state. rsKeyboardInterruptN) pn lineBufferlineBufferIndexryr6terminalnextLinerpsrs rrzzManhole.handle_INTs~   $$&   01   DGGDGG,-rct|jr|jjdy|jy)N)rrrrrs rr}zManhole.handle_EOFs' ?? MM   &    rc|jj|jj|jy)zh Handle a 'form feed' byte - generally used to request a screen refresh/redraw. N)r eraseDisplay cursorHome drawInputLiners rrzManhole.handle_FFs0 ""$   " rc8|jjyr)rloseConnectionrs rrzManhole.handle_QUITs $$&rcz|jj}|jd xr|jd S)Nr>sE)r lastWriteendswith)rws r _needsNewlinezManhole._needsNewlines4 MM # #::e$$AQZZ-A)AArc Zt|fi|}|rk|jj|jjt |j t |j |jz|jj||r|jr|jj|jj|j |j|j r,|j }g|_d|_ |j|yyy)Nr) r r eraseLinecursorBackwardlenrrrrrrr_deliverBuffer)rrrhri oldBuffers rrzManhole.addOutputs"75f5  MM # # % MM ( (T__)=DGGDGGDT@U)U V D! !!# &&( MM   0 1 OO "$'($##I.  rc|jj|}t||_|j r|j j |j j|j|jyr) ryrFboolrrrrrr)rrCrEs r lineReceivedzManhole.lineReceiveds^$$T*t*     MM " " $ DGGDGG,-rr)r%r&r'r(rvrrxrzr}rrrrrrrrrrrrs<I. < . 'B/*.rrrcReZdZdZdddddddd Zd Zd Zd ZddZdZ e e k(re Z y y ) 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 parametervariablestringnumberopscg|_yr)writtenrs rrzVT102Writer.__init__$s  rc>|jj|d}|S)Nr) typeToColorget)rtypers rcolorzVT102Writer.color's    s +rNc|rx|dk7rr|j|}|r|jj||jj||r&|jj|jyyyy)N )rrr@ normalColor)rtokenrcs rrzVT102Writer.write+sg Ue^ 4 A ##A& LL   & ##D$4$45 $5rc|dj|j}|jdjdS)Nrr>)r"rstrip splitlines)rss r __bytes__zVT102Writer.__bytes__4s0 HHT\\ "wwu~((*2..rr) r%r&r'r(rrrrrrbytesstr__str__rrrrrsS" !KK6/ |rrc2t|ts|jd}t}t |j j }t|}tj|jD]}|\}}}}} |||||| t|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?) rWrencoderr r printtokenrtokenizereadline) rDrprr tokenTyperstartendrCs rlastColorizedLiner=s fe $w' AQWW((AA""1::./05-FE3 )VUC./ 8OrceZdZdZdZdZy)ColoredManholez< A REPL which syntax colors input as users type it. cdj|jjdzdj|jzS)z Return a string containing the currently entered source. This is only the code which will be considered for execution next. r>r)r"ryr<rrs r getSourcezColoredManhole.getSourceYs6zz$**112U:SXXdoo=VVVrc|jdk(r'|jj|j|n'|g|j|j|jdz|xjdz c_|ry|dk(r|jj |y|j } t|}|jj|jjt|jt|j|jzdz |jj |j|j|zt|j|jz }|r|jj|yy#tj$r|jj |YywxYw)NinsertrT )moderrrrrrrrrrrrr TokenError)rchmoreCharactersComingrD coloredLinens rcharacterReceivedz ColoredManhole.characterReceivedbsk 99 OO " "4#7#7 ?DOO$t';';;A ,,Q/"" $ MM   # $s( F.GGN)r%r&r'r(rrrrrrrTsW&0rr)$r(r1r9rior tracebackrtypesrtypingr twisted.conchrtwisted.internetrtwisted.python.compatr twisted.python.htmlizerr twisted.python.monkeyr r r2r,r|r~rrrrrtrrrrrrrrrs  &""20/##0o.44o.d     [.h''[.|++\.40W40r