o
    ;h`                     @  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mZmZmZmZ d dlmZ dddZG dd dee	dZG dd deZG dd deZG dd dZdS )    )annotationsN)ABCabstractmethod)errors)StorageRegistry)AnyCallablePRcast)LazyDependencyfnCallable[P, R]returnc                   s   t  d	 fdd}|S )
NargsP.argskwargsP.kwargsr   r
   c               
     sN   t t| d }z | i |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    F/var/www/html/venv/lib/python3.10/site-packages/limits/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   c                      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   zC
    Base class to extend when implementing a storage backend.
    zlist[str] | NoneZSTORAGE_SCHEMEr   r   r   Nonec                   6   dD ]}t | |tt| | qt jdi | d S )N>   
get_expiryclearincrgetcheckresetr   setattrr   getattrsuper__init_subclass__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   )selfr2   r   r5   r0   r   r   r7   6   s   

zStorage.__init__-type[Exception] | tuple[type[Exception], ...]c                 C     t )NNotImplementedErrorr8   r   r   r   r   D   s   zStorage.base_exceptions   keystrexpiryintamountc                 C  r:   )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;   )r8   r?   rA   rC   r   r   r   r$   I   s   	zStorage.incrc                 C  r:   )zB
        :param key: the key to get the counter value for
        r;   r8   r?   r   r   r   r%   T      zStorage.getfloatc                 C  r:   )z;
        :param key: the key to get the expiry for
        r;   rD   r   r   r   r"   [   rE   zStorage.get_expiryc                 C  r:   )z-
        check if storage is healthy
        r;   r=   r   r   r   r&   b   rE   zStorage.check
int | Nonec                 C  r:   )z/
        reset storage to clear limits
        r;   r=   r   r   r   r'   i   rE   zStorage.resetc                 C  r:   )za
        resets the rate limit key

        :param key: the key to clear rate limits for
        r;   rD   r   r   r   r#   p   s   zStorage.clearr   r   r   r    )NF)r2   r3   r   r4   r5   r6   )r   r9   r>   )r?   r@   rA   rB   rC   rB   r   rB   )r?   r@   r   rB   )r?   r@   r   rF   )r   r4   )r   rG   )r?   r@   r   r    )__name__
__module____qualname____doc____annotations__r,   r7   propertyr   r   r$   r%   r"   r&   r'   r#   __classcell__r   r   r0   r   r   "   s.   
 
r   )	metaclassc                      s@   e Zd ZdZd fddZeddddZedddZ  ZS )MovingWindowSupportzh
    Abstract base class for storages that support
    the :ref:`strategies:moving window` strategy
    r   r   r   r    c                   r!   )N>   acquire_entryget_moving_windowr   r(   r-   r0   r   r   r,      s   z%MovingWindowSupport.__init_subclass__r>   r?   r@   limitrB   rA   rC   r4   c                 C  r:   )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;   r8   r?   rU   rA   rC   r   r   r   rS         z!MovingWindowSupport.acquire_entrytuple[float, int]c                 C  r:   )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;   )r8   r?   rU   rA   r   r   r   rT      s   
z%MovingWindowSupport.get_moving_windowrH   rI   
r?   r@   rU   rB   rA   rB   rC   rB   r   r4   )r?   r@   rU   rB   rA   rB   r   rX   )	rJ   rK   rL   rM   r,   r   rS   rT   rP   r   r   r0   r   rR   z   s    	rR   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 )SlidingWindowCounterSupportzr
    Abstract base class for storages that support
    the :ref:`strategies:sliding window counter` strategy.
    r   r   r   r    c                   r!   )N>   get_sliding_windowacquire_sliding_window_entryclear_sliding_windowr   r(   r-   r0   r   r   r,      s   z-SlidingWindowCounterSupport.__init_subclass__r>   r?   r@   rU   rB   rA   rC   r4   c                 C  r:   )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;   rV   r   r   r   r\      s   z8SlidingWindowCounterSupport.acquire_sliding_window_entrytuple[int, float, int, float]c                 C  r:   )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;   r8   r?   rA   r   r   r   r[      s   z.SlidingWindowCounterSupport.get_sliding_windowc                 C  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   r_   r   r   r   r]      rW   z0SlidingWindowCounterSupport.clear_sliding_windowrH   rI   rY   )r?   r@   rA   rB   r   r^   )r?   r@   rA   rB   r   r    )
rJ   rK   rL   rM   r,   r   r\   r[   r]   rP   r   r   r0   r   rZ      s    rZ   c                   @  s   e Zd ZdZedd
dZdS )TimestampedSlidingWindowz\Helper class for storage that support the sliding window counter, with timestamp based keys.r?   r@   rA   rB   atrF   r   tuple[str, str]c                 C  s0   | dt || |  | dt ||  fS )a-  
        returns the previous and the current window's keys.

        :param key: the key to get the window's keys from
        :param expiry: the expiry of the limit item, in seconds
        :param at: the timestamp to get the keys from. Default to now, ie ``time.time()``

        Returns a tuple with the previous and the current key: (previous, current).

        Example:
          - key = "mykey"
          - expiry = 60
          - at = 1738576292.6631825

        The return value will be the tuple ``("mykey/28976271", "mykey/28976270")``.
        /)rB   )r.   r?   rA   ra   r   r   r   sliding_window_keys   s   0z,TimestampedSlidingWindow.sliding_window_keysN)r?   r@   rA   rB   ra   rF   r   rb   )rJ   rK   rL   rM   classmethodrd   r   r   r   r   r`      s    r`   )r   r   r   r   )
__future__r   r   abcr   r   Zlimitsr   Zlimits.storage.registryr   Zlimits.typingr   r   r	   r
   r   Zlimits.utilr   r   r   rR   rZ   r`   r   r   r   r   <module>   s    
X)>