a
    a*j^                     @  s   d dl mZ d dl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ZeddG dd deedZG dd deZG dd deZdS )    )annotationsN)ABCabstractmethod)versionadded)errors)StorageRegistry)	Any	AwaitableCallableOptionalPRTypeUnioncast)LazyDependencyzCallable[P, Awaitable[R]])fnreturnc                   s$   t  dddd fdd}|S )NzP.argszP.kwargsr   )argskwargsr   c               
     sd   t t| d }z | i |I d H W S  |jy^ } z |jrHt|| W Y d }~n
d }~0 0 d S )Nr   )r   Storagebase_exceptionswrap_exceptionsr   ZStorageError)r   r   instanceexcr    S/var/www/html/assistant/venv/lib/python3.9/site-packages/limits/aio/storage/base.pyinner   s    z_wrap_errors.<locals>.inner)	functoolswraps)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 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   Noner   r   c                   sH   t  jf i | dD ]}t| |tt| | qt  jf i | d S )N>   clearcheckincrreset
get_expiryget)super__init_subclass__setattrr!   getattrclsr   method	__class__r   r   r,   1   s    zStorage.__init_subclass__NFzOptional[str]boolzUnion[float, str, bool])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   )selfr5   r   r6   r2   r   r   r7   >   s    

zStorage.__init__z3Union[Type[Exception], tuple[Type[Exception], ...]])r   c                 C  s   t d S )NNotImplementedErrorr8   r   r   r   r   K   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
        Nr9   )r8   r?   r@   rA   rB   r   r   r   r'   P   s    zStorage.incr)r?   r   c                   s   t dS )zB
        :param key: the key to get the counter value for
        Nr9   r8   r?   r   r   r   r*   _   s    zStorage.getfloatc                   s   t dS )z;
        :param key: the key to get the expiry for
        Nr9   rC   r   r   r   r)   f   s    zStorage.get_expiryc                   s   t dS )z-
        check if storage is healthy
        Nr9   r;   r   r   r   r&   m   s    zStorage.checkzOptional[int]c                   s   t dS )z/
        reset storage to clear limits
        Nr9   r;   r   r   r   r(   t   s    zStorage.resetc                   s   t dS )za
        resets the rate limit key

        :param key: the key to clear rate limits for
        Nr9   rC   r   r   r   r%   {   s    zStorage.clear)NF)Fr<   )__name__
__module____qualname____doc____annotations__r,   r7   propertyr   r   r'   r*   r)   r&   r(   r%   __classcell__r   r   r2   r   r   (   s,   
   r   )	metaclassc                      s\   e Zd ZdZ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 )MovingWindowSupportzn
    Abstract base class for async storages that support
    the :ref:`strategies:moving window` strategy
    r   r#   r$   c                   s6   dD ]}t | |tt| | qt jf i | d S )N>   get_moving_windowacquire_entryr-   r!   r.   r+   r,   r/   r2   r   r   r,      s    z%MovingWindowSupport.__init_subclass__r<   r=   r>   r4   r?   limitr@   rB   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
        Nr9   r8   r?   rR   r@   rB   r   r   r   rO      s    
z!MovingWindowSupport.acquire_entryztuple[float, int])r?   rR   r@   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)
        Nr9   )r8   r?   rR   r@   r   r   r   rN      s    z%MovingWindowSupport.get_moving_window)r<   )	rE   rF   rG   rH   r,   r   rO   rN   rK   r   r   r2   r   rM      s    rM   c                      sZ   e Zd ZdZ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Z  ZS )SlidingWindowCounterSupportzw
    Abstract base class for async storages that support
    the :ref:`strategies:sliding window counter` strategy
    r   r#   r$   c                   s6   dD ]}t | |tt| | qt jf i | d S )N>   acquire_sliding_window_entryget_sliding_windowrP   r/   r2   r   r   r,      s    z-SlidingWindowCounterSupport.__init_subclass__r<   r=   r>   r4   rQ   c                   s   t dS )aU  
        Acquire an entry if the weighted count of the current and previous
        windows is less than or equal to the limit

        :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
        Nr9   rS   r   r   r   rU      s    z8SlidingWindowCounterSupport.acquire_sliding_window_entryztuple[int, float, int, float])r?   r@   r   c                   s   t dS )a  
        Return the previous and current window information.

        :param key: the rate limit key
        :param expiry: the rate limit expiry, needed to compute the key in some implementations
        :return: a tuple of (int, float, int, float) with the following information:
          - previous window counter
          - previous window TTL
          - current window counter
          - current window TTL
        Nr9   )r8   r?   r@   r   r   r   rV      s    z.SlidingWindowCounterSupport.get_sliding_window)r<   )	rE   rF   rG   rH   r,   r   rU   rV   rK   r   r   r2   r   rT      s   	 rT   )
__future__r   r   abcr   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   rM   rT   r   r   r   r   <module>   s   ,\-