o
    ;h2                     @  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 d dlmZ 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PRcast)LazyDependencyfnCallable[P, Awaitable[R]]returnc                   s   t  d	 fdd}|S )
NargsP.argskwargsP.kwargsr   r   c               
     sV   t t| d }z | i |I d H W S  |jy* } z
|jr%t|| d }~ww )Nr   )r   Storagebase_exceptionswrap_exceptionsr   ZStorageError)r   r   instanceexcr    J/var/www/html/venv/lib/python3.10/site-packages/limits/aio/storage/base.pyinner   s   z_wrap_errors.<locals>.inner)r   r   r   r   r   r   )	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+ fdd	Z	
	d,d- fddZeed.ddZ	ed/d0ddZ
ed1dd Zed2d"d#Zed3d$d%Zed4d'd(Zed5d)d*Z  ZS )6r   zJ
    Base class to extend when implementing an async storage backend.
    zlist[str] | NoneZSTORAGE_SCHEMEr   r   r   Nonec                   sH   t  jdi | dD ]}t| |tt| | qt  jdi | d S )N>   
get_expiryclearincrgetcheckresetr   )super__init_subclass__setattrr!   getattrclsr   method	__class__r   r   r+   .   s   zStorage.__init_subclass__NFuri
str | Noner   booloptionsfloat | str | boolc                   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   )selfr3   r   r6   r1   r   r   r8   ;   s   


zStorage.__init__-type[Exception] | tuple[type[Exception], ...]c                 C  s   t )NNotImplementedErrorr9   r   r   r   r   H   s   zStorage.base_exceptions   keystrexpiryintamountc                      t )z
        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 amount: the number to increment by
        r;   )r9   r?   rA   rC   r   r   r   r&   M   s   	zStorage.incrc                   rD   )zB
        :param key: the key to get the counter value for
        r;   r9   r?   r   r   r   r'   X      zStorage.getfloatc                   rD   )z;
        :param key: the key to get the expiry for
        r;   rE   r   r   r   r$   _   rF   zStorage.get_expiryc                   rD   )z-
        check if storage is healthy
        r;   r=   r   r   r   r(   f   rF   zStorage.check
int | Nonec                   rD   )z/
        reset storage to clear limits
        r;   r=   r   r   r   r)   m   rF   zStorage.resetc                   rD   )za
        resets the rate limit key

        :param key: the key to clear rate limits for
        r;   rE   r   r   r   r%   t   s   zStorage.clearr   r   r   r#   )NF)r3   r4   r   r5   r6   r7   r   r#   )r   r:   r>   )r?   r@   rA   rB   rC   rB   r   rB   )r?   r@   r   rB   )r?   r@   r   rG   )r   r5   )r   rH   )r?   r@   r   r#   )__name__
__module____qualname____doc____annotations__r+   r8   propertyr   r   r&   r'   r$   r(   r)   r%   __classcell__r   r   r1   r   r   %   s.   
 
r   )	metaclassc                      sB   e Zd ZdZd fddZe	ddddZedddZ  ZS )MovingWindowSupportzn
    Abstract base class for async storages that support
    the :ref:`strategies:moving window` strategy
    r   r   r   r#   c                   6   dD ]}t | |tt| | qt jdi | d S )N>   acquire_entryget_moving_windowr   r,   r!   r-   r*   r+   r.   r1   r   r   r+      s   z%MovingWindowSupport.__init_subclass__r>   r?   r@   limitrB   rA   rC   r5   c                   rD   )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
        r;   r9   r?   rX   rA   rC   r   r   r   rU      s   
z!MovingWindowSupport.acquire_entrytuple[float, int]c                   rD   )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)
        r;   )r9   r?   rX   rA   r   r   r   rV      s   z%MovingWindowSupport.get_moving_windowrI   rJ   
r?   r@   rX   rB   rA   rB   rC   rB   r   r5   )r?   r@   rX   rB   rA   rB   r   rZ   )	rK   rL   rM   rN   r+   r   rU   rV   rQ   r   r   r1   r   rS   ~   s    rS   c                      sP   e Zd ZdZd fddZe	ddddZedddZe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   r#   c                   rT   )N>   get_sliding_windowacquire_sliding_window_entryclear_sliding_windowr   rW   r.   r1   r   r   r+      s   z-SlidingWindowCounterSupport.__init_subclass__r>   r?   r@   rX   rB   rA   rC   r5   c                   rD   )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
        r;   rY   r   r   r   r^      s   z8SlidingWindowCounterSupport.acquire_sliding_window_entrytuple[int, float, int, float]c                   rD   )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
        r;   r9   r?   rA   r   r   r   r]      s   z.SlidingWindowCounterSupport.get_sliding_windowc                   s   dS )z
        Resets the rate limit key(s) for the sliding window

        :param key: the key to clear rate limits for
        :param expiry: the rate limit expiry, needed to compute the key in some implemenations
        Nr   ra   r   r   r   r_      s   z0SlidingWindowCounterSupport.clear_sliding_windowrI   rJ   r[   )r?   r@   rA   rB   r   r`   )r?   r@   rA   rB   r   r#   )
rK   rL   rM   rN   r+   r   r^   r]   r_   rQ   r   r   r1   r   r\      s    r\   )r   r   r   r   )
__future__r   r   abcr   r   Zdeprecated.sphinxr   Zlimitsr   Zlimits.storage.registryr   Zlimits.typingr   r	   r
   r   r   r   Zlimits.utilr   r!   r   rS   r\   r   r   r   r   <module>   s     
X-