
    hc\                         S 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
  SSKJr  SSKJrJr  Sr " S	 S
\5      r " S S\5      r " S S\5      r " S S5      r " S S\\
5      r/ SQrg)ap  
Memcache client protocol. Memcached is a caching server, storing data in the
form of pairs key/value, and memcache is the protocol to talk with it.

To connect to a server, create a factory for L{MemCacheProtocol}::

    from twisted.internet import reactor, protocol
    from twisted.protocols.memcache import MemCacheProtocol, DEFAULT_PORT
    d = protocol.ClientCreator(reactor, MemCacheProtocol
        ).connectTCP("localhost", DEFAULT_PORT)
    def doSomething(proto):
        # Here you call the memcache operations
        return proto.set("mykey", "a lot of data")
    d.addCallback(doSomething)
    reactor.run()

All the operations of the memcache protocol are present, but
L{MemCacheProtocol.set} and L{MemCacheProtocol.get} are the more important.

See U{http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt} for
more information about the protocol.
    )deque)DeferredTimeoutErrorfail)LineReceiver)TimeoutMixin)log)nativeStringnetworkStringi+  c                       \ rS rSrSrSrg)NoSuchCommand(   z9
Exception raised when a non existent command is called.
 N__name__
__module____qualname____firstlineno____doc____static_attributes__r       a/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/protocols/memcache.pyr   r   (       r   r   c                       \ rS rSrSrSrg)ClientError.   z)
Error caused by an invalid client call.
r   Nr   r   r   r   r   r   .   r   r   r   c                       \ rS rSrSrSrg)ServerError4   z"
Problem happening on the server.
r   Nr   r   r   r   r   r   4   r   r   r   c                   *    \ rS rSrSrS rS rS rSrg)Command:   a  
Wrap a client action into an object, that holds the values used in the
protocol.

@ivar _deferred: the L{Deferred} object that will be fired when the result
    arrives.
@type _deferred: L{Deferred}

@ivar command: name of the command sent to the server.
@type command: L{bytes}
c                 x    Xl         [        5       U l        UR                  5        H  u  p4[	        XU5        M     g)z
Create a command.

@param command: the name of the command.
@type command: L{bytes}

@param kwargs: this values will be stored as attributes of the object
    for future use
N)commandr   	_deferreditemssetattr)selfr$   kwargskvs        r   __init__Command.__init__G   s.     !LLNDADQ #r   c                 :    U R                   R                  U5        g)z2
Shortcut method to fire the underlying deferred.
N)r%   callback)r(   values     r   successCommand.successV   s     	&r   c                 :    U R                   R                  U5        g)z%
Make the underlying deferred fails.
N)r%   errback)r(   errors     r   r   Command.fail\   s     	u%r   )r%   r$   N)	r   r   r   r   r   r,   r1   r   r   r   r   r   r!   r!   :   s    
 '&r   r!   c                   &   \ 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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-S jrS-S jrS rS.S jrS.S jrS.S jr S.S jr!S  r"S! r#S" r$S/S# jr%S/S$ jr&S% r'S0S' jr(S( r)S) r*S* r+S+r,g&)1MemCacheProtocolc   a  
MemCache protocol: connect to a memcached server to store/retrieve values.

@ivar persistentTimeOut: the timeout period used to wait for a response.
@type persistentTimeOut: L{int}

@ivar _current: current list of requests waiting for an answer from the
    server.
@type _current: L{deque} of L{Command}

@ivar _lenExpected: amount of data expected in raw mode, when reading for
    a value.
@type _lenExpected: L{int}

@ivar _getBuffer: current buffer of data, used to store temporary data
    when reading in raw mode.
@type _getBuffer: L{list}

@ivar _bufferLength: the total amount of bytes in C{_getBuffer}.
@type _bufferLength: L{int}

@ivar _disconnected: indicate if the connectionLost has been called or not.
@type _disconnected: L{bool}
   Fc                 h    [        5       U l        SU l        SU l        SU l        U=U l        U l        g)z
Create the protocol.

@param timeOut: the timeout to wait before detecting that the
    connection is dead and close it. It's expressed in seconds.
@type timeOut: L{int}
N)r   _current_lenExpected
_getBuffer_bufferLengthpersistentTimeOuttimeOut)r(   rA   s     r   r,   MemCacheProtocol.__init__   s3      !077r   c                     U R                   (       a?  U R                   R                  5       nUR                  U5        U R                   (       a  M>  gg)zG
Cancel all the outstanding commands, making them fail with C{reason}.
N)r<   popleftr   )r(   reasoncmds      r   _cancelCommands MemCacheProtocol._cancelCommands   s4     mm--'')CHHV mmmr   c                 l    U R                  [        S5      5        U R                  R                  5         g)z*
Close the connection in case of timeout.
zConnection timeoutN)rG   r   	transportloseConnectionr(   s    r   timeoutConnection"MemCacheProtocol.timeoutConnection   s(     	\*>?@%%'r   c                 `    SU l         U R                  U5        [        R                  " X5        g)z)
Cause any outstanding commands to fail.
TN)_disconnectedrG   r   connectionLost)r(   rE   s     r   rQ   MemCacheProtocol.connectionLost   s(     "V$##D1r   c                     U R                   (       d  U R                  U R                  5        [        R                  " X5        g)z1
Override sendLine to add a timeout to response.
N)r<   
setTimeoutr@   r   sendLine)r(   lines     r   rU   MemCacheProtocol.sendLine   s+     }}OOD223d)r   c                 R   U R                  5         U R                  R                  U5        U =R                  [	        U5      -  sl        U R                  U R
                  S-   :  a  SR                  U R                  5      nUSU R
                   nXR
                  S-   S nUnSU l        SU l        SU l        U R                  S   nUR                  (       a7  UR                  UR                     u  pgXgU4UR                  UR                  '   OXEl        U R                  U5        gg)z
Collect data for a get.
   r   Nr   )resetTimeoutr>   appendr?   lenr=   joinr<   multiplevalues
currentKeyr0   setLineMode)r(   databufremvalrF   flagscass           r   rawDataReceived MemCacheProtocol.rawDataReceived   s     	t$c$i'!2!2Q!6688DOO,D***+C((1,./CC $D"DO!%D--"C|| ZZ7
.3#->

3>>*	S! 7r   c                 V    U R                   R                  5       R                  S5        g)z/
Manage a success response to a set operation.
TNr<   rD   r1   rL   s    r   
cmd_STOREDMemCacheProtocol.cmd_STORED        	''-r   c                 V    U R                   R                  5       R                  S5        g)zr
Manage a specific 'not stored' response to a set operation: this is not
an error, but some condition wasn't met.
FNrk   rL   s    r   cmd_NOT_STOREDMemCacheProtocol.cmd_NOT_STORED   s    
 	''.r   c           	         U R                   R                  5       nUR                  S:X  a  UR                  (       aF  UR                  R                  5        VVs0 s H  u  p#X#SSS2   _M     nnnUR                  U5        gUR                  UR                  UR                  45        gUR                  S:X  a`  UR                  (       a  UR                  UR                  5        gUR                  UR                  UR                  UR                  45        gUR                  S:X  a  UR                  UR                  5        g[        SR                  [        UR                  5      5      5      es  snnf )z2
This the end token to a get or a stat operation.
   getNrY      gets   statsz%Unexpected END response to {} command)r<   rD   r$   r^   r_   r&   r1   rf   r0   rg   RuntimeErrorformatr
   )r(   rF   keyre   r_   s        r   cmd_ENDMemCacheProtocol.cmd_END   s    mm##%;;& ||8;

8H8H8JK8JHC#3Q3x-8JKF#SYY		23[[G#||CJJ'SYY;<[[H$KK

#7>> -  Ls   E3c                 V    U R                   R                  5       R                  S5        g)z-
Manage error response for incr/decr/delete.
FNrk   rL   s    r   cmd_NOT_FOUNDMemCacheProtocol.cmd_NOT_FOUND        	''.r   c                    U R                   S   nUR                  S:X  a  UR                  5       u  p4nSnOUR                  5       u  p4pV[        U5      U l        / U l        SU l        UR                  (       a;  X2R                  ;  a  [        S5      eX2l
        [        U5      U/UR                  U'   O1UR                  U:w  a  [        S5      e[        U5      Ul        Xbl        U R                  5         g)z*
Prepare the reading a value after a get.
r   rs   r   zUnexpected commands answer.N)r<   r$   splitintr=   r>   r?   r^   keysrv   r`   r_   rx   rf   rg   
setRawMode)r(   rV   rF   rx   rf   lengthrg   s          r   	cmd_VALUEMemCacheProtocol.cmd_VALUE   s     mmA;;& !%CC&*jjl#CK<<((""#@AA N"5z3/CJJsOww#~"#@AAE
CIGr   c                 f    U R                   S   nUR                  SS5      u  p4XBR                  U'   g)z
Reception of one stat line.
r          N)r<   r   r_   )r(   rV   rF   rx   re   s        r   cmd_STATMemCacheProtocol.cmd_STAT	  s/     mmA::dA&

3r   c                 V    U R                   R                  5       R                  U5        g)z
Read version token.
Nrk   )r(   versionDatas     r   cmd_VERSIONMemCacheProtocol.cmd_VERSION  s     	''4r   c                     [         R                  " S5        U R                  R                  5       nUR	                  [        5       5        g)z'
A non-existent command has been sent.
zNon-existent command sent.N)r	   errr<   rD   r   r   )r(   rF   s     r   	cmd_ERRORMemCacheProtocol.cmd_ERROR  s1     	,-mm##%!r   c                     [        U5      n[        R                  " SU-   5        U R                  R	                  5       nUR                  [        U5      5        g)z 
An invalid input as been sent.
zInvalid input: N)reprr	   r   r<   rD   r   r   r(   errTextrF   s      r   cmd_CLIENT_ERROR!MemCacheProtocol.cmd_CLIENT_ERROR  sB     w-!G+,mm##%W%&r   c                     [        U5      n[        R                  " SU-   5        U R                  R	                  5       nUR                  [        U5      5        g)z$
An error has happened server-side.
zServer error: N)r   r	   r   r<   rD   r   r   r   s      r   cmd_SERVER_ERROR!MemCacheProtocol.cmd_SERVER_ERROR(  sB     w- 7*+mm##%W%&r   c                 V    U R                   R                  5       R                  S5        g)z.
A delete command has completed successfully.
TNrk   rL   s    r   cmd_DELETEDMemCacheProtocol.cmd_DELETED1  rn   r   c                 V    U R                   R                  5       R                  S5        g)z&
The last command has been completed.
TNrk   rL   s    r   cmd_OKMemCacheProtocol.cmd_OK7  rn   r   c                 V    U R                   R                  5       R                  S5        g)z%
A C{checkAndSet} update has failed.
FNrk   rL   s    r   
cmd_EXISTSMemCacheProtocol.cmd_EXISTS=  r~   r   c                    U R                  5         UR                  SS5      S   n[        U S[        U5      -   S5      nUb0  UR                  SS5      SS nU(       a  U" US   5        OtU" 5         OlUR	                  SS5      n[        U S[        U5      -   S5      nUb  U" 5         O6U R
                  R                  5       n[        U5      nUR                  U5        U R
                  (       d  U R                  S5        gg)z(
Receive line commands from the server.
r   r   r   cmd_N   _)
rZ   r   getattrr
   replacer<   rD   r   r1   rT   )r(   rV   tokenrF   argsre   s         r   lineReceivedMemCacheProtocol.lineReceivedC  s     	

4#A&dF\%%88$??::dA&qr*DDG <<d+D$d); ;TBC mm++-$iC }}OOD! r   c                 &    U R                  SX5      $ )an  
Increment the value of C{key} by given value (default to 1).
C{key} must be consistent with an int. Return the new value.

@param key: the key to modify.
@type key: L{bytes}

@param val: the value to increment.
@type val: L{int}

@return: a deferred with will be called back with the new value
    associated with the key (after the increment).
@rtype: L{Deferred}
s   incr	_incrdecrr(   rx   re   s      r   	incrementMemCacheProtocol.increment`  s     ~~gs00r   c                 &    U R                  SX5      $ )a  
Decrement the value of C{key} by given value (default to 1).
C{key} must be consistent with an int. Return the new value, coerced to
0 if negative.

@param key: the key to modify.
@type key: L{bytes}

@param val: the value to decrement.
@type val: L{int}

@return: a deferred with will be called back with the new value
    associated with the key (after the decrement).
@rtype: L{Deferred}
s   decrr   r   s      r   	decrementMemCacheProtocol.decrementq  s      ~~gs00r   c                    U R                   (       a  [        [        S5      5      $ [        U[        5      (       d!  [        [        S[        U5       S35      5      $ [        U5      U R                  :  a  [        [        S5      5      $ SR                  XS[        U5      4-  /5      nU R                  U5        [        XS9nU R                  R                  U5        UR                  $ )z!
Internal wrapper for incr/decr.
not connectedInvalid type for key: , expecting bytesKey too longr   s   %drx   )rP   r   rv   
isinstancebytesr   typer\   MAX_KEY_LENGTHr]   r   rU   r!   r<   r[   r%   )r(   rF   rx   re   fullcmdcmdObjs         r   r   MemCacheProtocol._incrdecr  s     _566#u%%4T#YK?PQR  s8d)))N344))SuC{':;<g&V$r   c                 *    U R                  SXX4S5      $ )a-  
Replace the given C{key}. It must already exist in the server.

@param key: the key to replace.
@type key: L{bytes}

@param val: the new value associated with the key.
@type val: L{bytes}

@param flags: the flags to store with the key.
@type flags: L{int}

@param expireTime: if different from 0, the relative time in seconds
    when the key will be deleted from the store.
@type expireTime: L{int}

@return: a deferred that will fire with C{True} if the operation has
    succeeded, and C{False} with the key didn't previously exist.
@rtype: L{Deferred}
s   replacer   _setr(   rx   re   rf   
expireTimes        r   r   MemCacheProtocol.replace  s    * yySu#FFr   c                 *    U R                  SXX4S5      $ )a  
Add the given C{key}. It must not exist in the server.

@param key: the key to add.
@type key: L{bytes}

@param val: the value associated with the key.
@type val: L{bytes}

@param flags: the flags to store with the key.
@type flags: L{int}

@param expireTime: if different from 0, the relative time in seconds
    when the key will be deleted from the store.
@type expireTime: L{int}

@return: a deferred that will fire with C{True} if the operation has
    succeeded, and C{False} with the key already exists.
@rtype: L{Deferred}
s   addr   r   r   s        r   addMemCacheProtocol.add      * yy5cBBr   c                 *    U R                  SXX4S5      $ )a  
Set the given C{key}.

@param key: the key to set.
@type key: L{bytes}

@param val: the value associated with the key.
@type val: L{bytes}

@param flags: the flags to store with the key.
@type flags: L{int}

@param expireTime: if different from 0, the relative time in seconds
    when the key will be deleted from the store.
@type expireTime: L{int}

@return: a deferred that will fire with C{True} if the operation has
    succeeded.
@rtype: L{Deferred}
s   setr   r   r   s        r   setMemCacheProtocol.set  r   r   c                 *    U R                  SXXEU5      $ )a  
Change the content of C{key} only if the C{cas} value matches the
current one associated with the key. Use this to store a value which
hasn't been modified since last time you fetched it.

@param key: The key to set.
@type key: L{bytes}

@param val: The value associated with the key.
@type val: L{bytes}

@param cas: Unique 64-bit value returned by previous call of C{get}.
@type cas: L{bytes}

@param flags: The flags to store with the key.
@type flags: L{int}

@param expireTime: If different from 0, the relative time in seconds
    when the key will be deleted from the store.
@type expireTime: L{int}

@return: A deferred that will fire with C{True} if the operation has
    succeeded, C{False} otherwise.
@rtype: L{Deferred}
s   casr   )r(   rx   re   rg   rf   r   s         r   checkAndSetMemCacheProtocol.checkAndSet  s    4 yy5cBBr   c           
         U R                   (       a  [        [        S5      5      $ [        U[        5      (       d!  [        [        S[        U5       S35      5      $ [        U5      U R                  :  a  [        [        S5      5      $ [        U[        5      (       d!  [        [        S[        U5       S35      5      $ U(       a  SU-   n[        U5      nSR                  X[        SXEU4-  5      /5      U-   nU R                  U5        U R                  U5        [        XXGS9n	U R                  R                  U	5        U	R                  $ )	z&
Internal wrapper for setting values.
r   r   r   r   zInvalid type for value: r   z%d %d %d)rx   rf   r   )rP   r   rv   r   r   r   r   r\   r   r]   r   rU   r!   r<   r[   r%   )
r(   rF   rx   re   rf   r   rg   r   r   r   s
             r   r   MemCacheProtocol._set  s0    _566#u%%4T#YK?PQR  s8d)))N344#u%%6tCykARST  *CSII=u&6Q)QRS  	 	gcUBV$r   c                 ,    U R                  SXSSS5      $ )aL  
Append given data to the value of an existing key.

@param key: The key to modify.
@type key: L{bytes}

@param val: The value to append to the current value associated with
    the key.
@type val: L{bytes}

@return: A deferred that will fire with C{True} if the operation has
    succeeded, C{False} otherwise.
@rtype: L{Deferred}
s   appendr   r   r   r   s      r   r[   MemCacheProtocol.append  s      yyCaC88r   c                 ,    U R                  SXSSS5      $ )aN  
Prepend given data to the value of an existing key.

@param key: The key to modify.
@type key: L{bytes}

@param val: The value to prepend to the current value associated with
    the key.
@type val: L{bytes}

@return: A deferred that will fire with C{True} if the operation has
    succeeded, C{False} otherwise.
@rtype: L{Deferred}
s   prependr   r   r   r   s      r   prependMemCacheProtocol.prepend'  s      yySq!S99r   c                 *    U R                  U/US5      $ )a6  
Get the given C{key}. It doesn't support multiple keys. If
C{withIdentifier} is set to C{True}, the command issued is a C{gets},
that will return the current identifier associated with the value. This
identifier has to be used when issuing C{checkAndSet} update later,
using the corresponding method.

@param key: The key to retrieve.
@type key: L{bytes}

@param withIdentifier: If set to C{True}, retrieve the current
    identifier along with the value and the flags.
@type withIdentifier: L{bool}

@return: A deferred that will fire with the tuple (flags, value) if
    C{withIdentifier} is C{False}, or (flags, cas identifier, value)
    if C{True}.  If the server indicates there is no value
    associated with C{key}, the returned value will be L{None} and
    the returned flags will be C{0}.
@rtype: L{Deferred}
F_get)r(   rx   withIdentifiers      r   getMemCacheProtocol.get9  s    , yy#66r   c                 &    U R                  XS5      $ )at  
Get the given list of C{keys}.  If C{withIdentifier} is set to C{True},
the command issued is a C{gets}, that will return the identifiers
associated with each values. This identifier has to be used when
issuing C{checkAndSet} update later, using the corresponding method.

@param keys: The keys to retrieve.
@type keys: L{list} of L{bytes}

@param withIdentifier: If set to C{True}, retrieve the identifiers
    along with the values and the flags.
@type withIdentifier: L{bool}

@return: A deferred that will fire with a dictionary with the elements
    of C{keys} as keys and the tuples (flags, value) as values if
    C{withIdentifier} is C{False}, or (flags, cas identifier, value) if
    C{True}.  If the server indicates there is no value associated with
    C{key}, the returned values will be L{None} and the returned flags
    will be C{0}.
@rtype: L{Deferred}

@since: 9.0
Tr   )r(   r   r   s      r   getMultipleMemCacheProtocol.getMultipleQ  s    0 yyt44r   c           	      h   [        U5      nU R                  (       a  [        [        S5      5      $ U Hj  n[	        U[
        5      (       d#  [        [        S[        U5       S35      5      s  $ [        U5      U R                  :  d  MV  [        [        S5      5      s  $    U(       a  SnOSnSR                  U/U-   5      nU R                  U5        U(       a  U Vs0 s H  oDS_M     nn[        XQUS	S
9nO[        XQS   SSSSS9nU R                  R                  U5        UR                  $ s  snf )z.
Helper method for C{get} and C{getMultiple}.
r   r   r   r   rt   rs   r   )r   r   NT)r   r_   r^   r   Nr   F)rx   r0   rf   rg   r^   )listrP   r   rv   r   r   r   r   r\   r   r]   rU   r!   r<   r[   r%   )	r(   r   r   r^   rx   rF   r   r_   r   s	            r   r   MemCacheProtocol._getk  s    Dz_566Cc5))"8cCT UV  3x$---K788  CC))SEDL)g59:Tc>)TF:SFTJFaA3F 	V$ ;s   D/Nc                     U(       a  SU-   nOSnU R                   (       a  [        [        S5      5      $ U R                  U5        [	        S0 S9nU R
                  R                  U5        UR                  $ )a  
Get some stats from the server. It will be available as a dict.

@param arg: An optional additional string which will be sent along
    with the I{stats} command.  The interpretation of this value by
    the server is left undefined by the memcache protocol
    specification.
@type arg: L{None} or L{bytes}

@return: a deferred that will fire with a L{dict} of the available
    statistics.
@rtype: L{Deferred}
s   stats ru   r   )r_   rP   r   rv   rU   r!   r<   r[   r%   )r(   argrF   r   s       r   statsMemCacheProtocol.stats  sd     c/CC_566c"-V$r   c                     U R                   (       a  [        [        S5      5      $ U R                  S5        [	        S5      nU R
                  R                  U5        UR                  $ )z
Get the version of the server.

@return: a deferred that will fire with the string value of the
    version.
@rtype: L{Deferred}
r   s   versionr   r(   r   s     r   versionMemCacheProtocol.version  sQ     _566j!$V$r   c                 B   U R                   (       a  [        [        S5      5      $ [        U[        5      (       d!  [        [        S[        U5       S35      5      $ U R                  SU-   5        [        SUS9nU R                  R                  U5        UR                  $ )z
Delete an existing C{key}.

@param key: the key to delete.
@type key: L{bytes}

@return: a deferred that will be called back with C{True} if the key
    was successfully deleted, or C{False} if not.
@rtype: L{Deferred}
r   r   r   s   delete s   deleter   )rP   r   rv   r   r   r   r   rU   r!   r<   r[   r%   )r(   rx   r   s      r   deleteMemCacheProtocol.delete  s     _566#u%%4T#YK?PQR  	j3&',V$r   c                     U R                   (       a  [        [        S5      5      $ U R                  S5        [	        S5      nU R
                  R                  U5        UR                  $ )z
Flush all cached values.

@return: a deferred that will be called back with C{True} when the
    operation has succeeded.
@rtype: L{Deferred}
r   s	   flush_allr   r   s     r   flushAllMemCacheProtocol.flushAll  sQ     _566l#&V$r   )r?   r<   rP   r>   r=   r@   rA   )<   )r   )r   r   )F)N)-r   r   r   r   r   r   rP   r,   rG   rM   rQ   rU   rh   rl   rp   ry   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   r   r   r   r   r   r   r   r8   r8   c   s    2 NM8(2*"../2/25"''../":1"1$ $G.C.C.C8 >9$:$7054 < 2  , r   r8   )r8   DEFAULT_PORTr   r   r   N)r   collectionsr   twisted.internet.deferr   r   r   twisted.protocols.basicr   twisted.protocols.policiesr   twisted.pythonr	   twisted.python.compatr
   r   r   	Exceptionr   r   r   r!   r8   __all__r   r   r   <module>r     sm   
0  ? ? 0 3  =I ) ) && &&Rq	 |\ q	 hr   