
    h{                     &   S r SSKrSSKrSSKJr  SSKJrJrJr  SSK	J
r
  SSKJrJrJr  SSKJr  S rS	\l         Sr " S
 S\5      r " S S\5      r " S S\R.                  5      r " S S\R.                  5      r " S S5      r " S S\R.                  \5      r " S S\5      r " S S5      r " S S\R.                  \5      r " S S\5      r  " S S\5      r! " S  S!\5      r" " S" S#5      r#\
" \RH                  5       " S$ S%5      5       r%g)&zN
Basic protocols, such as line-oriented, netstring, and int prefixed strings.
    N)BytesIO)calcsizepackunpack)implementer)defer
interfacesprotocol)logc                 n    SR                  [        [        U 5      5      R                  S5      SU S/5      $ )N    ascii   :   ,)joinstrlenencode)datas    ^/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/protocols/basic.py_formatNetstringr      s-    88ST^**73T4FGGr   z_
Convert some C{bytes} into netstring format.

@param data: C{bytes} that will be reformatted.
c                       \ rS rSrSrSrg)NetstringParseError)   z5
The incoming data is not in valid Netstring format.
 N__name__
__module____qualname____firstlineno____doc____static_attributes__r   r   r   r   r   )       r   r   c                       \ rS rSrSrSrg)IncompleteNetstring/   z*
Not enough data to complete a netstring.
r   Nr   r   r   r   r%   r%   /   r#   r   r%   c                       \ rS rSrSrSr\R                  " S5      r\R                  " S5      r	Sr
SrSrS	r\" S
5      u  rrS rS rS rS rS rS rS rS rS rS rS rS rS rS rS rS r S r!S r"Sr#g)NetstringReceiver5   a	  
A protocol that sends and receives netstrings.

See U{http://cr.yp.to/proto/netstrings.txt} for the specification of
netstrings. Every netstring starts with digits that specify the length
of the data. This length specification is separated from the data by
a colon. The data is terminated with a comma.

Override L{stringReceived} to handle received netstrings. This
method is called with the netstring payload as a single argument
whenever a complete netstring is received.

Security features:
    1. Messages are limited in size, useful if you don't want
       someone sending you a 500MB netstring (change C{self.MAX_LENGTH}
       to the maximum length you wish to accept).
    2. The connection is lost if an illegal message is received.

@ivar MAX_LENGTH: Defines the maximum length of netstrings that can be
    received.
@type MAX_LENGTH: C{int}

@ivar _LENGTH: A pattern describing all strings that contain a netstring
    length specification. Examples for length specifications are C{b'0:'},
    C{b'12:'}, and C{b'179:'}. C{b'007:'} is not a valid length
    specification, since leading zeros are not allowed.
@type _LENGTH: C{re.Match}

@ivar _LENGTH_PREFIX: A pattern describing all strings that contain
    the first part of a netstring length specification (without the
    trailing comma). Examples are '0', '12', and '179'. '007' does not
    start a netstring length specification, since leading zeros are
    not allowed.
@type _LENGTH_PREFIX: C{re.Match}

@ivar _PARSING_LENGTH: Indicates that the C{NetstringReceiver} is in
    the state of parsing the length portion of a netstring.
@type _PARSING_LENGTH: C{int}

@ivar _PARSING_PAYLOAD: Indicates that the C{NetstringReceiver} is in
    the state of parsing the payload portion (data and trailing comma)
    of a netstring.
@type _PARSING_PAYLOAD: C{int}

@ivar brokenPeer: Indicates if the connection is still functional
@type brokenPeer: C{int}

@ivar _state: Indicates if the protocol is consuming the length portion
    (C{PARSING_LENGTH}) or the payload (C{PARSING_PAYLOAD}) of a netstring
@type _state: C{int}

@ivar _remainingData: Holds the chunk of data that has not yet been consumed
@type _remainingData: C{string}

@ivar _payload: Holds the payload portion of a netstring including the
    trailing comma
@type _payload: C{BytesIO}

@ivar _expectedPayloadSize: Holds the payload size plus one for the trailing
    comma.
@type _expectedPayloadSize: C{int}
韆 s   (0|[1-9]\d*)(:)s   (0|[1-9]\d*)$zBThe received netstring does not start with a length specification.zpThe length specification of the received netstring cannot be represented in Python - it causes an OverflowError!zQThe received netstring is longer than the maximum %s specified by self.MAX_LENGTHz4The received netstring is not terminated by a comma.   c                     [         R                  R                  X5        SU l        SU l        [        5       U l        U R                  U l        SU l	        SU l
        g)z
Initializes the protocol.
r   r   N)r
   ProtocolmakeConnection_remainingData_currentPayloadSizer   _payload_PARSING_LENGTH_state_expectedPayloadSize
brokenPeer)self	transports     r   r.    NetstringReceiver.makeConnection   sM     	((9!#$ 	**$%!r   c                 L    U R                   R                  [        U5      5        g)z
Sends a netstring.

Wraps up C{string} by adding length information and a
trailing comma; writes the result to the transport.

@param string: The string to send.  The necessary framing (length
    prefix, etc) will be added.
@type string: C{bytes}
N)r7   writer   r6   strings     r   
sendStringNetstringReceiver.sendString   s     	-f56r   c                     U =R                   U-  sl         U R                   (       a%   U R                  5         U R                   (       a  M$  gg! [         a     g[         a    U R	                  5          gf = f)a  
Receives some characters of a netstring.

Whenever a complete netstring is received, this method extracts
its payload and calls L{stringReceived} to process it.

@param data: A chunk of data representing a (possibly partial)
    netstring
@type data: C{bytes}
N)r/   _consumeDatar%   r   _handleParseErrorr6   r   s     r   dataReceivedNetstringReceiver.dataReceived   sh     	t#!!!!# !!! ' & &&(s   A 
A5A54A5c                     [        5       e)a&  
Override this for notification when each complete string is received.

@param string: The complete string which was received with all
    framing (length prefix, etc) removed.
@type string: C{bytes}

@raise NotImplementedError: because the method has to be implemented
    by the child class.
NotImplementedErrorr;   s     r   stringReceived NetstringReceiver.stringReceived   s     "##r   c                 p    [         R                  " [         R                  " U R                  5      5      S-   $ )z
Calculate and return the string size of C{self.MAX_LENGTH}.

@return: The size of the string representation for C{self.MAX_LENGTH}
@rtype: C{float}
   )mathceillog10
MAX_LENGTHr6   s    r   _maxLengthSize NetstringReceiver._maxLengthSize   s%     yyDOO4599r   c                     U R                   U R                  :X  a   U R                  5         U R                  5         U R                   U R                  :X  a  U R                  5         gg)a   
Consumes the content of C{self._remainingData}.

@raise IncompleteNetstring: if C{self._remainingData} does not
    contain enough data to complete the current netstring.
@raise NetstringParseError: if the received data do not
    form a valid netstring.
N)r3   r2   _consumeLength_prepareForPayloadConsumption_PARSING_PAYLOAD_consumePayloadrP   s    r   r@   NetstringReceiver._consumeData   sQ     ;;$...!..0;;$///  " 0r   c                     U R                   R                  U R                  5      nU(       d  U R                  5         [	        5       eU R                  U5        g)a  
Consumes the length portion of C{self._remainingData}.

@raise IncompleteNetstring: if C{self._remainingData} contains
    a partial length specification (digits without trailing
    comma).
@raise NetstringParseError: if the received data do not form a valid
    netstring.
N)_LENGTHmatchr/    _checkPartialLengthSpecificationr%   _processLength)r6   lengthMatchs     r   rT    NetstringReceiver._consumeLength   sD     ll(()<)<=113%''K(r   c                     U R                   R                  U R                  5      nU(       d  [        U R                  5      eUR                  S5      nU R                  U5        g)a  
Makes sure that the received data represents a valid number.

Checks if C{self._remainingData} represents a number smaller or
equal to C{self.MAX_LENGTH}.

@raise NetstringParseError: if C{self._remainingData} is no
    number or is too big (checked by L{_extractLength}).
rK   N)_LENGTH_PREFIXr[   r/   r   _MISSING_LENGTHgroup_extractLength)r6   partialLengthMatchlengthSpecifications      r   r\   2NetstringReceiver._checkPartialLengthSpecification   sU     "0066t7J7JK!%d&:&:;;066q9/0r   c                     UR                  S5      nUR                  S5      nU R                  SU nU R                  U5      S-   U l        U R                  US U l        g)a  
Processes the length definition of a netstring.

Extracts and stores in C{self._expectedPayloadSize} the number
representing the netstring size.  Removes the prefix
representing the length specification from
C{self._remainingData}.

@raise NetstringParseError: if the received netstring does not
    start with a number or the number is bigger than
    C{self.MAX_LENGTH}.
@param lengthMatch: A regular expression match object matching
    a netstring length specification
@type lengthMatch: C{re.Match}
rK   r+   N)endr/   rd   r4   )r6   r^   endOfNumberstartOfDatalengthStrings        r   r]    NetstringReceiver._processLength   s_      "ooa(!ooa(**<K8$($7$7$E$I!"11+,?r   c                     U R                  U5        [        U5      nX R                  :  a#  [        U R                  U R                  4-  5      eU$ )a1  
Attempts to extract the length information of a netstring.

@raise NetstringParseError: if the number is bigger than
    C{self.MAX_LENGTH}.
@param lengthAsString: A chunk of data starting with a length
    specification
@type lengthAsString: C{bytes}
@return: The length of the netstring
@rtype: C{int}
)_checkStringSizeintrO   r   	_TOO_LONG)r6   lengthAsStringlengths      r   rd    NetstringReceiver._extractLength  sF     	n-^$OO#%dnn7I&IJJr   c                     [        U5      U R                  5       :  a#  [        U R                  U R                  4-  5      eg)a  
Checks the sanity of lengthAsString.

Checks if the size of the length specification exceeds the
size of the string representing self.MAX_LENGTH. If this is
not the case, the number represented by lengthAsString is
certainly bigger than self.MAX_LENGTH, and a
NetstringParseError can be raised.

This method should make sure that netstrings with extremely
long length specifications are refused before even attempting
to convert them to an integer (which might trigger a
MemoryError).
N)r   rQ   r   rq   rO   )r6   rr   s     r   ro   "NetstringReceiver._checkStringSize)  s:     ~!4!4!66%dnn7I&IJJ 7r   c                     U R                   U l        SU l        U R                  R	                  S5        U R                  R                  5         g)zG
Sets up variables necessary for consuming the payload of a netstring.
r   N)rV   r3   r0   r1   seektruncaterP   s    r   rU   /NetstringReceiver._prepareForPayloadConsumption;  s;     ++#$ 1 r   c                     U R                  5         U R                  U R                  :  a
  [        5       eU R	                  5         U R
                  U l        U R                  5         g)ar  
Consumes the payload portion of C{self._remainingData}.

If the payload is complete, checks for the trailing comma and
processes the payload. If not, raises an L{IncompleteNetstring}
exception.

@raise IncompleteNetstring: if the payload received so far
    contains fewer characters than expected.
@raise NetstringParseError: if the payload does not end with a
comma.
N)_extractPayloadr0   r4   r%   _checkForTrailingCommar2   r3   _processPayloadrP   s    r   rW   !NetstringReceiver._consumePayloadD  sR     	##d&?&??%''##%**r   c                    U R                  5       (       ag  U R                  U R                  -
  nU R                  R	                  U R
                  SU 5        U R
                  US U l        U R                  U l        gU R                  R	                  U R
                  5        U =R                  [        U R
                  5      -  sl        SU l        g)aG  
Extracts payload information from C{self._remainingData}.

Splits C{self._remainingData} at the end of the netstring.  The
first part becomes C{self._payload}, the second part is stored
in C{self._remainingData}.

If the netstring is not yet complete, the whole content of
C{self._remainingData} is moved to C{self._payload}.
Nr   )_payloadCompleter4   r0   r1   r:   r/   r   )r6   remainingPayloadSizes     r   r|   !NetstringReceiver._extractPayloadX  s       ""#'#<#<t?W?W#W MM 3 34I5I JK"&"5"56J6K"LD'+'@'@D$MM 3 34$$D,?,?(@@$"%Dr   c                 `    [        U R                  5      U R                  -   U R                  :  $ )z
Checks if enough data have been received to complete the netstring.

@return: C{True} iff the received data contain at least as many
    characters as specified in the length section of the
    netstring
@rtype: C{bool}
)r   r/   r0   r4   rP   s    r   r   "NetstringReceiver._payloadCompletem  s0     ##$t'?'??(()	
r   c                 \    U R                  U R                  R                  5       SS 5        g)z
Processes the actual payload with L{stringReceived}.

Strips C{self._payload} of the trailing comma and calls
L{stringReceived} with the result.
N)rH   r1   getvaluerP   s    r   r~   !NetstringReceiver._processPayload{  s&     	DMM224Sb9:r   c                 p    U R                   R                  5       SS S:w  a  [        U R                  5      eg)z
Checks if the netstring has a trailing comma at the expected position.

@raise NetstringParseError: if the last payload character is
    anything but a comma.
r   Nr   )r1   r   r   _MISSING_COMMArP   s    r   r}   (NetstringReceiver._checkForTrailingComma  s6     ==!!#BC(D0%d&9&9:: 1r   c                 F    U R                   R                  5         SU l        g)zA
Terminates the connection and sets the flag C{self.brokenPeer}.
rK   N)r7   loseConnectionr5   rP   s    r   rA   #NetstringReceiver._handleParseError  s     	%%'r   )r0   r4   r1   r/   r3   r5   N)$r   r   r   r    r!   rO   recompilerZ   ra   rb   	_OVERFLOWrq   r   ranger2   rV   r.   r=   rC   rH   rQ   r@   rT   r\   r]   rd   ro   rU   rW   r|   r   r~   r}   rA   r"   r   r   r   r(   r(   5   s    =~ Jjj,-GZZ 12N 	P 	 	'  LN ).a%O%
7*$:#) 1 @.$K$!(&*
;;r   r(   c                   <    \ rS rSrSrSrSrSrS rS r	S r
S	 rS
rg)LineOnlyReceiveri  a  
A protocol that receives only lines.

This is purely a speed optimisation over LineReceiver, for the
cases that raw mode is known to be unnecessary.

@cvar delimiter: The line-ending delimiter to use. By default this is
                 C{b'\r\n'}.
@cvar MAX_LENGTH: The maximum length of a line to allow (If a
                  sent line is longer than this, the connection is dropped).
                  Default is 16384.
r      
 @  c                    U R                   U-   R                  U R                  5      nUR                  S5      U l         U H]  nU R                  R
                  (       a    g[        U5      U R                  :  a  U R                  U5      s  $ U R                  U5        M_     [        U R                   5      U R                  :  a  U R                  U R                   5      $ g)z6
Translates bytes into lines, and calls lineReceived.
r   N)
_buffersplit	delimiterpopr7   disconnectingr   rO   lineLengthExceededlineReceived)r6   r   lineslines       r   rC   LineOnlyReceiver.dataReceived  s     $++DNN;yy}D~~++
 4y4??*..t44!!$'  t||t.**4<<88 /r   c                     [         ez
Override this for when each line is received.

@param line: The line which was received with the delimiter removed.
@type line: C{bytes}
rF   r6   r   s     r   r   LineOnlyReceiver.lineReceived  
     "!r   c                 N    U R                   R                  XR                  45      $ z
Sends a line to the other end of the connection.

@param line: The line to send, not including the delimiter.
@type line: C{bytes}
)r7   writeSequencer   r   s     r   sendLineLineOnlyReceiver.sendLine  s      ~~++T>>,BCCr   c                 6    U R                   R                  5       $ )zr
Called when the maximum line length has been reached.
Override if it needs to be dealt with in some special way.
r7   r   r   s     r   r   #LineOnlyReceiver.lineLengthExceeded  s    
 ~~,,..r   r   N)r   r   r   r    r!   r   r   rO   rC   r   r   r   r"   r   r   r   r   r     s-     GIJ9("D/r   r   c                   *    \ rS rSrSrS rS rS rSrg)_PauseableMixini  Fc                 F    SU l         U R                  R                  5         g NT)pausedr7   pauseProducingrP   s    r   r   _PauseableMixin.pauseProducing  s    %%'r   c                 h    SU l         U R                  R                  5         U R                  S5        g )NFr   )r   r7   resumeProducingrC   rP   s    r   r   _PauseableMixin.resumeProducing  s'    &&(#r   c                 F    SU l         U R                  R                  5         g r   )r   r7   stopProducingrP   s    r   r   _PauseableMixin.stopProducing  s    $$&r   )r   N)	r   r   r   r    r   r   r   r   r"   r   r   r   r   r     s    F(
'r   r   c                   `    \ rS rSrSrSrSrS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g)LineReceiveri  a  
A protocol that receives lines and/or raw data, depending on mode.

In line mode, each line that's received becomes a callback to
L{lineReceived}.  In raw data mode, each chunk of raw data becomes a
callback to L{LineReceiver.rawDataReceived}.
The L{setLineMode} and L{setRawMode} methods switch between the two modes.

This is useful for line-oriented protocols such as IRC, HTTP, POP, etc.

@cvar delimiter: The line-ending delimiter to use. By default this is
                 C{b'\r\n'}.
@cvar MAX_LENGTH: The maximum length of a line to allow (If a
                  sent line is longer than this, the connection is dropped).
                  Default is 16384.
rK   r   Fr   r   c                 ,    U R                   Ssol         U$ )zS
Clear buffered data.

@return: All of the cleared buffered data.
@rtype: C{bytes}
r   r   )r6   bs     r   clearLineBufferLineReceiver.clearLineBuffer  s     ,,<r   c                 h   U R                   (       a  U =R                  U-  sl        g SU l         U =R                  U-  sl        U R                  (       GaL  U R                  (       Gd:  U R                  (       a   U R                  R	                  U R
                  S5      u  o l        [        U5      nX0R                  :  a:  X R
                  -   U R                  -   nSU l        U R                  U5      SU l         $ U R                  U5      nU(       d,  U R                  (       a$  U R                  R                  (       a	  USU l         $ O4U R                  nSU l        U R                  U5      nU(       a	  USU l         $ U R                  (       a  U R                  (       d  GM:  SU l         g! [         ap    [        U R                  5      U R                  [        U R
                  5      -   :  a-  U R                  Sso l        U R                  U5      s SU l         $  SU l         gf = f! SU l         f = f)zu
Protocol.dataReceived.
Translates bytes into lines, and calls lineReceived (or
rawDataReceived, depending on mode.)
NTrK   r   F)_busyReceivingr   r   	line_moder   r   r   rO   r   r   r7   r   
ValueErrorrawDataReceived)r6   r   r   
lineLengthexceededwhys         r   rC   LineReceiver.dataReceived	  s    LLD L	("&DLLD L,,,t{{{>>'-1\\-?-?PQ-R*l &)Y
%7'+nn'<t||'KH+.DL#'#:#:8#D #(D #//5$..T^^5Q5Q#& #(D  <<D#&DL..t4C""'D3 ,,t{{{2 #(D+ & t||,3t~~CV1VW15s.D,#'#:#:4#@@$ #(D# " #(D+* #(DsK   AH( ;-F+ (AH( <AH( 	-H( >"H( +A'H%H( H( $H%%H( (	H1c                 B    SU l         U(       a  U R                  U5      $ g)aU  
Sets the line-mode of this receiver.

If you are calling this from a rawDataReceived callback,
you can pass in extra unhandled data, and that data will
be parsed for lines.  Further data received will be sent
to lineReceived rather than rawDataReceived.

Do not pass extra data if calling this function from
within a lineReceived callback.
rK   N)r   rC   )r6   extras     r   setLineModeLineReceiver.setLineMode1  s$     $$U++ r   c                     SU l         g)zu
Sets the raw mode of this receiver.
Further data received will be sent to rawDataReceived rather
than lineReceived.
r   N)r   rP   s    r   
setRawModeLineReceiver.setRawModeA  s     r   c                     [         e)z.
Override this for when raw data is received.
rF   rB   s     r   r   LineReceiver.rawDataReceivedI  s
     "!r   c                     [         er   rF   r   s     r   r   LineReceiver.lineReceivedO  r   r   c                 P    U R                   R                  XR                  -   5      $ r   )r7   r:   r   r   s     r   r   LineReceiver.sendLineX  s      ~~##D>>$9::r   c                 6    U R                   R                  5       $ )a?  
Called when the maximum line length has been reached.
Override if it needs to be dealt with in some special way.

The argument 'line' contains the remainder of the buffer, starting
with (at least some part) of the line which is too long. This may
be more than one line, or may be only the initial portion of the
line.
r   r   s     r   r   LineReceiver.lineLengthExceededa  s     ~~,,..r   )r   r   r   N)r   )r   r   r   r    r!   r   r   r   r   rO   r   rC   r   r   r   r   r   r   r"   r   r   r   r   r     sK    " IGNIJ&(P, "";
/r   r   c                       \ rS rSrSrSrg)StringTooLongErrorin  zN
Raised when trying to send a string too long for a length prefixed
protocol.
r   Nr   r   r   r   r   r   n  s    r   r   c                   "    \ rS rSrSrSS jrSrg)_RecvdCompatHackiu  a  
Emulates the to-be-deprecated C{IntNStringReceiver.recvd} attribute.

The C{recvd} attribute was where the working buffer for buffering and
parsing netstrings was kept.  It was updated each time new data arrived and
each time some of that data was parsed and delivered to application code.
The piecemeal updates to its string value were expensive and have been
removed from C{IntNStringReceiver} in the normal case.  However, for
applications directly reading this attribute, this descriptor restores that
behavior.  It only copies the working buffer when necessary (ie, when
accessed).  This avoids the cost for applications not using the data.

This is a custom descriptor rather than a property, because we still need
the default __set__ behavior in both new-style and old-style subclasses.
Nc                 4    UR                   UR                  S  $ N)_unprocessed_compatibilityOffset)r6   oselftypes      r   __get___RecvdCompatHack.__get__  s    !!%"<"<">??r   r   r   )r   r   r   r    r!   r   r"   r   r   r   r   r   u  s     @r   r   c                   J    \ rS rSrSrSrSrSr\" 5       r	S r
S rS rS	 rS
rg)IntNStringReceiveri  a  
Generic class for length prefixed protocols.

@ivar _unprocessed: bytes received, but not yet broken up into messages /
    sent to stringReceived.  _compatibilityOffset must be updated when this
    value is updated so that the C{recvd} attribute can be generated
    correctly.
@type _unprocessed: C{bytes}

@ivar structFormat: format used for struct packing/unpacking. Define it in
    subclass.
@type structFormat: C{str}

@ivar prefixLength: length of the prefix, in bytes. Define it in subclass,
    using C{struct.calcsize(structFormat)}
@type prefixLength: C{int}

@ivar _compatibilityOffset: the offset within C{_unprocessed} to the next
    message to be parsed. (used to generate the recvd attribute)
@type _compatibilityOffset: C{int}
r*   r   r   c                     [         e)z
Override this for notification when each complete string is received.

@param string: The complete string which was received with all
    framing (length prefix, etc) removed.
@type string: C{bytes}
rF   r;   s     r   rH   !IntNStringReceiver.stringReceived  s
     "!r   c                 8    U R                   R                  5         g)z
Callback invoked when a length prefix greater than C{MAX_LENGTH} is
received.  The default implementation disconnects the transport.
Override this.

@param length: The length prefix which was received.
@type length: C{int}
Nr   )r6   rs   s     r   lengthLimitExceeded&IntNStringReceiver.lengthLimitExceeded  s     	%%'r   c                 x   U R                   U-   nSnU R                  nU R                  nX l         [        U5      X4-   :  a  U R                  (       d  X4-   n[        XRX6 5      u  nXpR                  :  a  X l         X0l        U R                  U5        gXg-   n[        U5      U:  a  OX&U n	UnX0l        U R                  U	5        SU R                  ;   a4  U R                  R                  S5      nX l         S=U l        nU(       a  M  g[        U5      X4-   :  a  U R                  (       d  M  X#S U l         SU l        g)z<
Convert int prefixed strings into calls to stringReceived.
r   Nrecvd)r   prefixLengthstructFormatr   r   r   rO   r   r   rH   __dict__r   )
r6   r   alldatacurrentOffsetr   fmtmessageStartrs   
messageEndpackets
             r   rC   IntNStringReceiver.dataReceived  s5    ##d*((#'l};<T[[(7LsM$GHIV'$+!,9)((0%.J7|j( *5F&M(5%'
 $--'--++G4$+!<==)M9 'l};<T[[[B $N3$%!r   c                    [        U5      SSU R                  -  -  :  a,  [        S[        U5      < SSSU R                  -  -  < 35      eU R                  R	                  [        U R                  [        U5      5      U-   5        g)z
Send a prefixed string to the other end of the connection.

@param string: The string to send.  The necessary framing (length
    prefix, etc) will be added.
@type string: C{bytes}
r+      zTry to send z bytes whereas maximum is N)r   r   r   r7   r:   r   r   r;   s     r   r=   IntNStringReceiver.sendString  su     v;!D$5$5 566$v;a$*;*;&; <>  	T$"3"3S[AFJKr   )r   r   N)r   r   r   r    r!   rO   r   r   r   r   rH   r   rC   r=   r"   r   r   r   r   r     s9    , JL E"	(/&bLr   r   c                   ,    \ rS rSrSrSr\" \5      rSrg)Int32StringReceiveri  z
A receiver for int32-prefixed strings.

An int32 string is a string prefixed by 4 bytes, the 32-bit length of
the string encoded in network byte order.

This class publishes the same interface as NetstringReceiver.
z!Ir   N	r   r   r   r    r!   r   r   r   r"   r   r   r   r   r          LL)Lr   r   c                   ,    \ rS rSrSrSr\" \5      rSrg)Int16StringReceiveri  z
A receiver for int16-prefixed strings.

An int16 string is a string prefixed by 2 bytes, the 16-bit length of
the string encoded in network byte order.

This class publishes the same interface as NetstringReceiver.
z!Hr   Nr   r   r   r   r  r    r  r   r  c                   ,    \ rS rSrSrSr\" \5      rSrg)Int8StringReceiveri  z
A receiver for int8-prefixed strings.

An int8 string is a string prefixed by 1 byte, the 8-bit length of
the string.

This class publishes the same interface as NetstringReceiver.
z!Br   Nr   r   r   r   r  r    r  r   r  c                   "    \ rS rSrSrSrS rSrg)StatefulStringProtocoli)  a  
A stateful string protocol.

This is a mixin for string protocols (L{Int32StringReceiver},
L{NetstringReceiver}) which translates L{stringReceived} into a callback
(prefixed with C{'proto_'}) depending on state.

The state C{'done'} is special; if a C{proto_*} method returns it, the
connection will be closed immediately.

@ivar state: Current state of the protocol. Defaults to C{'init'}.
@type state: C{str}
initc                     SU R                   -   n[        X5      nU" U5      U l         U R                   S:X  a  U R                  R                  5         gg! [         a%    [
        R                  " SU R                   S5         gf = f)ai  
Choose a protocol phase function and call it.

Call back to the appropriate protocol phase; this begins with
the function C{proto_init} and moves on to C{proto_*} depending on
what each C{proto_*} function returns.  (For example, if
C{self.proto_init} returns 'foo', then C{self.proto_foo} will be the
next function called when a protocol message is received.
proto_donecallbackz	not foundN)stategetattrr7   r   AttributeErrorr   msg)r6   r<   ptostatehandlers       r   rH   %StatefulStringProtocol.stringReceived:  ss    	0TZZ'C"4-L &f-DJzzV#--/ $	  	9GGJ

K8	9s   A ,BB)r  N)r   r   r   r    r!   r  rH   r"   r   r   r   r  r  )  s     E0r   r  c                   @    \ rS rSrSrSrSrSrSS jrS r	S r
S	 rS
rg)
FileSenderiO  a	  
A producer that sends the contents of a file to a consumer.

This is a helper for protocols that, at some point, will take a
file-like object, read its contents, and write them out to the network,
optionally performing some transformation on the bytes in between.
r    Nc                     Xl         X l        X0l        [        R                  " 5       =U l        nU R                  R                  U S5        U$ )a8  
Begin transferring a file

@type file: Any file-like object
@param file: The file object to read data from

@type consumer: Any implementor of IConsumer
@param consumer: The object to write data to

@param transform: A callable taking one string argument and returning
the same.  All bytes read from the file are passed through this before
being written to the consumer.

@rtype: C{Deferred}
@return: A deferred whose callback will be invoked when the file has
been completely written to the consumer. The last byte written to the
consumer is passed to the callback.
F)fileconsumer	transformr   DeferreddeferredregisterProducer)r6   r  r  r  r  s        r   beginFileTransferFileSender.beginFileTransfer^  s?    & 	 "#(>>#33&&tU3r   c                    SnU R                   (       a%  U R                   R                  U R                  5      nU(       d_  S U l         U R                  R	                  5         U R
                  (       a,  U R
                  R                  U R                  5        S U l        g U R                  (       a  U R                  U5      nU R                  R                  U5        USS  U l        g )Nr  r   )
r  read
CHUNK_SIZEr  unregisterProducerr  r  lastSentr  r:   )r6   chunks     r   r   FileSender.resumeProducingy  s    99IINN4??3EDIMM,,.}}&&t}}5 $>>NN5)EE"bc
r   c                     g r   r   rP   s    r   r   FileSender.pauseProducing  s    r   c                 ~    U R                   (       a,  U R                   R                  [        S5      5        S U l         g g )Nz#Consumer asked us to stop producing)r  errback	ExceptionrP   s    r   r   FileSender.stopProducing  s-    ==MM!!),Q"RS DM r   )r  r  r  r$  r  r   )r   r   r   r    r!   r"  r$  r  r  r   r   r   r"   r   r   r   r  r  O  s,     JHH6#"!r   r  )&r!   rL   r   ior   structr   r   r   zope.interfacer   twisted.internetr   r	   r
   twisted.pythonr   r   DEBUGr   r   r+  r%   r-   r(   r   r   r   AssertionErrorr   r   r   r   r  r  r  	IProducerr  r   r   r   <module>r5     s.  
  
  ) ) & 9 8 
H   	
* ) ^)) ^B=/x(( =/@' '"D/8$$o D/N @ @*rL**O rLj*, **, **+ *#0 #0L Z!!"@! @! #@!r   