
    9i`                        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Z G d	 d
ee	      Z G d de      Z G d de      Z G d d      Zy)    )annotationsN)ABCabstractmethod)errors)StorageRegistry)AnyCallablePRcast)LazyDependencyc                D     t        j                         d fd       }|S )Nc                     t        t        | d         }	  | i |S # |j                  $ r(}|j                  rt	        j
                  |      | d }~ww xY w)Nr   )r   Storagebase_exceptionswrap_exceptionsr   StorageError)argskwargsinstanceexcfns       F/var/www/html/venv/lib/python3.12/site-packages/limits/storage/base.pyinnerz_wrap_errors.<locals>.inner   s]    a)	t&v&&'' 	''))#.C7	s    A#AA)r   zP.argsr   zP.kwargsreturnr   )	functoolswraps)r   r   s   ` r   _wrap_errorsr      s'     __R  L    c                       e Zd ZU dZded<   	 d fdZ	 	 d	 	 	 	 	 d fdZeedd              Z	eddd       Z
edd       Zedd	       Zedd
       Zedd       Zedd       Z xZS )r   zC
    Base class to extend when implementing a storage backend.
    zlist[str] | NoneSTORAGE_SCHEMEc                r    dD ]"  }t        | |t        t        | |                   $ t        |   di | y )N>   getincrcheckclearreset
get_expiry setattrr   getattrsuper__init_subclass__clsr   method	__class__s      r   r.   zStorage.__init_subclass__*   sB    
 	EF Cgc6.B!CD	E 	!+F+r   c                0    t         |           || _        y)z
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        N)r-   __init__r   )selfurir   optionsr2   s       r   r4   zStorage.__init__6   s     	.r   c                    t         )NNotImplementedErrorr5   s    r   r   zStorage.base_exceptionsD   s
     "!r   c                    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
        r9   )r5   keyexpiryamounts       r   r$   zStorage.incrI   s
     "!r   c                    t         )zB
        :param key: the key to get the counter value for
        r9   r5   r=   s     r   r#   zStorage.getT   
    
 "!r   c                    t         )z;
        :param key: the key to get the expiry for
        r9   rA   s     r   r(   zStorage.get_expiry[   rB   r   c                    t         )z-
        check if storage is healthy
        r9   r;   s    r   r%   zStorage.checkb   rB   r   c                    t         )z/
        reset storage to clear limits
        r9   r;   s    r   r'   zStorage.reseti   rB   r   c                    t         )za
        resets the rate limit key

        :param key: the key to clear rate limits for
        r9   rA   s     r   r&   zStorage.clearp   s
     "!r   r   r   r   None)NF)r6   z
str | Noner   boolr7   zfloat | str | bool)r   z-type[Exception] | tuple[type[Exception], ...]   )r=   strr>   intr?   rM   r   rM   )r=   rL   r   rM   )r=   rL   r   float)r   rI   )r   z
int | None)r=   rL   r   rH   )__name__
__module____qualname____doc____annotations__r.   r4   propertyr   r   r$   r#   r(   r%   r'   r&   __classcell__r2   s   @r   r   r   "   s     %$E
,  %// / &	/ "  " " " " " " " " " " " " "r   r   )	metaclassc                  J     e Zd ZdZd fdZeddd       Zedd       Z xZS )	MovingWindowSupportzh
    Abstract base class for storages that support
    the :ref:`strategies:moving window` strategy
    c                r    dD ]"  }t        | |t        t        | |                   $ t        |   di | y )N>   acquire_entryget_moving_windowr)   r*   r/   s      r   r.   z%MovingWindowSupport.__init_subclass__   sE    
 	F WS&12		 	!+F+r   c                    t         )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
        r9   r5   r=   limitr>   r?   s        r   r[   z!MovingWindowSupport.acquire_entry   s
     "!r   c                    t         )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)
        r9   )r5   r=   r_   r>   s       r   r\   z%MovingWindowSupport.get_moving_window   s
     "!r   rG   rJ   
r=   rL   r_   rM   r>   rM   r?   rM   r   rI   )r=   rL   r_   rM   r>   rM   r   ztuple[float, int])	rO   rP   rQ   rR   r.   r   r[   r\   rU   rV   s   @r   rY   rY   z   s5    

, " " 	" 	"r   rY   c                  |     e Zd ZdZd fdZe	 d	 	 	 	 	 	 	 	 	 dd       Ze	 	 	 	 	 	 d	d       Zed
d       Z xZ	S )SlidingWindowCounterSupportzr
    Abstract base class for storages that support
    the :ref:`strategies:sliding window counter` strategy.
    c                r    dD ]"  }t        | |t        t        | |                   $ t        |   di | y )N>   get_sliding_windowclear_sliding_windowacquire_sliding_window_entryr)   r*   r/   s      r   r.   z-SlidingWindowCounterSupport.__init_subclass__   sE    
 		F
 WS&12		 	!+F+r   c                    t         )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
        r9   r^   s        r   rg   z8SlidingWindowCounterSupport.acquire_sliding_window_entry   s
     "!r   c                    t         )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
        r9   r5   r=   r>   s      r   re   z.SlidingWindowCounterSupport.get_sliding_window   s
     "!r   c                     y)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)   rj   s      r   rf   z0SlidingWindowCounterSupport.clear_sliding_window   s     	r   rG   rJ   ra   )r=   rL   r>   rM   r   ztuple[int, float, int, float])r=   rL   r>   rM   r   rH   )
rO   rP   rQ   rR   r.   r   rg   re   rf   rU   rV   s   @r   rc   rc      s    
, ?@"""",/"9<"	" " "" #"	&" "   r   rc   c                  "    e Zd ZdZedd       Zy)TimestampedSlidingWindowz\Helper class for storage that support the sliding window counter, with timestamp based keys.c                T    | dt        ||z
  |z         | dt        ||z         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")``.
        /)rM   )r0   r=   r>   ats       r   sliding_window_keysz,TimestampedSlidingWindow.sliding_window_keys   s>    $ aR&[F2345#aBK@P?Q7RRRr   N)r=   rL   r>   rM   rp   rN   r   ztuple[str, str])rO   rP   rQ   rR   classmethodrq   r)   r   r   rm   rm      s    fS Sr   rm   )r   Callable[P, R]r   rs   )
__future__r   r   abcr   r   limitsr   limits.storage.registryr   limits.typingr   r	   r
   r   r   limits.utilr   r   r   rY   rc   rm   r)   r   r   <module>rz      so    "  #  3  ' U"n U"p&"# &"R;# ;|S Sr   