U
    ]g                     @  s   d dl mZ d dl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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G dd	 d	eedZG dd deZdS )    )annotationsN)ABCabstractmethod)Anycast)errors)StorageRegistry)CallableListOptionalPRTupleTypeUnion)LazyDependencyStoragezCallable[P, R])storagefnreturnc                   s&   t  dddd fdd}|S )NzP.argszP.kwargsr   argskwargsr   c               
     sJ   z | |W S  j k
rD } zjr2t|| W 5 d }~X Y nX d S N)base_exceptionswrap_exceptionsr   ZStorageError)r   r   excr   r    7/tmp/pip-unpacked-wheel-kizsipjx/limits/storage/base.pyinner   s    z_wrap_errors.<locals>.inner)	functoolswraps)r   r   r    r   r   r   _wrap_errors   s    r#   c                      s   e Zd ZU dZded< ddd d fddZ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   zC
    Base class to extend when implementing a 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+   ,   s    zStorage.__new__NFzOptional[str]boolzUnion[float, str, bool])urir   optionsc                   s   t  | _t   || _dS )z
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        N)	threadingRLocklockr*   __init__r   )selfr5   r   r6   r2   r   r   r:   ;   s    

zStorage.__init__z3Union[Type[Exception], Tuple[Type[Exception], ...]])r   c                 C  s   t d S r   NotImplementedErrorr;   r   r   r   r   J   s    zStorage.base_exceptions   strint)keyexpiryelastic_expiryamountr   c                 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;   rB   rC   rD   rE   r   r   r   r'   O   s    zStorage.incr)rB   r   c                 C  s   t dS )zB
        :param key: the key to get the counter value for
        Nr<   r;   rB   r   r   r   r%   ^   s    zStorage.getc                 C  s   t dS )z;
        :param key: the key to get the expiry for
        Nr<   rF   r   r   r   r)   e   s    zStorage.get_expiryc                 C  s   t dS )z-
        check if storage is healthy
        Nr<   r>   r   r   r   r$   l   s    zStorage.checkzOptional[int]c                 C  s   t dS )z/
        reset storage to clear limits
        Nr<   r>   r   r   r   r&   s   s    zStorage.resetNonec                 C  s   t dS )za
        resets the rate limit key

        :param key: the key to clear rate limits for
        Nr<   rF   r   r   r   r(   z   s    zStorage.clear)NF)Fr?   )__name__
__module____qualname____doc____annotations__r+   r:   propertyr   r   r'   r%   r)   r$   r&   r(   __classcell__r   r   r2   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   r-   r.   r2   r   r   r+      s    zMovingWindowSupport.__new__r?   r@   rA   r4   )rB   limitrC   rE   r   c                 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;   rB   rS   rC   rE   r   r   r   rQ      s    z!MovingWindowSupport.acquire_entryzTuple[int, int])rB   rS   rC   r   c                 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;   rB   rS   rC   r   r   r   rR      s    
z%MovingWindowSupport.get_moving_window)r?   )	rH   rI   rJ   rK   r+   r   rQ   rR   rN   r   r   r2   r   rP      s   	rP   )
__future__r   r!   r7   abcr   r   typingr   r   Zlimitsr   Zlimits.storage.registryr   Zlimits.typingr	   r
   r   r   r   r   r   r   Zlimits.utilr   r#   r   rP   r   r   r   r   <module>   s   (
`