HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/twisted/protocols/__pycache__/memcache.cpython-38.pyc
U


W[�\�@s�dZddlmZmZddlmZddlmZddlm	Z	ddl
mZmZm
Z
ddlmZddlmZmZmZmZd	ZGd
d�de�ZGdd
�d
e�ZGdd�de�ZGdd�de�ZGdd�dee	�Zdddd
dgZdS)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.
�)�absolute_import�division)�deque)�LineReceiver)�TimeoutMixin)�Deferred�fail�TimeoutError)�log)�
intToBytes�	iteritems�nativeString�
networkStringi�+c@seZdZdZdS)�
NoSuchCommandzA
    Exception raised when a non existent command is called.
    N��__name__�
__module__�__qualname__�__doc__�rr�</usr/lib/python3/dist-packages/twisted/protocols/memcache.pyr-src@seZdZdZdS)�ClientErrorz1
    Error caused by an invalid client call.
    Nrrrrrr4src@seZdZdZdS)�ServerErrorz*
    Problem happening on the server.
    Nrrrrrr;src@s(eZdZdZdd�Zdd�Zdd�ZdS)	�Commanda8
    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}
    cKs0||_t�|_|��D]\}}t|||�qdS)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�vrrr�__init__Os
zCommand.__init__cCs|j�|�dS)zB
        Shortcut method to fire the underlying deferred.
        N)r�callback)r�valuerrr�success_szCommand.successcCs|j�|�dS)z5
        Make the underlying deferred fails.
        N)rZerrback)r�errorrrrrfszCommand.failN)rrrrr"r%rrrrrrBsrc@sTeZdZdZdZdZdRdd�Zdd�Zd	d
�Zdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�ZdSd.d/�ZdTd0d1�Zd2d3�ZdUd5d6�ZdVd7d8�ZdWd9d:�ZdXd;d<�Z d=d>�Z!d?d@�Z"dAdB�Z#dYdCdD�Z$dZdEdF�Z%dGdH�Z&d[dJdK�Z'dLdM�Z(dNdO�Z)dPdQ�Z*dIS)\�MemCacheProtocola1
    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}
    �F�<cCs*t�|_d|_d|_d|_||_|_dS)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)rr/rrrr"�s
zMemCacheProtocol.__init__cCs |jr|j��}|�|�qdS)zW
        Cancel all the outstanding commands, making them fail with C{reason}.
        N)r*�popleftr)r�reason�cmdrrr�_cancelCommands�s
z MemCacheProtocol._cancelCommandscCs|�td��|j��dS)z:
        Close the connection in case of timeout.
        zConnection timeoutN)r3r	Z	transportZloseConnection�rrrr�timeoutConnection�sz"MemCacheProtocol.timeoutConnectioncCs d|_|�|�t�||�dS)z9
        Cause any outstanding commands to fail.
        TN)�
_disconnectedr3r�connectionLost)rr1rrrr7�s
zMemCacheProtocol.connectionLostcCs"|js|�|j�t�||�dS)zA
        Override sendLine to add a timeout to response.
        N)r*�
setTimeoutr.r�sendLine)r�linerrrr9�szMemCacheProtocol.sendLinecCs�|��|j�|�|jt|�7_|j|jdkr�d�|j�}|d|j�}||jdd�}|}d|_d|_d|_|jd}|jr�|j	|j
\}}|||f|j	|j
<n||_|�|�dS)z)
        Collect data for a get.
        ��Nr)
�resetTimeoutr,�appendr-�lenr+�joinr*�multiple�values�
currentKeyr$ZsetLineMode)r�dataZbufZrem�valr2�flags�casrrr�rawDataReceived�s"
z MemCacheProtocol.rawDataReceivedcCs|j���d�dS)z?
        Manage a success response to a set operation.
        TN�r*r0r%r4rrr�
cmd_STORED�szMemCacheProtocol.cmd_STOREDcCs|j���d�dS)z�
        Manage a specific 'not stored' response to a set operation: this is not
        an error, but some condition wasn't met.
        FNrIr4rrr�cmd_NOT_STORED�szMemCacheProtocol.cmd_NOT_STOREDcCs�|j��}|jdkrN|jr:dd�t|j�D�}|�|�q�|�|j|jf�nb|jdkr�|jrl|�|j�q�|�|j|j	|jf�n,|jdkr�|�|j�nt
dt|j�f��dS)zB
        This the end token to a get or a stat operation.
        �getcSs i|]\}}||ddd��qS)Nr;r)�.0�keyrErrr�
<dictcomp>�sz,MemCacheProtocol.cmd_END.<locals>.<dictcomp>�gets�statsz%Unexpected END response to %s commandN)r*r0rrArrBr%rFr$rG�RuntimeErrorr
)rr2rBrrr�cmd_END�s"




��zMemCacheProtocol.cmd_ENDcCs|j���d�dS)z=
        Manage error response for incr/decr/delete.
        FNrIr4rrr�
cmd_NOT_FOUND�szMemCacheProtocol.cmd_NOT_FOUNDcCs�|jd}|jdkr(|��\}}}d}n|��\}}}}t|�|_g|_d|_|jr�||jkrft	d��||_
t|�|g|j|<n"|j|kr�t	d��t|�|_
||_|��dS)z:
        Prepare the reading a value after a get.
        rrLr<zUnexpected commands answer.N)r*r�split�intr+r,r-rA�keysrRrCrBrNrFrGZ
setRawMode)rr:r2rNrF�lengthrGrrr�	cmd_VALUEs$





zMemCacheProtocol.cmd_VALUEcCs(|jd}|�dd�\}}||j|<dS)z-
        Reception of one stat line.
        r� �N)r*rUrB)rr:r2rNrErrr�cmd_STATs
zMemCacheProtocol.cmd_STATcCs|j���|�dS)z%
        Read version token.
        NrI)rZversionDatarrr�cmd_VERSION%szMemCacheProtocol.cmd_VERSIONcCs$t�d�|j��}|�t��dS)z7
        A non-existent command has been sent.
        zNon-existent command sent.N)r
�errr*r0rr)rr2rrr�	cmd_ERROR,s

zMemCacheProtocol.cmd_ERRORcCs2t|�}t�d|�|j��}|�t|��dS)z0
        An invalid input as been sent.
        zInvalid input: N)�reprr
r^r*r0rr�rZerrTextr2rrr�cmd_CLIENT_ERROR5s
z!MemCacheProtocol.cmd_CLIENT_ERRORcCs2t|�}t�d|�|j��}|�t|��dS)z4
        An error has happened server-side.
        zServer error: N)r`r
r^r*r0rrrarrr�cmd_SERVER_ERROR?s
z!MemCacheProtocol.cmd_SERVER_ERRORcCs|j���d�dS)z>
        A delete command has completed successfully.
        TNrIr4rrr�cmd_DELETEDIszMemCacheProtocol.cmd_DELETEDcCs|j���d�dS)z6
        The last command has been completed.
        TNrIr4rrr�cmd_OKPszMemCacheProtocol.cmd_OKcCs|j���d�dS)z5
        A C{checkAndSet} update has failed.
        FNrIr4rrr�
cmd_EXISTSWszMemCacheProtocol.cmd_EXISTScCs�|��|�dd�d}t|dt|�d�}|dk	rb|�dd�dd�}|rZ||d�q�|�nL|�dd�}t|dt|�d�}|dk	r�|�n|j��}t|�}|�|�|js�|�	d�dS)z8
        Receive line commands from the server.
        rZr[rZcmd_N�_)
r=rU�getattrr
�replacer*r0rVr%r8)rr:�tokenr2�argsrErrr�lineReceived^s"

zMemCacheProtocol.lineReceivedr[cCs|�d||�S)a�
        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}
        sincr��	_incrdecr�rrNrErrr�	increment|szMemCacheProtocol.incrementcCs|�d||�S)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}
        sdecrrmrorrr�	decrement�szMemCacheProtocol.decrementcCs�|jrttd��St|t�s2ttdt|�f��St|�|jkrLttd��Sd�	||t
t|��g�}|�|�t
||d�}|j�|�|jS)z1
        Internal wrapper for incr/decr.
        �
not connected�)Invalid type for key: %s, expecting bytes�Key too longrZ�rN)r6rrR�
isinstance�bytesr�typer?�MAX_KEY_LENGTHr@rrVr9rr*r>r)rr2rNrE�fullcmd�cmdObjrrrrn�s
�
zMemCacheProtocol._incrdecrrcCs|�d||||d�S)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}
        sreplacer<��_set�rrNrErF�
expireTimerrrri�szMemCacheProtocol.replacecCs|�d||||d�S)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}
        saddr<r|r~rrr�add�szMemCacheProtocol.addcCs|�d||||d�S)a9
        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}
        ssetr<r|r~rrr�set�szMemCacheProtocol.setcCs|�d|||||�S)am
        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}
        scasr|)rrNrErGrFrrrr�checkAndSet�szMemCacheProtocol.checkAndSetc
	Cs�|jrttd��St|t�s2ttdt|�f��St|�|jkrLttd��St|t�slttdt|�f��S|rxd|}t|�}d�	||t
d|||f�g�|}|�|�|�|�t||||d�}	|j
�|	�|	jS)z6
        Internal wrapper for setting values.
        rrrsrtz+Invalid type for value: %s, expecting bytesrZz%d %d %d)rNrFrX)r6rrRrvrwrrxr?ryr@rr9rr*r>r)
rr2rNrErFrrGrXrzr{rrrr}s:
�
����

zMemCacheProtocol._setcCs|�d||ddd�S)a�
        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}
        sappendrr<r|rorrrr>4szMemCacheProtocol.appendcCs|�d||ddd�S)a�
        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}
        sprependrr<r|rorrr�prependGszMemCacheProtocol.prependcCs|�|g|d�S)a�
        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)rrN�withIdentifierrrr�getZszMemCacheProtocol.getcCs|�||d�S)a
        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�)rrWr�rrr�getMultiplesszMemCacheProtocol.getMultiplec	Cs�t|�}|jrttd��S|D]F}t|t�sFttdt|�f��St|�|j	krttd��Sq|rpd}nd}d�
|g|�}|�|�|r�tdd�|D��}t
|||d	d
�}nt
||dddd
dd�}|j�|�|jS)z>
        Helper method for C{get} and C{getMultiple}.
        rrrsrtrPrLrZcSsg|]}|df�qS))rr<Nr)rMrNrrr�
<listcomp>�sz)MemCacheProtocol._get.<locals>.<listcomp>T)rWrBrArNr<F)rNr$rFrGrA)�listr6rrRrvrwrrxr?ryr@r9�dictrr*r>r)	rrWr�rArNr2rzrBr{rrrr��s2
��

�zMemCacheProtocol._getNcCsL|rd|}nd}|jr$ttd��S|�|�tdid�}|j�|�|jS)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}
        sstats rQrr)rB�r6rrRr9rr*r>r)r�argr2r{rrr�stats�s

zMemCacheProtocol.statscCs6|jrttd��S|�d�td�}|j�|�|jS)z�
        Get the version of the server.

        @return: a deferred that will fire with the string value of the
            version.
        @rtype: L{Deferred}
        rrsversionr��rr{rrr�version�s
zMemCacheProtocol.versioncCs^|jrttd��St|t�s2ttdt|�f��S|�d|�td|d�}|j	�
|�|jS)a
        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}
        rrrssdelete sdeleteru)r6rrRrvrwrrxr9rr*r>r)rrNr{rrr�delete�s
�zMemCacheProtocol.deletecCs6|jrttd��S|�d�td�}|j�|�|jS)z�
        Flush all cached values.

        @return: a deferred that will be called back with C{True} when the
            operation has succeeded.
        @rtype: L{Deferred}
        rrs	flush_allr�r�rrr�flushAll�s
zMemCacheProtocol.flushAll)r))r[)r[)rr)rr)rr)rr)F)F)N)+rrrrryr6r"r3r5r7r9rHrJrKrSrTrYr\r]r_rbrcrdrerfrlrprqrnrir�r�r�r}r>r�r�r�r�r�r�r�r�rrrrr'nsP
					










r'�DEFAULT_PORTN)rZ
__future__rr�collectionsrZtwisted.protocols.basicrZtwisted.protocols.policiesrZtwisted.internet.deferrrr	Ztwisted.pythonr
Ztwisted.python.compatrrr
rr��	Exceptionrrr�objectrr'�__all__rrrr�<module>s*,�