
    h0                     0   S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJr  SS	KJr  SS
KJr   " S S5      r " S S\R*                  5      rSrSrSrSrSrSr " S S\R:                  5      r " S S5      rS r  " S S\5      r!g)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)TokenPrinter)MonkeyPatcherc                   8    \ rS rSrSrSrSrS rS rS r	S r
S	rg
)FileWrapper   z
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   normalc                     Xl         g No)selfr   s     \/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/conch/manhole.py__init__FileWrapper.__init__)   s        c                     g r    r   s    r   flushFileWrapper.flush,   s    r   c                 Z    U R                   R                  UR                  SS5      5        g )Nz

)r   	addOutputreplace)r   datas     r   writeFileWrapper.write/   s    fd34r   c                 D    U R                  SR                  U5      5        g )N )r"   join)r   liness     r   
writelinesFileWrapper.writelines2   s    

2775>"r   r   N)__name__
__module____qualname____firstlineno____doc__	softspacestater   r   r"   r(   __static_attributes__r   r   r   r   r      s'     IE5#r   r   c                   t    \ rS rSrSrSrSS jrS rS rS r	S	\
\   S
\S\SS4S jrS rS rS rSS jrSrg)ManholeInterpreter6   a>  
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   Nc                    [         R                  R                  X5        0 U l        Xl        X0l        U R                  5         [        5       U l        U R                  R                  [        SU R                  5        U R                  R                  [        SU R                  5        U R                  R                  [        S[        U R                  5      5        g )Ndisplayhook
excepthookstdout)codeInteractiveInterpreterr   _pendingDeferredshandlerfilenameresetBufferr
   monkeyPatcheraddPatchsysr6   r7   r   )r   r<   localsr=   s       r   r   ManholeInterpreter.__init__F   s    ##,,T:!# *_##C8H8HI##CtG##C;t||3LMr   c                     / U l         g)z
Reset the input buffer.
N)bufferr   s    r   r>   ManholeInterpreter.resetBufferR   s     r   c                     U R                   R                  U5        SR                  U R                   5      nUR                  S5      nU R	                  X R
                  5      nU(       d  U R                  5         U$ )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)rE   appendr&   decode	runsourcer=   r>   )r   linesourcemores       r   pushManholeInterpreter.pushX   s\    $ 	4 DKK(w'~~fmm4r   c                     U R                      [        R                  R                  " U /UQ70 UD6  S S S 5        g ! , (       d  f       g = fr   )r?   r9   r:   runcode)r   akws      r   rS   ManholeInterpreter.runcoder   s5    ''//?q?B?  s	   %;
A	excTypeexcValueexcTracebackreturnc                    UR                   R                  nUR                  [        R                  :X  a  UR
                  S:X  a  UR                  nOUn[        XU5      nU R                  SR                  U5      5        g)zC
Format exception tracebacks and write them to the output handler.
rS   r%   N)
tb_framef_codeco_filenamer9   __file__co_nametb_nextr   r"   r&   )r   rW   rX   rY   code_obj	tracebackr'   s          r   r7   ManholeInterpreter.excepthookv   sf      ((//4==0X5E5E5R$,,I %I I>

2775>"r   c                 v   XR                   S'   [        U[        R                  5      (       a  [	        US5      (       a  U R                  [        U5      5        g [        U5      U R                  ;   a/  U R                  SU R                  [        U5         S   4-  5        g U R                  nU R                  nX14U[        U5      '   U =R                  S-  sl	        UR                  U R                  U R                  X14X14S9  U R                  SU4-  5        g Ub  U R                  [        U5      5        g g )N_resultz<Deferred #%d>r      )callbackArgserrbackArgs)rB   
isinstancer   Deferredhasattrr"   repridr;   numDeferredsaddCallbacks_cbDisplayDeferred_ebDisplayDeferred)r   objdks       r   r6   ManholeInterpreter.displayhook   s   Cc5>>**sH%%

49%CD222

+t/E/Ebg/Nq/Q.SST**%%X"S'
!!Q&!  ++++"#!"	 !  

+qd23_JJtCy! r   c                 ^    U R                  SX!4-  S5        U R                  [        U5      	 U$ )NzDeferred #%d called back: %rT)r"   r;   ro   )r   rg   rv   rt   s       r   rr   %ManholeInterpreter._cbDisplayDeferred   s/    

1QK?F""2c7+r   c                 z    U R                  SX!R                  5       4-  S5        U R                  [        U5      	 U$ )NzDeferred #%d failed: %rT)r"   getErrorMessager;   ro   )r   failurerv   rt   s       r   rs   %ManholeInterpreter._ebDisplayDeferred   s9    

,3J3J3L/MMtT""2c7+r   c                 :    U R                   R                  X5        g r   )r<   r   )r   r!   isAsyncs      r   r"   ManholeInterpreter.write   s    t-r   )r;   rE   r=   r<   r?   )Nz	<console>r   )r*   r+   r,   r-   r.   rp   r   r>   rP   rS   r   BaseExceptionr   r7   r6   rr   rs   r"   r1   r   r   r   r3   r3   6   sh     L
N4@#m$#  # $	#
 
#&".

.r   r3                     c                   Z    \ rS rSrSrSrSS jrS rS rS r	S r
S	 rS
 rSS jrS rSrg)Manhole   a}  
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                 t    [         R                  R                  U 5        Ub  UR                  5       U l        g g r   )r   HistoricRecvLiner   copy	namespace)r   r   s     r   r   Manhole.__init__   s/    !!**40 &^^-DN !r   c                    [         R                  R                  U 5        [        X R                  5      U l        U R                  U R                  [        '   U R                  U R                  [        '   U R                  U R                  [        '   U R                  U R                  [        '   U R                  U R                  [         '   U R"                  U R                  [$        '   g r   )r   r   connectionMader3   r   interpreter
handle_INTkeyHandlersCTRL_C
handle_EOFCTRL_D	handle_FFCTRL_Lhandle_HOMECTRL_A
handle_ENDCTRL_Ehandle_QUITCTRL_BACKSLASHr   s    r   r   Manhole.connectionMade   s    !!006-dNNC#'?? #'?? #'>> #'#3#3 #'?? +/+;+;(r   c                 d   SU l         / U l        SU l        U R                  R	                  5         U R
                  R                  5         U R
                  R                  S5        U R
                  R                  5         U R
                  R                  U R                  U R                      5        g)zf
Handle ^C as an interrupt keystroke by resetting the current input
variables to their initial state.
r   s   KeyboardInterruptN)	pn
lineBufferlineBufferIndexr   r>   terminalnextLiner"   psr   s    r   r   Manhole.handle_INT   s~    
  $$& 01 DGGDGG,-r   c                 ~    U R                   (       a  U R                  R                  S5        g U R                  5         g )N   )r   r   r"   r   r   s    r   r   Manhole.handle_EOF   s'    ??MM&r   c                     U R                   R                  5         U R                   R                  5         U R                  5         g)zP
Handle a 'form feed' byte - generally used to request a screen
refresh/redraw.
N)r   eraseDisplay
cursorHomedrawInputLiner   s    r   r   Manhole.handle_FF   s0    
 	""$  "r   c                 8    U R                   R                  5         g r   )r   loseConnectionr   s    r   r   Manhole.handle_QUIT   s    $$&r   c                     U R                   R                  nUR                  S5      (       + =(       a    UR                  S5      (       + $ )NrH   s   E)r   	lastWriteendswith)r   ws     r   _needsNewlineManhole._needsNewline   s4    MM##::e$$AQZZ-A)AAr   c                 j   U(       ak  U R                   R                  5         U R                   R                  [        U R                  5      [        U R
                  U R                     5      -   5        U R                   R                  U5        U(       a  U R                  5       (       a  U R                   R                  5         U R                   R                  U R
                  U R                     5        U R                  (       a,  U R                  n/ U l        SU l
        U R                  U5        g g g )Nr   )r   	eraseLinecursorBackwardlenr   r   r   r"   r   r   r   _deliverBuffer)r   r!   r   	oldBuffers       r   r   Manhole.addOutput   s    MM##%MM((T__)=DGGDGGDT@U)UVD!!!##&&(MM 01 OO	"$'($##I.  r   c                    U R                   R                  U5      n[        U5      U l        U R	                  5       (       a  U R
                  R                  5         U R
                  R                  U R                  U R                     5        g r   )	r   rP   boolr   r   r   r   r"   r   )r   rM   rO   s      r   lineReceivedManhole.lineReceived  sa    $$T*t*MM""$DGGDGG,-r   )r   r   r   r   r   r   )r*   r+   r,   r-   r.   r   r   r   r   r   r   r   r   r   r   r1   r   r   r   r   r      s<     I.
<.'B/(.r   r   c                   L    \ rS rSrSrSSSSSSS	S
.rSrS rS rSS jr	S r
Srg)VT102Writeri  z
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)
identifierkeyword	parametervariablestringnumberops   [0mc                     / U l         g r   writtenr   s    r   r   VT102Writer.__init__(  s	    r   c                 >    U R                   R                  US5      nU$ )Nr   )typeToColorget)r   typers      r   colorVT102Writer.color+  s      s+r   Nc                    U(       a  US:w  a|  U R                  U5      nU(       a  U R                  R                  U5        U R                  R                  U5        U(       a&  U R                  R                  U R                  5        g g g g )N   )r   r   rJ   normalColor)r   tokenr   cs       r   r"   VT102Writer.write/  sg    Ue^

4 A##A&LL&##D$4$45  $5r   c                 |    SR                  U R                  5      nUR                  S5      R                  5       S   $ )Nr   rH   )r&   r   strip
splitlines)r   ss     r   	__bytes__VT102Writer.__bytes__8  s0    HHT\\"wwu~((*2..r   r   r   )r*   r+   r,   r-   r.   r   r   r   r   r"   r   r1   r   r   r   r   r     s@     " !K K6/r   r   c                 :   [        U [        5      (       d  U R                  S5      n [        5       n[	        UR
                  5      R                  n[        U 5      n[        R                  " UR                  5       H  nUu  pVpxn	U" XVXxU	5        M     [        U5      $ )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
rI   )
rk   bytesencoder   r	   r"   
printtokenr   tokenizereadline)
rN   r   pr   r   	tokenTyper   startendrM   s
             r   lastColorizedLiner   =  s     fe$$w'AQWW((AA""1::.05-E	)U. / 8Or   c                   $    \ rS rSrSrS rS rSrg)ColoredManholeiT  z4
A REPL which syntax colors input as users type it.
c                     SR                  U R                  R                  5      S-   SR                  U R                  5      -   $ )z~
Return a string containing the currently entered source.

This is only the code which will be considered for execution
next.
rH   r   )r&   r   rE   r   r   s    r   	getSourceColoredManhole.getSourceY  s6     zz$**112U:SXXdoo=VVVr   c                    U R                   S:X  a'  U R                  R                  U R                  U5        O'U/U R                  U R                  U R                  S-   & U =R                  S-  sl        U(       a  g US:X  a  U R                  R                  U5        g U R                  5       n [        U5      nU R                  R                  5         U R                  R                  [        U R                  5      [        U R                  U R                     5      -   S-
  5        U R                  R                  U R                  U R                     U-   5        [        U R                  5      U R                  -
  nU(       a  U R                  R                  U5        g g ! [        R                   a    U R                  R                  U5         g f = f)Ninsertrh       )moder   r   r   r   r"   r   r   r   r   r   r   r   r   
TokenError)r   chmoreCharactersComingrN   coloredLinens         r   characterReceived ColoredManhole.characterReceivedb  sk   99 OO""4#7#7<PRtDOOD0043G3G!3KL! :MM#!	0+F3K MM##%MM((DOO$s477477+;'<<q@
 MM 0; >? DOO$t';';;A,,Q/  "" 	$MM#	$s   /F# #/GGr   N)r*   r+   r,   r-   r.   r   r   r1   r   r   r   r   r   T  s    W&0r   r   )"r.   r9   rA   r   ior   rc   r   typesr   typingr   twisted.conchr   twisted.internetr   twisted.python.htmlizerr	   twisted.python.monkeyr
   r   r:   r3   r   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>r     s   
	  
   &   " " 0 /# #0u.44 u.p 
				Z.h'' Z.z'/ '/T.40W 40r   