a
    a*j*                     @  s4  U d Z ddlmZ ddlZddlmZmZ ddlmZm	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 ddlmZmZ ddlmZ G dd dedZG dd deZG dd deZeddG dd deZedddG dd deZeee ee ee ee f Z eeeedZ!de"d< dS )z
Rate limiting strategies
    )annotationsN)ABCMetaabstractmethod)floorinf)
deprecatedversionadded)SlidingWindowCounterSupport   )RateLimitItem)MovingWindowSupportStorageStorageTypes)Unioncast)WindowStatsc                   @  s~   e Zd ZddddZeddddd	d
dddZeddddd	d
dddZeddddddZddddddZdS )RateLimiterr   storagec                 C  s   t |tsJ || _d S N)
isinstancer   r   selfr    r   M/var/www/html/assistant/venv/lib/python3.9/site-packages/limits/strategies.py__init__   s    zRateLimiter.__init__r
   costr   strintboolitemidentifiersr   returnc                G  s   t dS )
        Consume the rate limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The cost of this hit, default 1
        NNotImplementedErrorr   r"   r   r#   r   r   r   hit   s    
zRateLimiter.hitc                G  s   t dS )a  
        Check the rate limit without consuming from it.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
          instance of the limit
        :param cost: The expected cost to be consumed, default 1
        Nr&   r(   r   r   r   test&   s    
zRateLimiter.testr   r"   r#   r$   c                 G  s   t dS )
  
        Query the reset time and remaining amount for the limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: (reset time, remaining)
        Nr&   r   r"   r#   r   r   r   get_window_stats2   s    
zRateLimiter.get_window_statsNonec                 G  s   | j |j| S r   )r   clearkey_forr-   r   r   r   r0   >   s    zRateLimiter.clearN)	__name__
__module____qualname__r   r   r)   r*   r.   r0   r   r   r   r   r      s   r   )	metaclassc                      sl   e Zd ZdZdd fddZdddd	d
ddddZdddd	d
ddddZdd	ddddZ  ZS )MovingWindowRateLimiterz4
    Reference: :ref:`strategies:moving window`
    r   r   c                   s2   t |ds"t |ds"td|j t | d S )Nacquire_entryget_moving_windowzBMovingWindowRateLimiting is not implemented for storage of type %shasattrr'   	__class__superr   r   r;   r   r   r   G   s    z MovingWindowRateLimiter.__init__r
   r   r   r   r   r    r!   c                G  s&   t t| jj|j| |j| |dS )a  
        Consume the rate limit

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The cost of this hit, default 1
        :return: (reset time, remaining)
        )amount)r   r   r   r7   r1   r>   
get_expiryr(   r   r   r   r)   Q   s    zMovingWindowRateLimiter.hitc                G  s0   t t| j|j| |j| d |j| kS a  
        Check if the rate limit can be consumed

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :param cost: The expected cost to be consumed, default 1
        r
   )r   r   r   r8   r1   r>   r?   r(   r   r   r   r*   `   s    zMovingWindowRateLimiter.testr   r+   c                 G  sB   t t| j|j| |j| \}}||  }t||j| S )a  
        returns the number of requests remaining within this limit.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: tuple (reset time, remaining)
        )r   r   r   r8   r1   r>   r?   r   )r   r"   r#   Zwindow_startZwindow_itemsresetr   r   r   r.   s   s    	z(MovingWindowRateLimiter.get_window_stats)	r2   r3   r4   __doc__r   r)   r*   r.   __classcell__r   r   r=   r   r6   B   s
   
r6   c                   @  sV   e Zd ZdZdddddddd	d
ZdddddddddZddddddZdS )FixedWindowRateLimiterz3
    Reference: :ref:`strategies:fixed window`
    r
   r   r   r   r   r    r!   c                G  s$   | j j|j| | d|d|jkS )r%   FZelastic_expiryr>   r   incrr1   r?   r>   r(   r   r   r   r)      s    zFixedWindowRateLimiter.hitc                G  s    | j |j| |j| d k S r@   )r   getr1   r>   r(   r   r   r   r*      s    
zFixedWindowRateLimiter.testr   r+   c                 G  s:   t d|j| j|j|  }| j|j| }t||S )r,   r   )maxr>   r   rH   r1   r?   r   )r   r"   r#   	remainingrA   r   r   r   r.      s    	z'FixedWindowRateLimiter.get_window_statsN)r2   r3   r4   rB   r)   r*   r.   r   r   r   r   rD      s   rD   z4.1)versionc                      s   e Zd ZdZdd fddZdddddd	d
dZdddddddddZdddddddddZddddddZ  Z	S )SlidingWindowCounterRateLimiterz=
    Reference: :ref:`strategies:sliding window counter`
    r   r   c                   s2   t |drt |ds"td|j t | d S )Nget_sliding_windowacquire_sliding_window_entryzJSlidingWindowCounterRateLimiting is not implemented for storage of type %sr9   r   r=   r   r   r      s    z(SlidingWindowCounterRateLimiter.__init__r   r   float)r"   previous_countprevious_expires_incurrent_countr$   c                 C  s   || |   | S )zu
        Return the approximated by weighting the previous window count and adding the current window count.
        )r?   )r   r"   rP   rQ   rR   r   r   r   _weighted_count   s    
z/SlidingWindowCounterRateLimiter._weighted_countr
   r   r   r    r!   c                G  s$   t t| j|j| |j| |S )r%   )r   r	   r   rN   r1   r>   r?   r(   r   r   r   r)      s    	z#SlidingWindowCounterRateLimiter.hitc                G  sD   t t| j|j| | \}}}}| |||||j| d k S r@   )r   r	   r   rM   r1   r?   rS   r>   )r   r"   r   r#   rP   rQ   rR   _r   r   r   r*      s    	z$SlidingWindowCounterRateLimiter.testr   r+   c              
   G  s   t t| j|j| | \}}}}td|jt| 	|||| }t

 }|s`|s`t||S | }	tt }
}|r||	|  }
|r||	 }t|t|
| |S )a  
        Query the reset time and remaining amount for the limit.

        :param item: The rate limit item
        :param identifiers: variable list of strings to uniquely identify this
         instance of the limit
        :return: WindowStats(reset time, remaining)
        r   )r   r	   r   rM   r1   r?   rI   r>   r   rS   timer   r   min)r   r"   r#   rP   rQ   rR   Zcurrent_expires_inrJ   nowZexpiryZprevious_reset_inZcurrent_reset_inr   r   r   r.      s2    	


z0SlidingWindowCounterRateLimiter.get_window_stats)
r2   r3   r4   rB   r   rS   r)   r*   r.   rC   r   r   r=   r   rL      s   
rL   always)rK   actionc                   @  s*   e Zd ZdZdddddddd	d
ZdS )#FixedWindowElasticExpiryRateLimiterzG
    Reference: :ref:`strategies:fixed window with elastic expiry`
    r
   r   r   r   r   r    r!   c                G  s$   | j j|j| | d|d|jkS )r%   TrE   rF   r(   r   r   r   r)   '  s    z'FixedWindowElasticExpiryRateLimiter.hitN)r2   r3   r4   rB   r)   r   r   r   r   rZ   !  s   rZ   )zsliding-window-counterzfixed-windowzfixed-window-elastic-expiryzmoving-windowzdict[str, KnownStrategy]
STRATEGIES)#rB   
__future__r   rU   abcr   r   mathr   r   Zdeprecated.sphinxr   r   Zlimits.storage.baser	   Zlimitsr   r   r   r   r   typingr   r   utilr   r   r6   rD   rL   rZ   typeZKnownStrategyr[   __annotations__r   r   r   r   <module>   s:   -B4h
