
    Vh"                        d dl mZ d dlZd dlZd dlZd dlmZmZ d dlm	Z	 d dl
Zd dlmZmZmZmZ  G d d      Z G d d	eeee      Zy)
    )annotationsN)Counterdefaultdict)floor)MovingWindowSupportSlidingWindowCounterSupportStorageTimestampedSlidingWindowc                      e Zd ZddZy)Entryc                ^    t        j                          | _        | j                  |z   | _        y N)timeatimeexpiry)selfr   s     c/var/www/catia.catastroantioquia-mas.com/tasa/lib/python3.12/site-packages/limits/storage/memory.py__init__zEntry.__init__   s    YY[
jj6)    N)r   floatreturnNone)__name__
__module____qualname__r    r   r   r   r      s    *r   r   c                      e Zd ZdZdgZdd fdZddZddZddZddZ	e
	 	 dd       Zddd	Zddd
ZddZd dZdd!dZd"dZd#dZ	 d	 	 	 	 	 	 	 	 	 d!dZ	 	 	 	 	 	 	 	 	 	 d$dZ	 	 	 	 	 	 d%dZd&dZd'dZd(dZ xZS ))MemoryStoragez
    rate limit storage using :class:`collections.Counter`
    as an in memory storage for fixed and sliding window strategies,
    and a simple list to implement moving window strategy.

    memoryc                   t               | _        t        t        j                        | _        i | _        i | _        t        j                  d| j                        | _
        | j                  j                          t        | 4  |fd|i| y )N{Gz?wrap_exceptions)r   storager   	threadingRLocklocksexpirationseventsTimer_MemoryStorage__expire_eventstimerstartsuperr   )r   urir"   _	__class__s       r   r   zMemoryStorage.__init__$   sj    3:98CIOO8T
-/.0&/oodD<P<P&Q


CoCCr   c                F    | j                   j                         }|d= |d= |S )Nr+   r&   )__dict__copyr   states     r   __getstate__zMemoryStorage.__getstate__-   s'    ""$'N'Nr   c                    | j                   j                  |       t        t        j                        | _        t        j                  d| j                        | _        | j                  j                          y Nr!   )
r2   updater   r$   r%   r&   r)   r*   r+   r,   r4   s     r   __setstate__zMemoryStorage.__setstate__3   sL    U# 1
__T4+?+?@


r   c                "   t        | j                  j                               D ]  }| j                  |   5  | j                  j	                  |g       x}rKt        j                  |t        j                          d       }| j                  |   d | | j                  |<   | j                  j	                  |d       s| j                  j                  |d        d d d         t        | j                  j                               D ]{  }| j                  |   t        j                         k  s(| j                  j                  |d        | j                  j                  |d        | j                  j                  |d        } y # 1 sw Y   jxY w)Nc                    | j                    S r   )r   )events    r   <lambda>z/MemoryStorage.__expire_events.<locals>.<lambda>>   s     r   key)listr(   keysr&   getbisectbisect_leftr   popr'   r#   )r   r@   r(   oldests       r   __expire_eventszMemoryStorage.__expire_events9   s@   ((*+ 	.CC .![[__S"5565#//2MF (,{{3'7'@DKK${{sD1JJNN3-. .	. ((--/0 	*C$		3  d+  $$S$/

sD)		*. .s   B"FF	c                    | j                   j                         s@t        j                  d| j                        | _         | j                   j                          y y r8   )r+   is_aliver$   r)   r*   r,   r   s    r   __schedule_expiryzMemoryStorage.__schedule_expiryI   s?    zz""$"t/C/CDDJJJ %r   c                    t         S r   )
ValueErrorrK   s    r   base_exceptionszMemoryStorage.base_exceptionsN   s
     r   c                ^   | j                  |       | j                          | j                  |   5  | j                  |xx   |z  cc<   | j                  |   |k(  r$t	        j                         |z   | j
                  |<   ddd       | j                  j                  |d      S # 1 sw Y   %xY w)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
        Nr   )rC   _MemoryStorage__schedule_expiryr&   r#   r   r'   )r   r@   r   amounts       r   incrzMemoryStorage.incrT   s     	 ZZ_ 	=LL'||C F*(,		f(<  %	= ||Q''		= 	=s   AB##B,c                   | j                  |       | j                          | j                  |   5  t        | j                  |   |z
  d      | j                  |<   ddd       | j                  j                  |d      S # 1 sw Y   %xY w)z
        decrements the counter for a given rate limit key

        :param key: the key to decrement
        :param amount: the number to decrement by
        r   N)rC   rQ   r&   maxr#   )r   r@   rR   s      r   decrzMemoryStorage.decrd   s{     	 ZZ_ 	C #DLL$5$> BDLL	C ||Q''	C 	Cs   *A??Bc                D   | j                   j                  |d      t        j                         k  rT| j                  j	                  |d       | j                   j	                  |d       | j
                  j	                  |d       | j                  j                  |d      S )zB
        :param key: the key to get the counter value for
        r   N)r'   rC   r   r#   rF   r&   r   r@   s     r   rC   zMemoryStorage.getr   sx    
 Q'499;6LLS$'  d+JJNN3%||Q''r   c                    | j                   j                  |d       | j                  j                  |d       | j                  j                  |d       | j                  j                  |d       y)z>
        :param key: the key to clear rate limits for
        N)r#   rF   r'   r(   r&   rX   s     r   clearzMemoryStorage.clear~   sR     	d#S$'T"

sD!r   c                   ||kD  ry| j                          | j                  |   5  | j                  j                  |g        t	        j                         }	 | j                  |   ||z
     }|r|j                  ||z
  k\  r
	 ddd       yt        |      g|z  | j                  |   dd 	 ddd       y# t
        $ r d}Y Tw xY w# 1 sw Y   yxY w)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
        FNr   T)rQ   r&   r(   
setdefaultr   
IndexErrorr   r   )r   r@   limitr   rR   	timestampentrys          r   acquire_entryzMemoryStorage.acquire_entry   s     E> ZZ_ 	KK""3+		IC(8 	F(::	 	 ).f'?C !$	 	
  	 	s5   1CB4-C C4C?CCCCc                ^    | j                   j                  |t        j                               S )z;
        :param key: the key to get the expiry for
        )r'   rC   r   rX   s     r   
get_expiryzMemoryStorage.get_expiry   s#    
 ##C55r   c                    t        j                          }| j                  j                  |g       x}r1t        j                  |||z
   d       }||dz
     j
                  |fS |dfS )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)
        c                    | j                    S r   )r   )r`   s    r   r>   z1MemoryStorage.get_moving_window.<locals>.<lambda>   s    %++ r   r?      r   )r   r(   rC   rD   rE   r   )r   r@   r^   r   r_   r(   rG   s          r   get_moving_windowzMemoryStorage.get_moving_window   so     IIK	[[__S"--6-'')f,-3MF &1*%++V33!|r   c                V   ||kD  ryt        j                          }| j                  |||      \  }}| j                  ||||      \  }}	}
}||	z  |z  |
z   }t        |      |z   |kD  ry| j	                  |d|z  |      }
||	z  |z  |
z   }t        |      |kD  r| j                  ||       yy)NF   )rR   T)r   sliding_window_keys_get_sliding_window_infor   rS   rV   )r   r@   r^   r   rR   nowprevious_keycurrent_keyprevious_countprevious_ttlcurrent_countr/   weighted_counts                r   acquire_sliding_window_entryz*MemoryStorage.acquire_sliding_window_entry   s     E>iik$($<$<S&#$N!k )),VSQ	
',6?-O 6)E1 !IIk1v:fIMM+l:VCmSN^$u, 		+v.r   c                    | j                  |      }| j                  |      }|dk(  rt        d      }nd||z
  |z  dz  z
  |z  }d||z  dz  z
  |z  |z   }||||fS )Nr   rf   )rC   r   )	r   rm   rn   r   rl   ro   rq   rp   current_ttls	            r   rk   z&MemoryStorage._get_sliding_window_info   sz     ,/-Q 8L3<6"9Q!>?6ILS6\Q./69FB|]KGGr   c                ~    t        j                          }| j                  |||      \  }}| j                  ||||      S r   )r   rj   rk   r   r@   r   rl   rm   rn   s         r   get_sliding_windowz MemoryStorage.get_sliding_window   s@     iik$($<$<S&#$N!k,,\;PSTTr   c                    t        j                          }| j                  |||      \  }}| j                  |       | j                  |       y r   )r   rj   rZ   rw   s         r   clear_sliding_windowz"MemoryStorage.clear_sliding_window   s>    iik$($<$<S&#$N!k

< 

;r   c                     y)z-
        check if storage is healthy
        Tr   rK   s    r   checkzMemoryStorage.check   s    
 r   c                :   t        t        | j                        t        | j                              }| j                  j	                          | j
                  j	                          | j                  j	                          | j                  j	                          |S r   )rU   lenr#   r(   rZ   r'   r&   )r   	num_itemss     r   resetzMemoryStorage.reset   sh    DLL)3t{{+;<	 

r   )NF)r.   z
str | Noner"   boolr/   str)r   dict[str, limits.typing.Any])r5   r   r   r   )r   r   )r   z-type[Exception] | tuple[type[Exception], ...])rf   )r@   r   r   r   rR   intr   r   )r@   r   rR   r   r   r   )r@   r   r   r   )r@   r   r   r   )
r@   r   r^   r   r   r   rR   r   r   r   )r@   r   r   r   )r@   r   r^   r   r   r   r   ztuple[float, int])
rm   r   rn   r   r   r   rl   r   r   tuple[int, float, int, float])r@   r   r   r   r   r   )r@   r   r   r   r   r   )r   r   )r   z
int | None)r   r   r   __doc__STORAGE_SCHEMEr   r6   r:   r*   rQ   propertyrO   rS   rV   rC   rZ   ra   rc   rg   rs   rk   rx   rz   r|   r   __classcell__)r0   s   @r   r   r      s    ZND* 
 	6 
( (
("26,   	
  
BHH H 	H
 H 
'H UU #U	&U r   r   )
__future__r   rD   r$   r   collectionsr   r   mathr   limits.typinglimitslimits.storage.baser   r   r	   r
   r   r   r   r   r   <module>r      sF    "    ,   * *k "=?Wkr   