U
    ]g                     @  s   d dl mZ d dlZd dlmZmZ d dlmZm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mZ d d	lmZ d
dddddZe
ddG dd
 d
eedZG dd deZdS )    )annotationsN)ABCabstractmethod)Anycast)versionadded)errors)StorageRegistry)		AwaitableCallableListOptionalPRTupleTypeUnion)LazyDependencyStoragezCallable[P, Awaitable[R]])storagefnreturnc                   s&   t  dddd fdd}|S )NzP.argszP.kwargsr   argskwargsr   c               
     sP   z | |I d H W S  j k
rJ } zjr8t|| W 5 d }~X Y nX d S N)base_exceptionswrap_exceptionsr   ZStorageError)r   r   excr   r    ;/tmp/pip-unpacked-wheel-kizsipjx/limits/aio/storage/base.pyinner   s    z_wrap_errors.<locals>.inner)	functoolswraps)r   r   r"   r    r   r!   _wrap_errors   s    r%   z2.1)versionc                      s   e Zd ZU dZded< ddd d fddZd'd
dddd fddZeeddddZ	ed(ddddddddZ
edddddZedddddZeddd d!Zed"dd#d$Zedddd%d&Z  ZS ))r   zJ
    Base class to extend when implementing an async storage backend.
    zOptional[List[str]]ZSTORAGE_SCHEMEr   r   c              	     s2   t  | }dD ]}t||t|t|| q|S )N>   checkgetresetincrclear
get_expiry)super__new__setattrr%   getattrclsr   r   instmethod	__class__r    r!   r.   2   s    zStorage.__new__NFzOptional[str]boolzUnion[float, str, bool]None)urir   optionsr   c                   s   t    || _dS )z
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        N)r-   __init__r   )selfr9   r   r:   r5   r    r!   r;   A   s    

zStorage.__init__z3Union[Type[Exception], Tuple[Type[Exception], ...]])r   c                 C  s   t d S r   NotImplementedErrorr<   r    r    r!   r   N   s    zStorage.base_exceptions   strint)keyexpiryelastic_expiryamountr   c                   s   t dS )aD  
        increments the counter for a given rate limit key

        :param key: the key to increment
        :param expiry: amount in seconds for the key to expire in
        :param elastic_expiry: whether to keep extending the rate limit
         window every hit.
        :param amount: the number to increment by
        Nr=   )r<   rC   rD   rE   rF   r    r    r!   r*   S   s    zStorage.incr)rC   r   c                   s   t dS )zB
        :param key: the key to get the counter value for
        Nr=   r<   rC   r    r    r!   r(   b   s    zStorage.getc                   s   t dS )z;
        :param key: the key to get the expiry for
        Nr=   rG   r    r    r!   r,   i   s    zStorage.get_expiryc                   s   t dS )z-
        check if storage is healthy
        Nr=   r?   r    r    r!   r'   p   s    zStorage.checkzOptional[int]c                   s   t dS )z/
        reset storage to clear limits
        Nr=   r?   r    r    r!   r)   w   s    zStorage.resetc                   s   t dS )za
        resets the rate limit key

        :param key: the key to clear rate limits for
        Nr=   rG   r    r    r!   r+   ~   s    zStorage.clear)NF)Fr@   )__name__
__module____qualname____doc____annotations__r.   r;   propertyr   r   r*   r(   r,   r'   r)   r+   __classcell__r    r    r5   r!   r   )   s.   
     )	metaclassc                      s^   e Zd ZdZddd d fddZedddddd	d
ddZedddddddZ  ZS )MovingWindowSupportzZ
    Abstract base for storages that intend to support
    the moving window strategy
    r   r   c              	     s8   t  | }dD ]"}t||ttt|t|| q|S )N>   acquire_entryget_moving_window)r-   r.   r/   r%   r   r   r0   r1   r5   r    r!   r.      s    zMovingWindowSupport.__new__r@   rA   rB   r7   )rC   limitrD   rF   r   c                   s   t dS )z
        :param key: rate limit key to acquire an entry in
        :param limit: amount of entries allowed
        :param expiry: expiry of the entry
        :param amount: the number of entries to acquire
        Nr=   )r<   rC   rS   rD   rF   r    r    r!   rQ      s    
z!MovingWindowSupport.acquire_entryzTuple[int, int])rC   rS   rD   r   c                   s   t dS )z
        returns the starting point and the number of entries in the moving
        window

        :param key: rate limit key
        :param expiry: expiry of entry
        :return: (start of window, number of acquired entries)
        Nr=   )r<   rC   rS   rD   r    r    r!   rR      s    z%MovingWindowSupport.get_moving_window)r@   )	rH   rI   rJ   rK   r.   r   rQ   rR   rN   r    r    r5   r!   rP      s    rP   )
__future__r   r#   abcr   r   typingr   r   Zdeprecated.sphinxr   Zlimitsr   Zlimits.storage.registryr	   Zlimits.typingr
   r   r   r   r   r   r   r   r   Zlimits.utilr   r%   r   rP   r    r    r    r!   <module>   s   ,^