
    5j_                        S SK Jr  S SKrS SKJrJr  S SKJr  S SKJ	r	  S SK
JrJrJrJrJr  S SKJr      SS jr " S	 S
\\	S9r " S S\5      r " S S\5      r " S S5      rg)    )annotationsN)ABCabstractmethod)errors)StorageRegistry)AnyCallablePRcast)LazyDependencyc                J   ^  [         R                  " T 5      SU 4S jj5       nU$ )Nc                    > [        [        U S   5      n T" U 0 UD6$ ! UR                   a.  nUR                  (       a  [        R
                  " U5      Uee S nAff = f)Nr   )r   Storagebase_exceptionswrap_exceptionsr   StorageError)argskwargsinstanceexcfns       R/root/lseg-credit/backend/venv/lib/python3.13/site-packages/limits/storage/base.pyinner_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                     ^  \ rS rSr% SrS\S'    SU 4S jjr  S     SU 4S jjjr\\	SS j5       5       r
\	SSS jj5       r\	SS	 j5       r\	SS
 j5       r\	SS j5       r\	SS j5       r\	SS j5       rSrU =r$ )r   "   z;
Base class to extend when implementing a storage backend.
zlist[str] | NoneSTORAGE_SCHEMEc                t   > S H!  n[        X[        [        X5      5      5        M#     [        TU ]  " S0 UD6  g )N>   getincrcheckclearreset
get_expiry setattrr   getattrsuper__init_subclass__clsr   method	__class__s      r   r0   Storage.__init_subclass__*   s7    
F Cgc.B!CD
 	!+F+r    c                .   > [         TU ]  5         X l        g)zu
:param wrap_exceptions: Whether to wrap storage exceptions in
 :exc:`limits.errors.StorageError` before raising it.
N)r/   __init__r   )selfurir   optionsr4   s       r   r7   Storage.__init__6   s     	.r    c                    [         e)NNotImplementedErrorr8   s    r   r   Storage.base_exceptionsD   s
     "!r    c                    [         e)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   keyexpiryamounts       r   r&   Storage.incrI   s
     "!r    c                    [         e)z2
:param key: the key to get the counter value for
r=   r8   rB   s     r   r%   Storage.getT   
    
 "!r    c                    [         e)z+
:param key: the key to get the expiry for
r=   rG   s     r   r*   Storage.get_expiry[   rI   r    c                    [         e)z
check if storage is healthy
r=   r?   s    r   r'   Storage.checkb   rI   r    c                    [         e)z
reset storage to clear limits
r=   r?   s    r   r)   Storage.reseti   rI   r    c                    [         e)zI
resets the rate limit key

:param key: the key to clear rate limits for
r=   rG   s     r   r(   Storage.clearp   s
     "!r    )r   r   r   r   None)NF)r9   z
str | Noner   boolr:   zfloat | str | bool)r   z-type[Exception] | tuple[type[Exception], ...]   )rB   strrC   intrD   rX   r   rX   )rB   rW   r   rX   )rB   rW   r   float)r   rT   )r   z
int | None)rB   rW   r   rS   )__name__
__module____qualname____firstlineno____doc____annotations__r0   r7   propertyr   r   r&   r%   r*   r'   r)   r(   __static_attributes____classcell__r4   s   @r   r   r   "   s     %$E
,  %// / &	/ / "  " " " " " " " " " " " " "r    r   )	metaclassc                  \   ^  \ rS rSrSrSU 4S jjr\SS	S jj5       r\S
S j5       rSr	U =r
$ )MovingWindowSupportz   z\
Abstract base class for storages that support
the :ref:`strategies:moving window` strategy
c                v   > S H"  n[        U U[        [        X5      5      5        M$     [        TU ]  " S0 UD6  g )N>   acquire_entryget_moving_windowr+   r,   r1   s      r   r0   %MovingWindowSupport.__init_subclass__   s>    
F WS12	
 	!+F+r    c                    [         e)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   rB   limitrC   rD   s        r   ri   !MovingWindowSupport.acquire_entry   s
     "!r    c                    [         e)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   rB   rn   rC   s       r   rj   %MovingWindowSupport.get_moving_window   s
     "!r    r+   rR   rU   
rB   rW   rn   rX   rC   rX   rD   rX   r   rT   )rB   rW   rn   rX   rC   rX   r   ztuple[float, int])rZ   r[   r\   r]   r^   r0   r   ri   rj   ra   rb   rc   s   @r   rf   rf   z   s5    

, " " 	" 	"r    rf   c                     ^  \ rS rSrSrSU 4S jjr\ S	         S
S jj5       r\      SS j5       r\SS j5       r	Sr
U =r$ )SlidingWindowCounterSupport   zf
Abstract base class for storages that support
the :ref:`strategies:sliding window counter` strategy.
c                v   > S H"  n[        U U[        [        X5      5      5        M$     [        TU ]  " S0 UD6  g )N>   get_sliding_windowclear_sliding_windowacquire_sliding_window_entryr+   r,   r1   s      r   r0   -SlidingWindowCounterSupport.__init_subclass__   s>    
F
 WS12
 	!+F+r    c                    [         e)a  
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=   rm   s        r   ry   8SlidingWindowCounterSupport.acquire_sliding_window_entry   s
     "!r    c                    [         e)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   rB   rC   s      r   rw   .SlidingWindowCounterSupport.get_sliding_window   s
     "!r    c                    g)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~   s      r   rx   0SlidingWindowCounterSupport.clear_sliding_window   s     	r    r+   rR   rU   rr   )rB   rW   rC   rX   r   ztuple[int, float, int, float])rB   rW   rC   rX   r   rS   )rZ   r[   r\   r]   r^   r0   r   ry   rw   rx   ra   rb   rc   s   @r   rt   rt      s    
, ?@"""",/"9<"	" " "" #"	&" "   r    rt   c                  ,    \ rS rSrSr\SS j5       rSrg)TimestampedSlidingWindow   z\Helper class for storage that support the sliding window counter, with timestamp based keys.c                P    U S[        X2-
  U-  5       3U S[        X2-  5       34$ )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")``.
/)rX   )r2   rB   rC   ats       r   sliding_window_keys,TimestampedSlidingWindow.sliding_window_keys   s:    $ aR[F2345#aBK@P?Q7RRRr    r+   N)rB   rW   rC   rX   r   rY   r   ztuple[str, str])rZ   r[   r\   r]   r^   classmethodr   ra   r+   r    r   r   r      s    fS Sr    r   )r   Callable[P, R]r   r   )
__future__r   r   abcr   r   limitsr   limits._storage_schemer   limits.typingr   r	   r
   r   r   limits.utilr   r   r   rf   rt   r   r+   r    r   <module>r      so    "  #  2  ' U"n U"p&"# &"R;# ;|S Sr    