a
    a*j`                     @  sh   d dl mZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZ e
rTd dlZG dd deZdS )    )annotationsN)ConcurrentUpdateError)Storage)TYPE_CHECKINGOptionalUnionc                      s   e Zd ZdZdgZdgZdZdZedfddd	dd
d fddZe	ddddZ
dddddZd&ddd	dddddZdddddZdddddZd	ddd Zd!dd"d#Zdd
dd$d%Z  ZS )'EtcdStoragezQ
    Rate limit storage with etcd as backend.

    Depends on :pypi:`etcd3`.
    Zetcdetcd3Zlimits   FstrintboolNone)urimax_retrieswrap_exceptionsoptionsreturnc                   sP   t j|}| jd j| _| jj|j|jfi || _	|| _
t j||d dS )a&  
        :param uri: etcd location of the form
         ``etcd://host:port``,
        :param max_retries: Maximum number of attempts to retry
         in the case of concurrent updates to a rate limit key
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        :param options: all remaining keyword arguments are passed
         directly to the constructor of :class:`etcd3.Etcd3Client`
        :raise ConfigurationError: when :pypi:`etcd3` is not available
        r	   )r   N)urllibparseurlparsedependenciesmodulelibclienthostnameportstorager   super__init__)selfr   r   r   r   parsed	__class__ O/var/www/html/assistant/venv/lib/python3.9/site-packages/limits/storage/etcd.pyr      s    zEtcdStorage.__init__z3Union[type[Exception], tuple[type[Exception], ...]])r   c                 C  s   | j jS N)r   ZEtcd3Exceptionr    r$   r$   r%   base_exceptions5   s    zEtcdStorage.base_exceptionsbytes)keyr   c                 C  s   | j  d|  S N/)PREFIXencoder    r*   r$   r$   r%   prefixed_key;   s    zEtcdStorage.prefixed_key   )r*   expiryelastic_expiryamountr   c                 C  sl  d}|  |}|| jk r^t }| j|}|| }	| jj| jj|dkg| jjj|| d|	 	 |j
dg| jj|gd}
|
d r|S |
d d d \}}|d\}}	t|	}	|	|kr| j|j | j| nt|r| j|j || }	t|| }| jj| jj||kg| jjj|| d|	 	 |jdgg dd rT|S |d7 }qt||d S )Nr   0:)lease)comparesuccessZfailurer1      :)r0   r   timer   r7   ZtransactionZtransactionscreateputr.   idgetsplitfloatZrevoke_leaseZlease_iddeleteZrefresh_leaser   valuer   )r    r*   r2   r3   r4   retriesZetcd_keynowr7   Z
window_endZcreate_attemptcurmetaZ	cur_valuenewr$   r$   r%   incr>   sV    


zEtcdStorage.incrc                 C  sD   | j | |\}}|r@|d\}}t|t kr@t|S dS )Nr:   r   )r   r?   r0   r@   rA   r;   r   )r    r*   rC   rG   r4   r2   r$   r$   r%   r?   o   s    zEtcdStorage.getrA   c                 C  s4   | j | |\}}|r,t|dd S t S )Nr:   r1   )r   r?   r0   rA   r@   r;   )r    r*   rC   _r$   r$   r%   
get_expiryw   s    zEtcdStorage.get_expiryc                 C  s$   z| j   W dS    Y dS 0 d S )NTF)r   statusr'   r$   r$   r%   check}   s
    
zEtcdStorage.checkzOptional[int]c                 C  s   | j | j djS r+   )r   Zdelete_prefixr-   Zdeletedr'   r$   r$   r%   reset   s    zEtcdStorage.resetc                 C  s   | j | | d S r&   )r   rB   r0   r/   r$   r$   r%   clear   s    zEtcdStorage.clear)Fr1   )__name__
__module____qualname____doc__ZSTORAGE_SCHEMEZDEPENDENCIESr-   ZMAX_RETRIESr   propertyr(   r0   rI   r?   rK   rM   rN   rO   __classcell__r$   r$   r"   r%   r      s$    1r   )
__future__r   r;   urllib.parser   Zlimits.errorsr   Zlimits.storage.baser   Zlimits.typingr   r   r   r	   r   r$   r$   r$   r%   <module>   s   