%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3/dist-packages/twisted/protocols/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3/dist-packages/twisted/protocols/__pycache__/memcache.cpython-312.pyc

�

Ϫ�fc\����dZddlmZddlmZmZmZddlmZddl	m
Z
ddlmZddl
mZmZdZGd	�d
e�ZGd�de�ZGd
�de�ZGd�d�ZGd�dee
�Zgd�Zy)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��eZdZdZy)�
NoSuchCommandzA
    Exception raised when a non existent command is called.
    N��__name__�
__module__�__qualname__�__doc__���</usr/lib/python3/dist-packages/twisted/protocols/memcache.pyr
r
(���rr
c��eZdZdZy)�ClientErrorz1
    Error caused by an invalid client call.
    Nrrrrrr.rrrc��eZdZdZy)�ServerErrorz*
    Problem happening on the server.
    Nrrrrrr4rrrc�"�eZdZdZd�Zd�Zd�Zy)�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}
    c�z�||_t�|_|j�D]\}}t	|||��y)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__zCommand.__init__Gs9�����!�����L�L�N�	 �D�A�q��D�!�Q��	 rc�:�|jj|�y)zB
        Shortcut method to fire the underlying deferred.
        N)r�callback)r"�values  r�successzCommand.successVs��	
������&rc�:�|jj|�y)z5
        Make the underlying deferred fails.
        N)r�errback)r"�errors  rrzCommand.fail\s��	
�����u�%rN)rrrrr&r*rrrrrr:s��
�
 �'�&rrc�
�eZdZdZdZdZd*d�Zd�Zd�Zd�Z	d�Z
d	�Zd
�Zd�Z
d�Zd
�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Zd+d�Zd+d�Zd�Zd,d�Zd,d�Zd,d�Zd,d�Z d�Z!d �Z"d!�Z#d-d"�Z$d-d#�Z%d$�Z&d.d&�Z'd'�Z(d(�Z)d)�Z*y%)/�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}
    �Fc�h�t�|_d|_d|_d|_|x|_|_y)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"r7s  rr&zMemCacheProtocol.__init__�s3�����
� ������!���07�7����rc��|jr9|jj�}|j|�|jr�8yy)zW
        Cancel all the outstanding commands, making them fail with C{reason}.
        N)r2�popleftr)r"�reason�cmds   r�_cancelCommandsz MemCacheProtocol._cancelCommands�s2���m�m��-�-�'�'�)�C��H�H�V���m�mrc�l�|jtd��|jj�y)z:
        Close the connection in case of timeout.
        zConnection timeoutN)r<r�	transport�loseConnection�r"s r�timeoutConnectionz"MemCacheProtocol.timeoutConnection�s(��	
���\�*>�?�@����%�%�'rc�`�d|_|j|�tj||�y)z9
        Cause any outstanding commands to fail.
        TN)�
_disconnectedr<r�connectionLost)r"r:s  rrDzMemCacheProtocol.connectionLost�s*��"������V�$��#�#�D�&�1rc�~�|js|j|j�tj||�y)zA
        Override sendLine to add a timeout to response.
        N)r2�
setTimeoutr6r�sendLine)r"�lines  rrGzMemCacheProtocol.sendLine�s-���}�}��O�O�D�2�2�3����d�D�)rc�N�|j�|jj|�|xjt	|�z
c_|j|j
dzk\r�dj
|j�}|d|j
}||j
dzd}|}d|_d|_d|_|jd}|jr9|j|j\}}|||f|j|j<n||_|j|�yy)z)
        Collect data for a get.
        �rNr)
�resetTimeoutr4�appendr5�lenr3�joinr2�multiple�values�
currentKeyr)�setLineMode)r"�data�buf�rem�valr;�flags�cass        r�rawDataReceivedz MemCacheProtocol.rawDataReceived�s��	
���������t�$����c�$�i�'������!2�!2�Q�!6�6��8�8�D�O�O�,�D��*��*�*�+�C��t�(�(�1�,�.�/�C��C� $�D��"�D�O�!%�D���-�-��"�C��|�|� �Z�Z����7�
��s�.3�S�#�->��
�
�3�>�>�*���	����S�!�7rc�V�|jj�jd�y)z?
        Manage a success response to a set operation.
        TN�r2r9r*r@s r�
cmd_STOREDzMemCacheProtocol.cmd_STORED����	
�
�
����'�'��-rc�V�|jj�jd�y)z�
        Manage a specific 'not stored' response to a set operation: this is not
        an error, but some condition wasn't met.
        FNr[r@s r�cmd_NOT_STOREDzMemCacheProtocol.cmd_NOT_STORED�s��
	
�
�
����'�'��.rc	���|jj�}|jdk(ry|jrE|jj�D��cic]\}}||ddd���}}}|j
|�y|j
|j|jf�y|jdk(r[|jr|j
|j�y|j
|j|j|jf�y|jdk(r|j
|j�ytdjt|j����cc}}w)zB
        This the end token to a get or a stat operation.
        �getNrJ�gets�statsz%Unexpected END response to {} command)
r2r9rrOrPr r*rWr)rX�RuntimeError�formatr
)r"r;�keyrVrPs     r�cmd_ENDzMemCacheProtocol.cmd_END�s	���m�m�#�#�%���;�;�&� ��|�|�8;�
�
�8H�8H�8J�K�H�C��#�s�3�Q�3�x�-�K��K����F�#����S�Y�Y��	�	�2�3�
�[�[�G�
#��|�|����C�J�J�'����S�Y�Y�������;�<�
�[�[�H�
$��K�K��
�
�#��7�>�>� ����-���
��Ls�E%c�V�|jj�jd�y)z=
        Manage error response for incr/decr/delete.
        FNr[r@s r�
cmd_NOT_FOUNDzMemCacheProtocol.cmd_NOT_FOUND����	
�
�
����'�'��.rc���|jd}|jdk(r|j�\}}}d}n|j�\}}}}t|�|_g|_d|_|jr;||jvrtd��||_
t|�|g|j|<n1|j|k7rtd��t|�|_
||_|j�y)z:
        Prepare the reading a value after a get.
        rrarzUnexpected commands answer.N)r2r�split�intr3r4r5rO�keysrdrQrPrfrWrX�
setRawMode)r"rHr;rfrW�lengthrXs       r�	cmd_VALUEzMemCacheProtocol.cmd_VALUE�s����m�m�A����;�;�&� �!%�����C����C�&*�j�j�l�#�C������K����������<�<��#�(�(�"�"�#@�A�A� �C�N�"�5�z�3�/�C�J�J�s�O��w�w�#�~�"�#@�A�A��E�
�C�I��C�G����rc�j�|jd}|jdd�\}}||j|<y)z-
        Reception of one stat line.
        r� �N)r2rlrP)r"rHr;rfrVs     r�cmd_STATzMemCacheProtocol.cmd_STAT	s3���m�m�A����:�:�d�A�&���S���
�
�3�rc�V�|jj�j|�y)z%
        Read version token.
        Nr[)r"�versionDatas  r�cmd_VERSIONzMemCacheProtocol.cmd_VERSIONs��	
�
�
����'�'��4rc��tjd�|jj�}|j	t��y)z7
        A non-existent command has been sent.
        zNon-existent command sent.N)r	�errr2r9rr
)r"r;s  r�	cmd_ERRORzMemCacheProtocol.cmd_ERRORs1��	���,�-��m�m�#�#�%�������!rc��t|�}tjd|z�|jj	�}|jt
|��y)z0
        An invalid input as been sent.
        zInvalid input: N)�reprr	rzr2r9rr�r"�errTextr;s   r�cmd_CLIENT_ERRORz!MemCacheProtocol.cmd_CLIENT_ERRORsB���w�-�����!�G�+�,��m�m�#�#�%������W�%�&rc��t|�}tjd|z�|jj	�}|jt
|��y)z4
        An error has happened server-side.
        zServer error: N)r}r	rzr2r9rrr~s   r�cmd_SERVER_ERRORz!MemCacheProtocol.cmd_SERVER_ERROR(sB���w�-����� �7�*�+��m�m�#�#�%������W�%�&rc�V�|jj�jd�y)z>
        A delete command has completed successfully.
        TNr[r@s r�cmd_DELETEDzMemCacheProtocol.cmd_DELETED1r]rc�V�|jj�jd�y)z6
        The last command has been completed.
        TNr[r@s r�cmd_OKzMemCacheProtocol.cmd_OK7r]rc�V�|jj�jd�y)z5
        A C{checkAndSet} update has failed.
        FNr[r@s r�
cmd_EXISTSzMemCacheProtocol.cmd_EXISTS=rjrc���|j�|jdd�d}t|dt|�zd�}|�+|jdd�dd}|r||d�ns|�nk|j	dd�}t|dt|�zd�}|�|�n6|j
j
�}t|�}|j|�|j
s|jd�yy)z8
        Receive line commands from the server.
        rsrtr�cmd_N�_)
rKrl�getattrr
�replacer2r9rmr*rF)r"rH�tokenr;�argsrVs      r�lineReceivedzMemCacheProtocol.lineReceivedCs���	
�����
�
�4��#�A�&���d�F�\�%�%8�8�$�?���?��:�:�d�A�&�q�r�*�D���D��G�����<�<��d�+�D��$���d�);� ;�T�B�C������m�m�+�+�-���$�i�����C� ��}�}��O�O�D�!�rc�(�|jd||�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�r"rfrVs   r�	incrementzMemCacheProtocol.increment`s���~�~�g�s�C�0�0rc�(�|jd||�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}
        sdecrr�r�s   r�	decrementzMemCacheProtocol.decrementqs�� �~�~�g�s�C�0�0rc���|jrttd��St|t�s!ttdt
|��d���St|�|jkDrttd��Sdj||dt|�fzg�}|j|�t||��}|jj|�|jS)z1
        Internal wrapper for incr/decr.
        �
not connected�Invalid type for key: �, expecting bytes�Key too longrss%d�rf)rCrrd�
isinstance�bytesr�typerM�MAX_KEY_LENGTHrNrmrGrr2rLr)r"r;rfrV�fullcmd�cmdObjs      rr�zMemCacheProtocol._incrdecr�s��������_�5�6�6��#�u�%���4�T�#�Y�K�?P�Q�R��
��s�8�d�)�)�)���N�3�4�4��)�)�S�#�u��C��{�':�;�<���
�
�g����#�&���
�
���V�$����rc�.�|jd||||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�r"rfrVrW�
expireTimes     rr�zMemCacheProtocol.replace�s��*�y�y��S�#�u�j�#�F�Frc�.�|jd||||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}
        saddrr�r�s     r�addzMemCacheProtocol.add����*�y�y���c�5�*�c�B�Brc�.�|jd||||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}
        ssetrr�r�s     r�setzMemCacheProtocol.set�r�rc�.�|jd|||||�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�)r"rfrVrXrWr�s      r�checkAndSetzMemCacheProtocol.checkAndSet�s��4�y�y���c�5�*�c�B�Brc
�~�|jrttd��St|t�s!ttdt
|��d���St|�|jkDrttd��St|t�s!ttdt
|��d���S|rd|z}t|�}dj||td|||fz�g�|z}|j|�|j|�t||||��}	|jj|	�|	jS)	z6
        Internal wrapper for setting values.
        r�r�r�r�zInvalid type for value: rsz%d %d %d)rfrWrp)rCrrdr�r�rr�rMr�rNrrGrr2rLr)
r"r;rfrVrWr�rXrpr�r�s
          rr�zMemCacheProtocol._set�s2�������_�5�6�6��#�u�%���4�T�#�Y�K�?P�Q�R��
��s�8�d�)�)�)���N�3�4�4��#�u�%���6�t�C�y�k�AR�S�T��
����*�C��S����I�I��c�=��u�j�&�6Q�)Q�R�S�
��
�	�	
�
�
�g���
�
�c����#�U�6�B���
�
���V�$����rc�.�|jd||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}
        sappendrrr�r�s   rrLzMemCacheProtocol.appends�� �y�y��C��a��C�8�8rc�.�|jd||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}
        sprependrrr�r�s   r�prependzMemCacheProtocol.prepend's�� �y�y��S�#�q�!�S�9�9rc�*�|j|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)r"rf�withIdentifiers   r�getzMemCacheProtocol.get9s��,�y�y�#����6�6rc�(�|j||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�)r"rnr�s   r�getMultiplezMemCacheProtocol.getMultipleQs��0�y�y��~�t�4�4rc	�F�t|�}|jrttd��S|D]c}t	|t
�s#tt
dt|��d���cSt|�|jkDs�Ott
d��cS|rd}nd}dj|g|z�}|j|�|r |D�cic]}|d��}}t|||d	�
�}nt||dddd
d��}|jj|�|jScc}w)z>
        Helper method for C{get} and C{getMultiple}.
        r�r�r�r�rbrars)rrNT)rnrPrOrNrF)rfr)rWrXrO)�listrCrrdr�r�rr�rMr�rNrGrr2rLr)	r"rnr�rOrfr;r�rPr�s	         rr�zMemCacheProtocol._getks!���D�z�������_�5�6�6��	9�C��c�5�)���"8��c���CT� U�V����3�x�$�-�-�-��K��7�8�8�
	9���C��C��)�)�S�E�D�L�)���
�
�g���59�:�c�c�>�)�:�F�:��S�t�F�T�J�F����a���A�3���F�	
�
�
���V�$������;s�
DNc���|rd|z}nd}|jrttd��S|j|�t	di��}|j
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 rcr�)rP�rCrrdrGrr2rLr)r"�argr;r�s    r�statszMemCacheProtocol.stats�sd����c�/�C��C������_�5�6�6��
�
�c����"�-���
�
���V�$����rc���|jrttd��S|jd�t	d�}|j
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}
        r�sversionr��r"r�s  r�versionzMemCacheProtocol.version�sQ�������_�5�6�6��
�
�j�!���$���
�
���V�$����rc�4�|jrttd��St|t�s!ttdt
|��d���S|jd|z�td|��}|jj|�|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}
        r�r�r�sdelete sdeleter�)rCrrdr�r�rr�rGrr2rLr)r"rfr�s   r�deletezMemCacheProtocol.delete�s��������_�5�6�6��#�u�%���4�T�#�Y�K�?P�Q�R��
�	
�
�
�j�3�&�'����,���
�
���V�$����rc���|jrttd��S|jd�t	d�}|j
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}
        r�s	flush_allr�r�s  r�flushAllzMemCacheProtocol.flushAll�sQ�������_�5�6�6��
�
�l�#���&���
�
���V�$����r)�<)rt)rr)F)N)+rrrrr�rCr&r<rArDrGrYr\r_rgrirqrurxr{r�r�r�r�r�r�r�r�r�r�r�r�r�r�rLr�r�r�r�r�r�r�r�rrrr/r/cs����2�N��M�8��(�2�*�"�..�/��2/��2�5�"�'�'�.�.�/�"�:1�"1�$ �$G�.C�.C�.C�8 �>9�$:�$7�05�4 �< �2
 � �,
 rr/)r/�DEFAULT_PORTr
rrN)r�collectionsr�twisted.internet.deferrrr�twisted.protocols.basicr�twisted.protocols.policiesr�twisted.pythonr	�twisted.python.compatr
rr��	Exceptionr
rrrr/�__all__rrr�<module>r�sm��
�0�?�?�0�3��=����I���)���)��&&�&&�Rq	 �|�\�q	 �h�r

Zerion Mini Shell 1.0