+
    Dfjc\                         R t ^ RIHt ^ RIHtHtHt ^ RIHt ^ RI	H
t
 ^ RIHt ^ RIHtHt Rt ! R R	]4      t ! R
 R]4      t ! R R]4      t ! R R4      t ! R R]]
4      t. ROtR# )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                       ] tR t^(tRtRtR# )NoSuchCommandz9
Exception raised when a non existent command is called.
 N__name__
__module____qualname____firstlineno____doc____static_attributes__r       </usr/lib/python3/dist-packages/twisted/protocols/memcache.pyr   r   (       r   r   c                       ] tR t^.tRtRtR# )ClientErrorz)
Error caused by an invalid client call.
r   Nr   r   r   r   r   r   .   r   r   r   c                       ] tR t^4tRtRtR# )ServerErrorz"
Problem happening on the server.
r   Nr   r   r   r   r   r   4   r   r   r   c                   6   a  ] tR t^:t o RtR tR tR tRtV t	R# )Commanda  
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                z    Wn         \        4       V n        VP                  4        F  w  r4\	        WV4       K  	  R# )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                <    V P                   P                  V4       R# )z2
Shortcut method to fire the underlying deferred.
N)r    callback)r#   values   &&r   successCommand.successV   s     	&r   c                <    V P                   P                  V4       R# )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'   r,   r   r   __classdictcell____classdict__s   @r   r   r   :   s     
 '& &r   r   c                   2  a  ] tR t^ct o Rt^tRtR*R ltR tR t	R t
R tR tR	 tR
 tR tR tR tR tR tR tR tR tR tR tR tR tR+R ltR+R ltR tR,R ltR,R ltR,R lt R,R lt!R t"R t#R  t$R-R! lt%R-R" lt&R# t'R.R% lt(R& t)R' t*R( t+R)t,V t-R$# )/MemCacheProtocola  
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                j    \        4       V n        RV n        RV n        RV n        V;V n        V n        R# )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#   r=   s   &&r   r'   MemCacheProtocol.__init__   s3      !077r   c                    V P                   '       d.   V P                   P                  4       pVP                  V4       K?  R# )zG
Cancel all the outstanding commands, making them fail with C{reason}.
N)r8   popleftr   )r#   reasoncmds   && r   _cancelCommands MemCacheProtocol._cancelCommands   s0     mmm--'')CHHV r   c                n    V P                  \        R4      4       V P                  P                  4        R# )z*
Close the connection in case of timeout.
zConnection timeoutN)rC   r   	transportloseConnectionr#   s   &r   timeoutConnection"MemCacheProtocol.timeoutConnection   s(     	\*>?@%%'r   c                b    RV n         V P                  V4       \        P                  ! W4       R# )z)
Cause any outstanding commands to fail.
TN)_disconnectedrC   r   connectionLost)r#   rA   s   &&r   rM   MemCacheProtocol.connectionLost   s(     "V$##D1r   c                    V P                   '       g   V P                  V P                  4       \        P                  ! W4       R# )z1
Override sendLine to add a timeout to response.
N)r8   
setTimeoutr<   r   sendLine)r#   lines   &&r   rQ   MemCacheProtocol.sendLine   s-     }}}OOD223d)r   c                   V P                  4        V P                  P                  V4       V ;P                  \	        V4      ,          un        V P                  V P
                  ^,           8  d   RP                  V P                  4      pVRV P
                   pWP
                  ^,           R pTpRV n        RV n        RV n        V P                  ^ ,          pVP                  '       d<   VP                  VP                  ,          w  rgWgV3VP                  VP                  &   MWEn        V P                  V4       R# R# )z
Collect data for a get.
r   N)resetTimeoutr:   appendr;   lenr9   joinr8   multiplevalues
currentKeyr+   setLineMode)r#   databufremvalrB   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                X    V P                   P                  4       P                  R4       R# )z/
Manage a success response to a set operation.
TNr8   r@   r,   rH   s   &r   
cmd_STOREDMemCacheProtocol.cmd_STORED        	''-r   c                X    V P                   P                  4       P                  R4       R# )zr
Manage a specific 'not stored' response to a set operation: this is not
an error, but some condition wasn't met.
FNrf   rH   s   &r   cmd_NOT_STOREDMemCacheProtocol.cmd_NOT_STORED   s    
 	''.r   c                   V P                   P                  4       pVP                  R8X  d   VP                  '       dH   VP                  P                  4        UUu/ uF  w  r#W#R,          bK  	  pppVP                  V4       R# VP                  VP                  VP                  34       R# VP                  R8X  dd   VP                  '       d   VP                  VP                  4       R# VP                  VP                  VP                  VP                  34       R# VP                  R8X  d   VP                  VP                  4       R# \        RP                  \        VP                  4      4      4      hu uppi )z2
This the end token to a get or a stat operation.
   get:NN      gets   statsz%Unexpected END response to {} commandN)r8   r@   r   rY   rZ   r!   r,   ra   r+   rb   RuntimeErrorformatr	   )r#   rB   keyr`   rZ   s   &    r   cmd_ENDMemCacheProtocol.cmd_END   s    mm##%;;& |||8;

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

#7>> -  Ls   E=c                X    V P                   P                  4       P                  R4       R# )z-
Manage error response for incr/decr/delete.
FNrf   rH   s   &r   cmd_NOT_FOUNDMemCacheProtocol.cmd_NOT_FOUND        	''.r   c                   V P                   ^ ,          pVP                  R8X  d   VP                  4       w  r4pRpMVP                  4       w  r4rV\        V4      V n        . V n        ^ V n        VP                  '       d=   W2P                  9  d   \        R4      hW2n
        \        V4      V.VP                  V&   M2VP                  V8w  d   \        R4      h\        V4      Vn        Wbn        V P                  4        R# )z*
Prepare the reading a value after a get.
rn   r   zUnexpected commands answer.N)r8   r   splitintr9   r:   r;   rY   keysrr   r[   rZ   rt   ra   rb   
setRawMode)r#   rR   rB   rt   ra   lengthrb   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                p    V P                   ^ ,          pVP                  R^4      w  r4WBP                  V&   R# )z
Reception of one stat line.
    N)r8   r|   rZ   )r#   rR   rB   rt   r`   s   &&   r   cmd_STATMemCacheProtocol.cmd_STAT	  s/     mmA::dA&

3r   c                X    V P                   P                  4       P                  V4       R# )z
Read version token.
Nrf   )r#   versionDatas   &&r   cmd_VERSIONMemCacheProtocol.cmd_VERSION  s     	''4r   c                    \         P                  ! R4       V P                  P                  4       pVP	                  \        4       4       R# )z'
A non-existent command has been sent.
zNon-existent command sent.N)r   errr8   r@   r   r   )r#   rB   s   & r   	cmd_ERRORMemCacheProtocol.cmd_ERROR  s1     	,-mm##%!r   c                    \        V4      p\        P                  ! RV,           4       V P                  P	                  4       pVP                  \        V4      4       R# )z 
An invalid input as been sent.
zInvalid input: N)reprr   r   r8   r@   r   r   r#   errTextrB   s   && r   cmd_CLIENT_ERROR!MemCacheProtocol.cmd_CLIENT_ERROR  sB     w-!G+,mm##%W%&r   c                    \        V4      p\        P                  ! RV,           4       V P                  P	                  4       pVP                  \        V4      4       R# )z$
An error has happened server-side.
zServer error: N)r   r   r   r8   r@   r   r   r   s   && r   cmd_SERVER_ERROR!MemCacheProtocol.cmd_SERVER_ERROR(  sB     w- 7*+mm##%W%&r   c                X    V P                   P                  4       P                  R4       R# )z.
A delete command has completed successfully.
TNrf   rH   s   &r   cmd_DELETEDMemCacheProtocol.cmd_DELETED1  ri   r   c                X    V P                   P                  4       P                  R4       R# )z&
The last command has been completed.
TNrf   rH   s   &r   cmd_OKMemCacheProtocol.cmd_OK7  ri   r   c                X    V P                   P                  4       P                  R4       R# )z%
A C{checkAndSet} update has failed.
FNrf   rH   s   &r   
cmd_EXISTSMemCacheProtocol.cmd_EXISTS=  rz   r   c                8   V P                  4        VP                  R^4      ^ ,          p\        V R\        V4      ,           R4      pVe:   VP                  R^4      R,          pV'       d   V! V^ ,          4       MyV! 4        MqVP	                  RR4      p\        V R\        V4      ,           R4      pVe	   V! 4        M6V P
                  P                  4       p\        V4      pVP                  V4       V P
                  '       g   V P                  R4       R# R# )z(
Receive line commands from the server.
r   cmd_N:   NN   _)
rU   r|   getattrr	   replacer8   r@   r}   r,   rP   )r#   rR   tokenrB   argsr`   s   &&    r   lineReceivedMemCacheProtocol.lineReceivedC  s     	

4#A&dF\%%88$??::dA&r*DDG <<d+D$d); ;TBC mm++-$iC }}}OOD! r   c                &    V P                  RW4      # )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#   rt   r`   s   &&&r   	incrementMemCacheProtocol.increment`  s     ~~gs00r   c                &    V P                  RW4      # )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                   V P                   '       d   \        \        R4      4      # \        V\        4      '       g"   \        \        R\        V4       R24      4      # \        V4      V P                  8  d   \        \        R4      4      # RP                  WR\        V4      3,          .4      pV P                  V4       \        WR7      pV P                  P                  V4       VP                  # )z!
Internal wrapper for incr/decr.
not connectedInvalid type for key: , expecting bytesKey too longr   s   %drt   )rL   r   rr   
isinstancebytesr   typerW   MAX_KEY_LENGTHrX   r}   rQ   r   r8   rV   r    )r#   rB   rt   r`   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                *    V P                  RWW4R4      # )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#   rt   r`   ra   
expireTimes   &&&&&r   r   MemCacheProtocol.replace  s    * yySu#FFr   c                *    V P                  RWW4R4      # )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                *    V P                  RWW4R4      # )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                *    V P                  RWWEV4      # )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#   rt   r`   rb   ra   r   s   &&&&&&r   checkAndSetMemCacheProtocol.checkAndSet  s    4 yy5cBBr   c           
        V P                   '       d   \        \        R4      4      # \        V\        4      '       g"   \        \        R\        V4       R24      4      # \        V4      V P                  8  d   \        \        R4      4      # \        V\        4      '       g"   \        \        R\        V4       R24      4      # V'       d
   RV,           p\        V4      pRP                  W\        RWEV3,          4      .4      V,           pV P                  V4       V P                  V4       \        WWGR7      p	V P                  P                  V	4       V	P                  # )	z&
Internal wrapper for setting values.
r   r   r   r   zInvalid type for value: r   z%d %d %d)rt   ra   r   )rL   r   rr   r   r   r   r   rW   r   rX   r
   rQ   r   r8   rV   r    )
r#   rB   rt   r`   ra   r   rb   r   r   r   s
   &&&&&&&   r   r   MemCacheProtocol._set  s3    _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                ,    V P                  RW^ ^ R4      # )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   s   &&&r   rV   MemCacheProtocol.append  s      yyCaC88r   c                ,    V P                  RW^ ^ R4      # )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   s   &&&r   prependMemCacheProtocol.prepend'  s      yySq!S99r   c                *    V P                  V.VR4      # )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#   rt   withIdentifiers   &&&r   getMemCacheProtocol.get9  s    , yy#66r   c                &    V P                  WR4      # )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           	        \        V4      pV P                  '       d   \        \        R4      4      # V Fl  p\	        V\
        4      '       g$   \        \        R\        V4       R24      4      u # \        V4      V P                  8  g   KX  \        \        R4      4      u # 	  V'       d   RpMRpRP                  V.V,           4      pV P                  V4       V'       d!   V Uu/ uF  qDRbK  	  pp\        WQVR
R7      pM\        WQ^ ,          R	^ RRR7      pV P                  P                  V4       VP                  # u upi )z.
Helper method for C{get} and C{getMultiple}.
r   r   r   r   rp   rn   r   r   NT)r~   rZ   rY   F)rt   r+   ra   rb   rY   )    r   N)listrL   r   rr   r   r   r   r   rW   r   rX   rQ   r   r8   rV   r    )	r#   r~   r   rY   rt   rB   r   rZ   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   'ENc                    V'       d   RV,           pMRpV P                   '       d   \        \        R4      4      # V P                  V4       \	        R/ R7      pV P
                  P                  V4       VP                  # )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 rq   r   )rZ   rL   r   rr   rQ   r   r8   rV   r    )r#   argrB   r   s   &&  r   statsMemCacheProtocol.stats  sg     c/CC_566c"-V$r   c                    V P                   '       d   \        \        R4      4      # V P                  R4       \	        R4      pV P
                  P                  V4       VP                  # )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  sT     _566j!$V$r   c                T   V P                   '       d   \        \        R4      4      # \        V\        4      '       g"   \        \        R\        V4       R24      4      # V P                  RV,           4       \        RVR7      pV P                  P                  V4       VP                  # )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   )rL   r   rr   r   r   r   r   rQ   r   r8   rV   r    )r#   rt   r   s   && r   deleteMemCacheProtocol.delete  s     _566#u%%4T#YK?PQR  	j3&',V$r   c                    V P                   '       d   \        \        R4      4      # V P                  R4       \	        R4      pV P
                  P                  V4       VP                  # )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  sT     _566l#&V$r   )r;   r8   rL   r:   r9   r<   r=   )<   )r   )r   r   )F)N).r   r   r   r   r   r   rL   r'   rC   rI   rM   rQ   rc   rg   rk   ru   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rV   r   r   r   r   r   r   r   r   r   r2   r3   s   @r   r6   r6   c   s     2 NM8(2*"../2/25"''../":1"1$ $G.C.C.C8 >9$:$7054 < 2  ,   r   r6   N)r6   DEFAULT_PORTr   r   r   )r   collectionsr   twisted.internet.deferr   r   r   twisted.protocols.basicr   twisted.protocols.policiesr   twisted.pythonr   twisted.python.compatr	   r
   r   	Exceptionr   r   r   r   r6   __all__r   r   r   <module>r      sm   
0  ? ? 0 3  =I ) ) && &&Rq	 |\ q	 hr   