U
    be                     @   s  U d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZ d	Zd
d
d
d
d
ddZdhZdddddZ eee!e"f e!f Z#ee$e!f Z%e$e&dddZ'e$e&dddZ(e$e)dddZ*e$e"dddZ+e$e*e*e'e'e$e)e$e+e'e'e(e'e'e'dZ,ee$ee$ge
f f e-d< d5e%e&e$e$dddZ.e#e#d d!d"Z/G d#d$ d$Z0G d%d& d&Z1G d'd( d(Z2eje$ee$e$f d)d*d+Z3eje$e"d,d-d.Z4eje$e$ee$e$f d/d0d1Z5eje"e$d2d3d4Z6dS )6    N)partial)
SSLContext)
ModuleType)AnyCallableDictIterableListOptionalTupleUnion)pool)MemcacheClientErrorMemcacheIllegalInputErrorMemcacheServerErrorMemcacheUnexpectedCloseErrorMemcacheUnknownCommandErrorMemcacheUnknownError)LegacyWrappingSerdei   )   STORED
   NOT_STORED)r      EXISTS	   NOT_FOUND)   set   add   replace   append   prepend   casLinuxTF)r   r   r   r   )valuereturnc                 C   s   t | dkS )Nr   intr     r%   X/var/www/html/services/stratfitenv/lib/python3.8/site-packages/pymemcache/client/base.py_parse_bool_int=   s    r'   c                 C   s   | dkS )Ns   yesr%   r$   r%   r%   r&   _parse_bool_string_is_yesA   s    r(   c                 C   s   t | ddS )N   :   .)floatreplacer$   r%   r%   r&   _parse_floatE   s    r-   c                 C   s
   t | dS )N   r"   r$   r%   r%   r&   
_parse_hexI   s    r/   )   versions   rusage_users   rusage_systems   hash_is_expandings   slab_reassign_runnings   inters   growth_factors   stat_key_prefixs   umasks   detail_enableds   cas_enableds   auth_enabled_sasls   maxconns_fasts   slab_reassigns   slab_automove
STAT_TYPES    )keyallow_unicode_keys
key_prefixr!   c              	   C   s   |rt | trX| d} n>t | trXz| d} W n$ ttfk
rV   td|  Y nX ||  } |  }t| dkrtd|  n>t|dks|r|d | krtd|  nd	| krtd
|  | S )Checks key and add key_prefix.utf8asciizNon-ASCII key: %r   zKey is too long: %r   r   zKey contains whitespace: %r    zKey contains null: %r)
isinstancestrencodeUnicodeEncodeErrorUnicodeDecodeErrorr   splitlen)r3   r4   r5   partsr%   r%   r&   check_key_helperd   s"    

rD   )serverr!   c                 C   s   t | tr| S t | ts&td| | dr<| dd  S | drJ| S d| ks\| drh| d }}n"| dd}|d	 t|d  }}|d
r|d}||fS )Nz"Unsupported server specification: zunix:   /:]i+  r:   r   [z[])	r<   tupler=   
ValueError
startswithendswithrsplitr#   strip)rE   hostportrC   r%   r%   r&   normalize_server_spec   s    





rS   c                   @   s*   e Zd ZdZdZd	eeeddddZdS )
KeepaliveOptsa  
    A configuration structure to define the socket keepalive.

    This structure must be passed to a client. The client will configure
    its socket keepalive by using the elements of the structure.

    Args:
      idle: The time (in seconds) the connection needs to remain idle
        before TCP starts sending keepalive probes. Should be a positive
        integer most greater than zero.
      intvl: The time (in seconds) between individual keepalive probes.
        Should be a positive integer most greater than zero.
      cnt: The maximum number of keepalive probes TCP should send before
        dropping the connection. Should be a positive integer most greater
        than zero.
    )idleintvlcntr:   rF   N)rU   rV   rW   r!   c                 C   sF   |dk rt d|| _|dk r&t d|| _|dk r<t d|| _d S )Nr:   z1The idle parameter must be greater or equal to 1.z2The intvl parameter must be greater or equal to 1.z0The cnt parameter must be greater or equal to 1.)rL   rU   rV   rW   )selfrU   rV   rW   r%   r%   r&   __init__   s    zKeepaliveOpts.__init__)r:   r:   rF   )__name__
__module____qualname____doc__	__slots__r#   rY   r%   r%   r%   r&   rT      s   rT   c                   @   s$  e Zd ZdZdddddddeddddddfeee ee eee	ee
 eeeeee ddd	Zeeed
ddZddddZddddZeZdmeeeee ee ee dddZdneeef eee ee ee dddZeZdoeeeee ee edddZdpeeee ee edddZdqeeee ee edddZdreee ee d d!d"Zdseee ee d#d$d%Zdteee ed&d'd(Z e!e eeef d)d*d+Z"e"Z#dueeee$eef d,d-d.Z%e!e eee$eef f d)d/d0Z&dveee ed1d2d3Z'dwe!e ee ed4d5d6Z(e(Z)dxeeee ee d7d8d9Z*dyeeee ee d7d:d;Z+dzeeee ed<d=d>Z,d?d@ Z-eddAdBZ.eddCdDZ/d{e0eef e0eef edFdGdHZ1d|eee edIdJdKZ2dddLdMZ3d}eddNdOdPZ4eeddQdRdSZ5eeedTdUdVZ6e0eeef edWdXdYZ7eeeeeef ee e$ee0ee$eef f ef dZd[d\Z8d~ee!e eeeeef d]d^d_Z9deeeef eeee ee eeee f d`dadbZ:de!e eee ee dcdddeZ;edfdgdhZ<didj Z=dkdl Z>dS )Clienta  
    A client for a single memcached server.

    *Server Connection*

     The ``server`` parameter controls how the client connects to the memcached
     server. You can either use a (host, port) tuple for a TCP connection or a
     string containing the path to a UNIX domain socket.

     The ``connect_timeout`` and ``timeout`` parameters can be used to set
     socket timeout values. By default, timeouts are disabled.

     When the ``no_delay`` flag is set, the ``TCP_NODELAY`` socket option will
     also be set. This only applies to TCP-based connections.

     Lastly, the ``socket_module`` allows you to specify an alternate socket
     implementation (such as `gevent.socket`_).

     .. _gevent.socket: http://www.gevent.org/api/gevent.socket.html

    *Keys and Values*

     Keys must have a __str__() method which should return a str with no more
     than 250 ASCII characters and no whitespace or control characters. Unicode
     strings must be encoded (as UTF-8, for example) unless they consist only
     of ASCII characters that are neither whitespace nor control characters.

     Values must have a __str__() method to convert themselves to a byte
     string. Unicode objects can be a problem since str() on a Unicode object
     will attempt to encode it as ASCII (which will fail if the value contains
     code points larger than U+127). You can fix this with a serializer or by
     just calling encode on the string (using UTF-8, for instance).

     If you intend to use anything but str as a value, it is a good idea to use
     a serializer. The pymemcache.serde library has an already implemented
     serializer which pickles and unpickles data.

    *Serialization and Deserialization*

     The constructor takes an optional object, the "serializer/deserializer"
     ("serde"), which is responsible for both serialization and deserialization
     of objects. That object must satisfy the serializer interface by providing
     two methods: `serialize` and `deserialize`. `serialize` takes two
     arguments, a key and a value, and returns a tuple of two elements, the
     serialized value, and an integer in the range 0-65535 (the "flags").
     `deserialize` takes three parameters, a key, value, and flags, and returns
     the deserialized value.

     Here is an example using JSON for non-str values:

     .. code-block:: python

         class JSONSerde(object):
             def serialize(self, key, value):
                 if isinstance(value, str):
                     return value, 1
                 return json.dumps(value), 2

             def deserialize(self, key, value, flags):
                 if flags == 1:
                     return value

                 if flags == 2:
                     return json.loads(value)

                 raise Exception("Unknown flags for value: {1}".format(flags))

    .. note::

     Most write operations allow the caller to provide a ``flags`` value to
     support advanced interaction with the server. This will **override** the
     "flags" value returned by the serializer and should therefore only be
     used when you have a complete understanding of how the value should be
     serialized, stored, and deserialized.

    *Error Handling*

     All of the methods in this class that talk to memcached can throw one of
     the following exceptions:

      * :class:`pymemcache.exceptions.MemcacheUnknownCommandError`
      * :class:`pymemcache.exceptions.MemcacheClientError`
      * :class:`pymemcache.exceptions.MemcacheServerError`
      * :class:`pymemcache.exceptions.MemcacheUnknownError`
      * :class:`pymemcache.exceptions.MemcacheUnexpectedCloseError`
      * :class:`pymemcache.exceptions.MemcacheIllegalInputError`
      * :class:`socket.timeout`
      * :class:`socket.error`

     Instances of this class maintain a persistent connection to memcached
     which is terminated when any of these exceptions are raised. The next
     call to a method on the object will result in a new connection being made
     to memcached.
    NFr2   Tr8   )rE   connect_timeouttimeoutno_delay
ignore_excsocket_modulesocket_keepaliver5   default_noreplyr4   encodingtls_contextc                 C   s   t || _|pt||| _|| _|| _|| _|| _|	| _|
| _	t
 }| j	dk	r|tkrttdj|dttdt| j	tstdd| _t|tr|d}t|tstd|| _|| _|| _|| _|| _dS )a  
        Constructor.

        Args:
          server: tuple(hostname, port) or string containing a UNIX socket path.
          serde: optional serializer object, see notes in the class docs.
          serializer: deprecated serialization function
          deserializer: deprecated deserialization function
          connect_timeout: optional float, seconds to wait for a connection to
            the memcached server. Defaults to "forever" (uses the underlying
            default socket timeout, which can be very long).
          timeout: optional float, seconds to wait for send or recv calls on
            the socket connected to memcached. Defaults to "forever" (uses the
            underlying default socket timeout, which can be very long).
          no_delay: optional bool, set the TCP_NODELAY flag, which may help
            with performance in some cases. Defaults to False.
          ignore_exc: optional bool, True to cause the "get", "gets",
            "get_many" and "gets_many" calls to treat any errors as cache
            misses. Defaults to False.
          socket_module: socket module to use, e.g. gevent.socket. Defaults to
            the standard library's socket module.
          socket_keepalive: Activate the socket keepalive feature by passing
            a KeepaliveOpts structure in this parameter. Disabled by default
            (None). This feature is only supported on Linux platforms.
          key_prefix: Prefix of key. You can use this as namespace. Defaults
            to b''.
          default_noreply: bool, the default value for 'noreply' as passed to
            store commands (except from cas, incr, and decr, which default to
            False).
          allow_unicode_keys: bool, support unicode (utf8) keys
          encoding: optional str, controls data encoding (defaults to 'ascii').

        Notes:
          The constructor does not make a connection to memcached. The first
          call to a method on the object will do that.
        Na-  Pymemcache's socket keepalive mechanism doesn't support your system ({user_system}). If you see this message it mean that you tried to configure your socket keepalive on an unsupported system. To fix the problem pass `socket_keepalive=False` or use a supported system. Supported systems are: {systems}z, )user_systemZsystemsa	  Unsupported keepalive options. If you see this message it means that you passed an unsupported object within the param `socket_keepalive`. To fix it please instantiate and pass to socket_keepalive a KeepaliveOpts object. That's the only supported type of structure.r8   key_prefix should be bytes.)rS   rE   r   serder`   ra   rb   rc   rd   re   platformsystem!SOCKET_KEEPALIVE_SUPPORTED_SYSTEMSystemErrorformatjoinsortedr<   rT   rL   sockr=   r>   bytes	TypeErrorr5   rf   r4   rg   rh   )rX   rE   rk   
serializerdeserializerr`   ra   rb   rc   rd   re   r5   rf   r4   rg   rh   ri   r%   r%   r&   rY     s>    6




zClient.__init__)r3   r5   r!   c                 C   s   t || j|dS r6   )r4   r5   )rD   r4   )rX   r3   r5   r%   r%   r&   	check_keys  s
      zClient.check_keyr!   c                 C   s  |    | j}t| jts2| j}||j|j}nd }d }| j\}}||||j	|j|j
}|D ]\}}	}
}}zD|||	|
}| jr||j
|jd | jr| j}|j||d}W n: tk
r } z|}|d k	r|   d }W 5 d }~X Y q`X  qq`|d k	r|z|| j | jd k	rp|tjtjd |tj
tj| jj |tj
tj| jj |tj
tj| jj || || j W n  tk
r   |    Y nX || _d S )Nr:   )server_hostname)closerd   r<   rE   rK   socketAF_UNIXSOCK_STREAMgetaddrinfo	AF_UNSPECIPPROTO_TCPrb   
setsockoptTCP_NODELAYrh   wrap_socket	Exception
settimeoutr`   re   
SOL_SOCKETSO_KEEPALIVETCP_KEEPIDLErU   TCP_KEEPINTVLrV   TCP_KEEPCNTrW   connectra   rs   )rX   ssockaddrrs   errorrQ   rR   infofamilysocktypeproto_contexter%   r%   r&   _connecty  sd    

    
zClient._connectc                 C   s@   | j dk	r<z(z| j   W n tk
r.   Y nX W 5 d| _ X dS )zClose the connection to memcached, if it is open. The next call to a
        method that requires a connection will re-open it.N)rs   r|   r   rX   r%   r%   r&   r|     s    

zClient.closer   )r3   r    expirenoreplyflagsr!   c                 C   s*   |dkr| j }| jd||i|||d| S )a  
        The memcached "set" command.

        Args:
          key: str, see class docs for details.
          value: str, see class docs for details.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).
          flags: optional int, arbitrary bit field used for server-specific
                flags

        Returns:
          If no exception is raised, always returns True. If an exception is
          raised, the set may or may not have occurred. If noreply is True,
          then a successful return does not guarantee a successful set.
        Nr   r   rf   
_store_cmdrX   r3   r    r   r   r   r%   r%   r&   set  s    z
Client.set)valuesr   r   r   r!   c                 C   s4   |dkr| j }| jd||||d}dd | D S )a  
        A convenience function for setting multiple values.

        Args:
          values: dict(str, str), a dict of keys and values, see class docs
                  for details.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).
          flags: optional int, arbitrary bit field used for server-specific
                 flags

        Returns:
          Returns a list of keys that failed to be inserted.
          If noreply is True, always returns empty list.
        Nr   r   c                 S   s   g | ]\}}|s|qS r%   r%   ).0kvr%   r%   r&   
<listcomp>  s      z#Client.set_many.<locals>.<listcomp>)rf   r   items)rX   r   r   r   r   resultr%   r%   r&   set_many  s    zClient.set_manyc                 C   s:   |dkr| j }| jd||i|||d| }|dk	s6t|S )a1  
        The memcached "add" command.

        Args:
          key: str, see class docs for details.
          value: str, see class docs for details.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).
          flags: optional int, arbitrary bit field used for server-specific
                  flags

        Returns:
          If ``noreply`` is True (or if it is unset and ``self.default_noreply``
          is True), the return value is always True. Otherwise the return value
          is True if the value was stored, and False if it was not (because
          the key already existed).
        Nr   r   rf   r   AssertionErrorrX   r3   r    r   r   r   responser%   r%   r&   add  s    z
Client.add)r3   r   r   r   r!   c                 C   s:   |dkr| j }| jd||i|||d| }|dk	s6t|S )a*  
        The memcached "replace" command.

        Args:
          key: str, see class docs for details.
          value: str, see class docs for details.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).
          flags: optional int, arbitrary bit field used for server-specific
                flags

        Returns:
          If ``noreply`` is True (or if it is unset and ``self.default_noreply``
          is True), the return value is always True. Otherwise returns True if
          the value was stored and False if it wasn't (because the key didn't
          already exist).
        Nr   r   r   r   r%   r%   r&   r,      s        zClient.replacec                 C   s:   |dkr| j }| jd||i|||d| }|dk	s6t|S )a1  
        The memcached "append" command.

        Args:
          key: str, see class docs for details.
          value: str, see class docs for details.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).
          flags: optional int, arbitrary bit field used for server-specific
                flags

        Returns:
          True.
        Nr   r   r   r   r%   r%   r&   appendD  s        zClient.appendr   r   r   c                 C   s*   |dkr| j }| jd||i|||d| S )a2  
        The memcached "prepend" command.

        Args:
          key: str, see class docs for details.
          value: str, see class docs for details.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).
          flags: optional int, arbitrary bit field used for server-specific
                flags

        Returns:
          True.
        Nr   r   r   r   r%   r%   r&   prepende  s
    zClient.prepend)r   r   r!   c                 C   s(   |  |}| jd||i||||d| S )a\  
        The memcached "cas" command.

        Args:
          key: str, see class docs for details.
          value: str, see class docs for details.
          cas: int or str that only contains the characters '0'-'9'.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, False to wait for the reply (the default).
          flags: optional int, arbitrary bit field used for server-specific
                flags

        Returns:
          If ``noreply`` is True (or if it is unset and ``self.default_noreply``
          is True), the return value is always True. Otherwise returns None if
          the key didn't exist, False if it existed but had a different cas
          value and True if it existed and was changed.
        r   )r   cas)
_check_casr   )rX   r3   r    r   r   r   r   r%   r%   r&   r     s    
     z
Client.casr3   defaultr!   c                 C   s   | j d|gd| jd||S )a5  
        The memcached "get" command, but only for one key, as a convenience.

        Args:
          key: str, see class docs for details.
          default: value that will be returned if the key was not found.

        Returns:
          The value for the key, or default if the key wasn't found.
           getFr5   
_fetch_cmdr5   get)rX   r3   r   r%   r%   r&   r     s     z
Client.getkeysr!   c                 C   s   |si S | j d|d| jdS )aJ  
        The memcached "get" command.

        Args:
          keys: list(str), see class docs for details.

        Returns:
          A dict in which the keys are elements of the "keys" argument list
          and the values are values from the cache. The dict may contain all,
          some or none of the given keys.
        r   Fr   r   r5   rX   r   r%   r%   r&   get_many  s    zClient.get_many)r3   r   cas_defaultr!   c                 C   s&   ||f}| j d|gd| jd||S )a  
        The memcached "gets" command for one key, as a convenience.

        Args:
          key: str, see class docs for details.
          default: value that will be returned if the key was not found.
          cas_default: same behaviour as default argument.

        Returns:
          A tuple of (value, cas)
          or (default, cas_defaults) if the key was not found.
           getsTr   r   )rX   r3   r   r   defaultsr%   r%   r&   gets  s
     zClient.getsc                 C   s   |si S | j d|d| jdS )a[  
        The memcached "gets" command.

        Args:
          keys: list(str), see class docs for details.

        Returns:
          A dict in which the keys are elements of the "keys" argument list and
          the values are tuples of (value, cas) from the cache. The dict may
          contain all, some or none of the given keys.
        r   Tr   r   r   r%   r%   r&   	gets_many  s    zClient.gets_manyr3   r   r!   c                 C   sX   |dkr| j }d| || j }|r,|d7 }|d7 }| |gd|}|rLdS |d dkS )	a  
        The memcached "delete" command.

        Args:
          key: str, see class docs for details.
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).

        Returns:
          If ``noreply`` is True (or if it is unset and ``self.default_noreply``
          is True), the return value is always True. Otherwise returns True if
          the key was deleted, and False if it wasn't found.
        N   delete     noreply   
   deleteTr   s   DELETED)rf   ry   r5   	_misc_cmd)rX   r3   r   cmdresultsr%   r%   r&   delete  s    zClient.deleter   r   r!   c                 C   s^   |sdS |dkr| j }g }|D ],}|d| || j |r>dnd d  q| |d| dS )a  
        A convenience function to delete multiple keys.

        Args:
          keys: list(str), the list of keys to delete.
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).

        Returns:
          True. If an exception is raised then all, some or none of the keys
          may have been deleted. Otherwise all the keys have been sent to
          memcache for deletion and if noreply is False, they have been
          acknowledged by memcache.
        TNr   r   r2   r   r   )rf   r   ry   r5   r   )rX   r   r   cmdsr3   r%   r%   r&   delete_many  s"    
zClient.delete_many)r3   r    r   r!   c                 C   sr   |  || j}| |d}d| d | }|r6|d7 }|d7 }| |gd|}|rVdS |d d	krfdS t|d S )
a  
        The memcached "incr" command.

        Args:
          key: str, see class docs for details.
          value: int, the amount by which to increment the value.
          noreply: optional bool, False to wait for the reply (the default).

        Returns:
          If noreply is True, always returns None. Otherwise returns the new
          value of the key, or None if the key wasn't found.
        r    s   incr     r   r   s   incrNr   r   ry   r5   _check_integerr   r#   rX   r3   r    r   valr   r   r%   r%   r&   incr&  s    zClient.incrc                 C   sr   |  || j}| |d}d| d | }|r6|d7 }|d7 }| |gd|}|rVdS |d d	krfdS t|d S )
a  
        The memcached "decr" command.

        Args:
          key: str, see class docs for details.
          value: int, the amount by which to decrement the value.
          noreply: optional bool, False to wait for the reply (the default).

        Returns:
          If noreply is True, always returns None. Otherwise returns the new
          value of the key, or None if the key wasn't found.
        r    s   decr r   r   r   s   decrNr   r   r   r   r%   r%   r&   decrB  s    zClient.decr)r3   r   r   r!   c                 C   sp   |dkr| j }| || j}| |d}d| d | }|rD|d7 }|d7 }| |gd|}|rddS |d	 d
kS )a  
        The memcached "touch" command.

        Args:
          key: str, see class docs for details.
          expire: optional int, number of seconds until the item is expired
                  from the cache, or zero for no expiry (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).

        Returns:
          True if the expiration time was updated, False if the key wasn't
          found.
        Nr   s   touch r   r   r   s   touchTr   s   TOUCHED)rf   ry   r5   r   r   )rX   r3   r   r   expire_bytesr   r   r%   r%   r&   touch^  s    zClient.touchc              	   G   sV   |  d|d}| D ]:\}}t|t}z||||< W q tk
rN   Y qX q|S )a  
        The memcached "stats" command.

        The returned keys depend on what the "stats" command returns.
        A best effort is made to convert values to appropriate Python
        types, defaulting to strings when a conversion cannot be made.

        Args:
          *arg: extra string arguments to the "stats" command. See the
                memcached protocol documentation for more information.

        Returns:
          A dict of the returned stats.
           statsF)r   r   r1   r   r#   r   )rX   argsr   r3   r    	converterr%   r%   r&   statsz  s    zClient.statsc                 C   s    |  |d}| d|gd dS )z
        The memcached "cache_memlimit" command.

        Args:
          memlimit: int, the number of megabytes to set as the new cache memory
                    limit.

        Returns:
          If no exception is raised, always returns True.
        memlimits   cache_memlimitFT)r   r   )rX   r   r%   r%   r&   cache_memlimit  s    zClient.cache_memlimitc                 C   sF   d}|  |gdd}|d d\}}}|dkrBtd|d |S )zs
        The memcached "version" command.

        Returns:
            A string of the memcached version.
        s	   version
r0   Fr   r   s   VERSIONzReceived unexpected response: )r   	partitionr   )rX   r   r   beforer   afterr%   r%   r&   version  s    zClient.version
)command
end_tokensr!   c                 C   s\   | j r
dnd}t|tr"||n|}t|tr:||n|}| d| d g|d|d S )a  
        Sends an arbitrary command to the server and parses the response until a
        specified token is encountered.

        Args:
            command: str|bytes: The command to send.
            end_tokens: str|bytes: The token expected at the end of the
                response. If the `end_token` is not found, the client will wait
                until the timeout specified in the constructor.

        Returns:
            The response from the server, with the `end_token` removed.
        r7   r8   r2   r   Fr   )r4   r<   r=   r>   r   )rX   r   r   rg   r%   r%   r&   raw_command  s
    zClient.raw_command)delayr   r!   c                 C   sZ   |dkr| j }| |d}d| }|r.|d7 }|d7 }| |gd|}|rNdS |d d	kS )
ah  
        The memcached "flush_all" command.

        Args:
          delay: optional int, the number of seconds to wait before flushing,
                 or zero to flush immediately (the default).
          noreply: optional bool, True to not wait for the reply (defaults to
                   self.default_noreply).

        Returns:
          True.
        Nr   s
   flush_all r   r   s	   flush_allTr      OK)rf   r   r   )rX   r   r   Zdelay_bytesr   r   r%   r%   r&   	flush_all  s    zClient.flush_allc                 C   s    d}|  |gdd |   dS )z
        The memcached "quit" command.

        This will close the connection with memcached. Calling any other
        method on this object will re-open the connection, so this object can
        be re-used after quit.
        s   quit
s   quitTN)r   r|   )rX   r   r%   r%   r&   quit  s    zClient.quitgracefulr!   c                 C   sF   d}|r|d7 }|d7 }z|  |gdd W n tk
r@   Y nX dS )a  
        The memcached "shutdown" command.

        This will request shutdown and eventual termination of the server,
        optionally preceded by a graceful stop of memcached's internal state
        machine. Note that the server needs to have been started with the
        shutdown protocol command enabled with the --enable-shutdown flag.

        Args:
          graceful: optional bool, True to request a graceful shutdown with
                    SIGUSR1 (defaults to False, i.e. SIGINT shutdown).
        s   shutdowns	    gracefulr   FN)r   r   )rX   r   r   r%   r%   r&   shutdown  s    zClient.shutdown)linenamer!   c                 C   sf   | drt|| dr:||dd d  }t|| drb||dd d  }t|d S )Ns   ERRORs   CLIENT_ERRORr   r:   s   SERVER_ERROR)rM   r   findr   r   )rX   r   r   r   r%   r%   r&   _raise_errors  s    


zClient._raise_errors)r    r   r!   c                 C   s,   t |tst| d|t|| jS )zACheck that a value is an integer and encode it as a binary stringz! must be integer, got bad value: )r<   r#   r   r=   r>   rg   )rX   r    r   r%   r%   r&   r     s
    
zClient._check_integer)r   r!   c                 C   st   t |ttfrFzt|| j}W q\ tk
rB   td| Y q\X nt |ts\td| | sptd| |S )zCheck that a value is a valid input for 'cas' -- either an int or a
        string containing only 0-9

        The value will be (re)encoded so that we can accept strings or bytes.
        znon-ASCII cas value: %rz8cas must be integer, string, or bytes, got bad value: %rz6cas must only contain values in 0-9, got bad value: %r)	r<   r#   r=   r>   rg   r?   r   rt   isdigit)rX   r   r%   r%   r&   r     s    
zClient._check_cas)
expect_casr   bufremapped_keysprefixed_keysr!   c              
   C   s   |r|  \}}}}	}
nLz|  \}}}}	W n6 tk
rb } ztd|d| W 5 d}~X Y nX d}z(| jdk	sxtt| j|t|	\}}W n tk
r   |    Y nX || }| j	
||t|}|r|||
f|fS |||fS dS )z
        This function is abstracted from _fetch_cmd to support different ways
        of value extraction. In order to use this feature, _extract_value needs
        to be overridden in the subclass.
        zUnable to parse line z: N)rA   r   rL   rs   r   
_readvaluer#   r   r|   rk   Zdeserialize)rX   r   r   r   r   r   r   r3   r   sizer   r   r    Zoriginal_keyr%   r%   r&   _extract_value5  s$    &zClient._extract_value)r   r   r   r5   r!   c                    s   fdd|D }t t||}|}|r<|dd| 7 }|d7 }zNjd krh  jd k	shtj| d}d }	i }
ztj|\}}	W n tk
r   	   Y nX 
|	| |	dks|	dkr|
W S |	dr||	|||\}}}||
|< q|d	krF|	d
rF|	 }t|dkr8|d nd|
|d < q|d	kr|	dr|	 }d|dd  |
|d < qt|	d d qW n0 tk
r   	  jri  Y S  Y nX d S )Nc                    s   g | ]}j | d qS )r   )ry   r   r   r5   rX   r%   r&   r   b  s     z%Client._fetch_cmd.<locals>.<listcomp>r   r   r2   s   ENDr   s   VALUEr   s   STAT   r:   s   ITEM    )dictziprq   rs   r   r   sendall	_readliner   r|   r   rM   r   rA   rB   r   r   rc   )rX   r   r   r   r5   r   r   r   r   r   r   r3   r    	key_valuer%   r   r&   r   [  sV    
    

$zClient._fetch_cmd)r   r   r   r   r   r   r!   c                 C   s"  g }g }d}	|d k	r |	d| 7 }	|r,|	d7 }	|  |d}
| D ]\}}|| | || j}| j||\}}|d k	r~|}t|tszt	|
| j}W n. tk
r } ztd| W 5 d }~X Y nX ||d | d t	|
| j d |
 d t	t|
| j |	 d | d  q@| jd krH|   | jd k	sHtz| jd| |rrdd |D W S i }d}d }|D ]t}zt| j|\}}W n  tk
r   |    Y nX | || |t| krt| ||< nt|d d	 q|W S  tk
r   |    Y nX d S )
Nr2   r   r   r   z#Data values must be binary-safe: %sr   c                 S   s   i | ]
}|d qS )Tr%   r   r%   r%   r&   
<dictcomp>  s      z%Client._store_cmd.<locals>.<dictcomp>r   )r   r   r   ry   r5   rk   	serializer<   rt   r=   r>   rg   r?   r   rB   rs   r   r   r  rq   r  r   r|   r   VALID_STORE_RESULTSSTORE_RESULTS_VALUEr   r   )rX   r   r   r   r   r   r   r   r   extrar   r3   dataZ
data_flagsr   r   r   r   r%   r%   r&   r     s    	

	
zClient._store_cmd)r   cmd_namer   r!   c           
   	   C   s   |rt t|d}nt}| jd kr6|   | jd k	s6tz| jd| |rTg W S g }d}d }|D ]N}	z|| j|\}}W n tk
r   | 	   Y nX | 
|| || qd|W S  tk
r   | 	   Y nX d S )N)r   r2   )r   _readsegmentr  rs   r   r   r  rq   r   r|   r   r   r   )
rX   r   r
  r   r   _readerr   r   r   r   r%   r%   r&   r     s2    
zClient._misc_cmdr3   c                 C   s   | j ||dd d S NTr   r   rX   r3   r    r%   r%   r&   __setitem__  s    zClient.__setitem__c                 C   s   |  |}|d krt|S Nr   KeyErrorr  r%   r%   r&   __getitem__  s    
zClient.__getitem__c                 C   s   | j |dd d S r  r   rX   r3   r%   r%   r&   __delitem__   s    zClient.__delitem__)r   NN)r   NN)r   NN)r   NN)r   NN)r   NN)r   FN)N)NN)N)N)F)F)r   N)r   )r   N)F)r2   )NN)N)?rZ   r[   r\   r]   r}   
ServerSpecr
   r+   boolr   rT   rt   r=   r   rY   Keyry   r   r|   disconnect_allr   r#   r   r   r	   r   	set_multir   r,   r   r   r   r   r   r   	get_multir   r   r   r   r   delete_multir   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_      sn  ba7   #   
   (   (   %   #   !     
$        
 
	
+ 
?  
[ -r_   c                   @   s  e Zd ZdZeZdddddddedddddddddfeedd	d
Z	e
edddZedddZddddZeZdPeee ee dddZdQeee ee dddZeZdReee ee dddZdSeee ee dddZdTeee ee dddZdUeee ddd ZdVe
eed!d"d#Zee
 ee
ef d$d%d&ZeZe
eeef dd'd(Z ee
 ee
eeef f d$d)d*Z!dWe
ee ed+d,d-Z"dXee
 ee ed.d/d0Z#e#Z$dYe
eee ee d1d2d3Z%dZe
d4d5d6Z&d[e
d4d7d8Z'd\e
ed9d:d;Z(d<d= Z)edd>d?Z*d]edd@dAZ+dddBdCZ,d^eddDdEdFZ-d_dHdIZ.e
d4dJdKZ/dLdM Z0dNdO Z1dS )`PooledClienta  A thread-safe pool of clients (with the same client api).

    Args:
      max_pool_size: maximum pool size to use (going above this amount
                     triggers a runtime error), by default this is 2147483648L
                     when not provided (or none).
      pool_idle_timeout: pooled connections are discarded if they have been
                         unused for this many seconds. A value of 0 indicates
                         that pooled connections are never discarded.
      lock_generator: a callback/type that takes no arguments that will
                      be called to create a lock or semaphore that can
                      protect the pool from concurrent access (for example a
                      eventlet lock or semaphore could be used instead)

    Further arguments are interpreted as for :py:class:`.Client` constructor.

    Note: if `serde` is given, the same object will be used for *all* clients
    in the pool. Your serde object must therefore be thread-safe.
    NFr2   r   Tr8   )rE   rf   c                 C   s   t || _|pt||| _|| _|| _|| _|| _|	| _|
| _	|| _
|| _t|tr^|d}t|tsptd|| _tj| jdd |||d| _|| _|| _d S )Nr8   rj   c                 S   s   |   S r  )r|   )clientr%   r%   r&   <lambda>b  r2   z'PooledClient.__init__.<locals>.<lambda>)Zafter_removemax_sizeZidle_timeoutlock_generator)rS   rE   r   rk   r`   ra   rb   rc   rd   re   rf   r4   r<   r=   r>   rt   ru   r5   r   Z
ObjectPool_create_clientclient_poolrg   rh   )rX   rE   rk   rv   rw   r`   ra   rb   rc   rd   re   r5   Zmax_pool_sizeZpool_idle_timeoutr%  rf   r4   rg   rh   r%   r%   r&   rY   <  s0    



zPooledClient.__init__)r3   r!   c                 C   s   t || j| jdS rx   )rD   r4   r5   r  r%   r%   r&   ry   j  s
      zPooledClient.check_keyrz   c                 C   s8   | j | j| j| j| j| jd| j| j| j| j	| j
| jdS )NF)rk   r`   ra   rb   rc   rd   re   r5   rf   r4   rh   )client_classrE   rk   r`   ra   rb   rd   re   r5   rf   r4   rh   r   r%   r%   r&   r&  p  s    zPooledClient._create_clientc                 C   s   | j   d S r  )r'  clearr   r%   r%   r&   r|     s    zPooledClient.closer   c              
   C   s:   | j jdd"}|j|||||dW  5 Q R  S Q R X d S NTZdestroy_on_failr   )r'  get_and_releaser   rX   r3   r    r   r   r   r"  r%   r%   r&   r     s    zPooledClient.setc              
   C   s8   | j jdd }|j||||dW  5 Q R  S Q R X d S r*  )r'  r,  r   )rX   r   r   r   r   r"  r%   r%   r&   r     s    zPooledClient.set_manyc              
   C   s:   | j jdd"}|j|||||dW  5 Q R  S Q R X d S r*  )r'  r,  r,   r-  r%   r%   r&   r,     s        zPooledClient.replacec              
   C   s:   | j jdd"}|j|||||dW  5 Q R  S Q R X d S r*  )r'  r,  r   r-  r%   r%   r&   r     s        zPooledClient.appendc              
   C   s:   | j jdd"}|j|||||dW  5 Q R  S Q R X d S r*  )r'  r,  r   r-  r%   r%   r&   r     s        zPooledClient.prepend)r   r   c              
   C   s<   | j jdd$}|j||||||dW  5 Q R  S Q R X d S r*  )r'  r,  r   )rX   r3   r    r   r   r   r   r"  r%   r%   r&   r     s    	     zPooledClient.casr   c              
   C   sj   | j jddR}z|||W W  5 Q R  S  tk
rZ   | jrT| Y W  5 Q R  S  Y nX W 5 Q R X d S NTr+  )r'  r,  r   r   rc   )rX   r3   r   r"  r%   r%   r&   r     s    zPooledClient.getr   c              
   C   sh   | j jddP}z||W W  5 Q R  S  tk
rX   | jrRi  Y W  5 Q R  S  Y nX W 5 Q R X d S r.  )r'  r,  r   r   rc   rX   r   r"  r%   r%   r&   r     s    zPooledClient.get_manyc              
   C   sd   | j jddL}z||W W  5 Q R  S  tk
rT   | jrNY W 5 Q R  dS  Y nX W 5 Q R X d S )NTr+  )NN)r'  r,  r   r   rc   )rX   r3   r"  r%   r%   r&   r     s    zPooledClient.getsc              
   C   sh   | j jddP}z||W W  5 Q R  S  tk
rX   | jrRi  Y W  5 Q R  S  Y nX W 5 Q R X d S r.  )r'  r,  r   r   rc   r/  r%   r%   r&   r     s    zPooledClient.gets_manyr   c              
   C   s4   | j jdd}|j||dW  5 Q R  S Q R X d S NTr+  r  )r'  r,  r   )rX   r3   r   r"  r%   r%   r&   r     s    zPooledClient.deleter   c              
   C   s4   | j jdd}|j||dW  5 Q R  S Q R X d S r0  )r'  r,  r   )rX   r   r   r"  r%   r%   r&   r     s    zPooledClient.delete_many)r3   r   r   r   c              
   C   s:   | j jdd"}|j|||||dW  5 Q R  S Q R X d S r*  )r'  r,  r   r-  r%   r%   r&   r     s    zPooledClient.addr  c              
   C   s6   | j jdd}|j|||dW  5 Q R  S Q R X d S r0  )r'  r,  r   rX   r3   r    r   r"  r%   r%   r&   r     s    zPooledClient.incrc              
   C   s6   | j jdd}|j|||dW  5 Q R  S Q R X d S r0  )r'  r,  r   r1  r%   r%   r&   r     s    zPooledClient.decr)r3   r   c              
   C   s6   | j jdd}|j|||dW  5 Q R  S Q R X d S )NTr+  )r   r   )r'  r,  r   )rX   r3   r   r   r"  r%   r%   r&   r     s    zPooledClient.touchc              
   G   sh   | j jddP}z|j| W W  5 Q R  S  tk
rX   | jrRi  Y W  5 Q R  S  Y nX W 5 Q R X d S r.  )r'  r,  r   r   rc   )rX   r   r"  r%   r%   r&   r     s    zPooledClient.statsc              
   C   s.   | j jdd}| W  5 Q R  S Q R X d S r.  )r'  r,  r   rX   r"  r%   r%   r&   r   (  s    zPooledClient.versionc              
   C   s4   | j jdd}|j||dW  5 Q R  S Q R X d S )NTr+  )r   r   )r'  r,  r   )rX   r   r   r"  r%   r%   r&   r   ,  s    zPooledClient.flush_allc              
   C   s:   | j jdd"}z|  W 5 | j | X W 5 Q R X d S r.  )r'  r,  destroyr   r2  r%   r%   r&   r   0  s    zPooledClient.quitr   c              	   C   s(   | j jdd}|| W 5 Q R X d S r.  )r'  r,  r   )rX   r   r"  r%   r%   r&   r   7  s    zPooledClient.shutdownr   c              
   C   s2   | j jdd}|||W  5 Q R  S Q R X d S r.  )r'  r,  r   )rX   r   r   r"  r%   r%   r&   r   ;  s    zPooledClient.raw_commandc                 C   s   | j ||dd d S r  r  r  r%   r%   r&   r  ?  s    zPooledClient.__setitem__c                 C   s   |  |}|d krt|S r  r  r  r%   r%   r&   r  B  s    
zPooledClient.__getitem__c                 C   s   | j |dd d S r  r  r  r%   r%   r&   r  H  s    zPooledClient.__delitem__)r   NN)r   NN)r   NN)r   NN)r   NN)r   FN)N)N)N)r   NN)F)F)r   N)r   N)F)r   )2rZ   r[   r\   r]   r_   r(  r}   r  r  rY   r  rt   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   r   r   r  r  r  r%   r%   r%   r&   r!  $  s   .      
            


$
   

r!  )rs   r   r!   c                 C   s   g }d}|dkrJ|dd dkrJ|d dd |d< |dd d |fS |d}|dkr|d| ||d	 d  }}|| |d |fS |r|| |dd }t| t}|st qdS )
a  Read line of text from the socket.

    Read a line of text (delimited by "
") from the socket, and
    return that line along with any trailing characters read from the
    socket.

    Args:
        sock: Socket object, should be connected.
        buf: Bytes, zero or more characters, returned from an earlier
            call to _readline or _readvalue (pass an empty byte string on the
            first call).

    Returns:
      A tuple of (buf, line) where line is the full line read from the
      socket (minus the "
" characters) and buf is any trailing
      characters read after the "
" was found (which may be an empty
      byte string).

    r2      r   r:      
Nr   r   )rq   r   r   _recv	RECV_SIZEr   )rs   r   chunks	last_charZ	token_posr   r   r%   r%   r&   r  L  s     	



r  )rs   r   r   c                 C   s   g }|d }|t | dkrL|r6|t |8 }|| t| t}|st q|dkrj|d dd |d< n||d|d   ||d d|fS )a  Read specified amount of bytes from the socket.

    Read size bytes, followed by the "
" characters, from the socket,
    and return those bytes and any trailing bytes read after the "
".

    Args:
        sock: Socket object, should be connected.
        buf: String, zero or more characters, returned from an earlier
            call to _readline or _readvalue (pass an empty string on the
            first call).
        size: Integer, number of bytes to read from the socket.

    Returns:
      A tuple of (buf, value) where value is the bytes read from the
      socket (there will be exactly size bytes) and buf is trailing
      characters read after the "
" following the bytes (but not
      including the 
).

    r   r   r:   r6  Nr2   )rB   r   r7  r8  r   rq   )rs   r   r   r9  Zrlenr%   r%   r&   r     s    

r   )rs   r   r   r!   c                 C   sd   t  }||}|dkrJ|d| ||t| d  }}||7 }||fS t| t}|st qdS )a  Read a segment from the socket.

    Read a segment from the socket, up to the first end_token sub-string/bytes,
    and return that segment.

    Args:
        sock: Socket object, should be connected.
        buf: bytes, zero or more bytes, returned from an earlier
            call to _readline, _readsegment or _readvalue (pass an empty
            byte-string on the first call).
        end_tokens: bytes, indicates the end of the segment, generally this is
            b"\r\n" for memcached.

    Returns:
      A tuple of (buf, line) where line is the full line read from the
      socket (minus the end_tokens bytes) and buf is any trailing
      characters read after the end_tokens was found (which may be an empty
      bytes object).

    r6  N)rt   r   rB   r7  r8  r   )rs   r   r   r   Z
tokens_posr   r   r%   r%   r&   r    s    
"
r  )rs   r   r!   c              
   C   sD   z|  |W S  tk
r< } z|jtjkr, W 5 d}~X Y q X q dS )zsock.recv() with retry on EINTRN)recvOSErrorerrnoZEINTR)rs   r   r   r%   r%   r&   r7    s
    r7  )r2   )7r=  rl   r}   	functoolsr   sslr   typesr   typingr   r   r   r   r	   r
   r   r   Z
pymemcacher   Zpymemcache.exceptionsr   r   r   r   r   r   Zpymemcache.serder   r8  r  rn   r  r=   r#   r  rt   r  r  r'   r(   r+   r-   r/   r1   __annotations__rD   rS   rT   r_   r!  r  r   r  r7  r%   r%   r%   r&   <module>   s    ( 
             z  *3.  
&