o
    KDi`                     @  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   StorageError)r   r   instanceexcr    L/var/www/Datamplify/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] | NoneSTORAGE_SCHEMEr   r   r   Nonec                   6   dD ]}t | |tt| | qt jdi | d S )N>   getincrcheckclearreset
get_expiryr   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   )selfr4   r   r7   r2   r   r   r9   6   s   

zStorage.__init__-type[Exception] | tuple[type[Exception], ...]c                 C     t )NNotImplementedErrorr:   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=   )r:   rA   rC   rE   r   r   r   r%   I   s   	zStorage.incrc                 C  r<   )zB
        :param key: the key to get the counter value for
        r=   r:   rA   r   r   r   r$   T      zStorage.getfloatc                 C  r<   )z;
        :param key: the key to get the expiry for
        r=   rF   r   r   r   r)   [   rG   zStorage.get_expiryc                 C  r<   )z-
        check if storage is healthy
        r=   r?   r   r   r   r&   b   rG   zStorage.check
int | Nonec                 C  r<   )z/
        reset storage to clear limits
        r=   r?   r   r   r   r(   i   rG   zStorage.resetc                 C  r<   )za
        resets the rate limit key

        :param key: the key to clear rate limits for
        r=   rF   r   r   r   r'   p   s   zStorage.clearr   r   r   r"   )NF)r4   r5   r   r6   r7   r8   )r   r;   r@   )rA   rB   rC   rD   rE   rD   r   rD   )rA   rB   r   rD   )rA   rB   r   rH   )r   r6   )r   rI   )rA   rB   r   r"   )__name__
__module____qualname____doc____annotations__r.   r9   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 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/   r2   r   r   r.      s   z%MovingWindowSupport.__init_subclass__r@   rA   rB   limitrD   rC   rE   r6   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=   r:   rA   rW   rC   rE   r   r   r   rU         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=   )r:   rA   rW   rC   r   r   r   rV      s   
z%MovingWindowSupport.get_moving_windowrJ   rK   
rA   rB   rW   rD   rC   rD   rE   rD   r   r6   )rA   rB   rW   rD   rC   rD   r   rZ   )	rL   rM   rN   rO   r.   r   rU   rV   rR   r   r   r2   r   rT   z   s    	rT   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clear_sliding_windowacquire_sliding_window_entryr   r*   r/   r2   r   r   r.      s   z-SlidingWindowCounterSupport.__init_subclass__r@   rA   rB   rW   rD   rC   rE   r6   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=   rX   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=   r:   rA   rC   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   ra   r   r   r   r^      rY   z0SlidingWindowCounterSupport.clear_sliding_windowrJ   rK   r[   )rA   rB   rC   rD   r   r`   )rA   rB   rC   rD   r   r"   )
rL   rM   rN   rO   r.   r   r_   r]   r^   rR   r   r   r2   r   r\      s    r\   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.rA   rB   rC   rD   atrH   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")``.
        /)rD   )r0   rA   rC   rc   r   r   r   sliding_window_keys   s   0z,TimestampedSlidingWindow.sliding_window_keysN)rA   rB   rC   rD   rc   rH   r   rd   )rL   rM   rN   rO   classmethodrf   r   r   r   r   rb      s    rb   )r   r   r   r   )
__future__r   r   abcr   r   limitsr   limits.storage.registryr   limits.typingr   r   r	   r
   r   limits.utilr   r    r   rT   r\   rb   r   r   r   r   <module>   s    
X)>