o
    \"<fC                     @   s   d Z ddlZddlmZmZmZmZmZmZm	Z	 e
eZee
e ejejejdZe Zdd Zddd	ZdddZdd Zdd Zdd ZG dd dejZedkr`ddlZe  dS dS )zPython-level wrapper client    N   )hashersdistributionsall_behaviorshashers_rvsdistributions_rvsall_callbacksBehaviorDict)tcpudpunixc                 C   s4   |  dr	d| fS |  drd| dd  fS d| fS )N/r   zudp:r      r
   )
startswith)spec r   E/var/www/html/kck/venv/lib/python3.10/site-packages/pylibmc/client.py_split_spec_type   s
   

r   c                 C   s   |  dr&d| vrt| | dr| dd }n| dd  dd\}}nd| v r3| dd\}}n| }t|trFd|v rF|dd\}}|||fS )N[]r   z]::)r   
ValueErrorendswithrsplitsplit
isinstancestr)r   portweightaddrr   r   r   _unpack_addr   s   


r!   +  c                 C   s\   t | \}}t| }|dkr|d}}n|dv r"t|||d\}}}|t|t|t|fS )a  Translate/normalize specification *server* into 4-tuple (scheme, addr, port, weight).

    This is format is used by the extension module.

    >>> translate_server_spec("127.0.0.1")
    (1, '127.0.0.1', 11211, 1)
    >>> translate_server_spec("udp:127.0.0.1")
    (2, '127.0.0.1', 11211, 1)
    >>> translate_server_spec("/var/run/memcached.sock")
    (4, '/var/run/memcached.sock', 0, 1)
    >>> translate_server_spec("/var/run/memcached.sock", weight=2)
    (4, '/var/run/memcached.sock', 0, 2)

    >>> translate_server_spec("127.0.0.1:22122")
    (1, '127.0.0.1', 22122, 1)
    >>> translate_server_spec("127.0.0.1:1234:2")
    (1, '127.0.0.1', 1234, 2)
    >>> translate_server_spec("127.0.0.1", port=1234, weight=2)
    (1, '127.0.0.1', 1234, 2)

    >>> translate_server_spec("[::1]:22122")
    (1, '::1', 22122, 1)
    >>> translate_server_spec("[::1]")
    (1, '::1', 11211, 1)
    >>> translate_server_spec("[::1]:22122:3")
    (1, '::1', 22122, 3)
    r   r   )r
   r   )r   r   )r   server_type_mapr!   r   int)serverr   r   schemer   styper    r   r   r   translate_server_spec+   s   r(   c                 C   s@   g }| D ]}t |trt|dkr|}nt|}|| q|S )N   )r   tuplelenr(   append)servers	addr_tupsr%   addr_tupr   r   r   translate_server_specsO   s   r0   c                 C   s,   |   } t| d  | d< t| d  | d< | S )z,Turn numeric constants into symbolic stringshashdistribution)copyr   r   )	behaviorsr   r   r   _behaviors_symbolicZ   s   r5   c                 C   s   | s| S |   } d| v r| d| d t| t}|r/dttt	|}t
d| | ddur>t| d  | d< | dtv rMt| d  | d< | ddur\t| d  | d< | S )	z&Inverse function of behaviors_symbolic_retry_timeoutretry_timeout, zunknown behavior names: r1   Nketama_hashr2   )r3   
setdefaultpopset
difference_all_behaviors_setjoinmapr   sortedr   getr   r   )r4   unknownnamesr   r   r   _behaviors_numericd   s    rE   c                       s   e Zd Z		d fdd	Zdd Zdd Zd	d
 Zdd Zdd Zdd Z	 fddZ
 fddZee
eZedd Z fddZ  ZS )ClientNFc                    s2   || _ t|| _t jt||||t|d dS )a  Initialize a memcached client instance.

        This connects to the servers in *servers*, which will default to being
        TCP servers. If it looks like a filesystem path, a UNIX socket. If
        prefixed with `udp:`, a UDP connection.

        If *binary* is True, the binary memcached protocol is used.

        SASL authentication is supported if libmemcached supports it (check
        *pylibmc.support_sasl*). Requires both username and password.
        Note that SASL requires *binary*=True.
        )r-   binaryusernamepasswordr4   N)rG   list	addressessuper__init__r0   rE   )selfr-   r4   rG   rH   rI   	__class__r   r   rM   ~   s   

zClient.__init__c                 C   s   d | jj| j| jS )Nz{}({!r}, binary={!r}))formatrP   __name__rK   rG   rN   r   r   r   __repr__      
zClient.__repr__c                 C   s&   d tt| j}d| jj|| jS )Nr8   z<{} for {}, binary={!r}>)r?   r@   r   rK   rQ   rP   rR   rG   )rN   addrsr   r   r   __str__   s   
zClient.__str__c                 C   s    |  |t}|tu rt||S N)rB   _MISS_SENTINELKeyErrorrN   keyvaluer   r   r   __getitem__   s   zClient.__getitem__c                 C   s   |  ||std|d S )Nzfailed setting )r<   rZ   r[   r   r   r   __setitem__   s   zClient.__setitem__c                 C   s   |  |s	t|d S rX   )deleterZ   rN   r\   r   r   r   __delitem__   rU   zClient.__delitem__c                 C   s   |  |ttuS rX   )rB   rY   ra   r   r   r   __contains__   s   zClient.__contains__c                    s   t | tt  S )zGets the behaviors from the underlying C client instance.

        Reverses the integer constants for `hash` and `distribution` into more
        understandable string values. See *set_behaviors* for info.
        )r	   r5   rL   get_behaviorsrS   rO   r   r   rd      s   zClient.get_behaviorsc                    s   t  t|S )a  Sets the behaviors on the underlying C client instance.

        Takes care of morphing the `hash` key, if specified, into the
        corresponding integer constant (which the C client expects.) If,
        however, an unknown value is specified, it's passed on to the C client
        (where it most surely will error out.)

        This also happens for `distribution`.

        Translates old underscored behavior names to new ones for API leniency.
        )rL   set_behaviorsrE   )rN   r4   rO   r   r   re      s   zClient.set_behaviorsc                 C   s   t d)Nznobody uses british spellings)AttributeErrorrS   r   r   r   
behaviours   s   zClient.behavioursc                    s"   t   }t| j|_| j|_|S rX   )rL   clonerJ   rK   rG   )rN   objrO   r   r   rh      s   
zClient.clone)NFNN)rR   
__module____qualname__rM   rT   rW   r^   r_   rb   rc   rd   re   propertyr4   rg   rh   __classcell__r   r   rO   r   rF   }   s     

rF   __main__)Nr   )r"   r   )__doc___pylibmcconstsr   r   r   r   r   r   r	   r<   r>   updateserver_type_tcpserver_type_udpserver_type_unixr#   objectrY   r   r!   r(   r0   r5   rE   clientrF   rR   doctesttestmodr   r   r   r   <module>   s*    $
	
$
V