o
    ;h)                     @  sv   d dl mZ d dlZd dlZd dlmZ d dlZd dlmZ ddl	m
Z
 ddlmZ ddlmZmZ G d	d
 d
ZdS )    )annotationsN)Iterable)
OrderedSet   )ExemptionScope)get_qualified_name)Limit
LimitGroupc                   @  s   e Zd ZdDddZedEddZedEddZdFddZdFddZ	dGdHdd Z	dId"d#Z
dJd&d'ZdKd(d)ZdLd,d-ZdMd.d/Z	0	0	0		dNdOd8d9ZdPd:d;ZdQd<d=ZdRd>d?ZdSdBdCZd0S )TLimitManagerapplication_limitslist[LimitGroup]default_limitsdecorated_limits!dict[str, OrderedSet[LimitGroup]]blueprint_limitsroute_exemptionsdict[str, ExemptionScope]blueprint_exemptionsreturnNonec                 C  s:   || _ || _|| _|| _|| _|| _i | _td| _	d S )Nzflask-limiter)
_application_limits_default_limits_decorated_limits_blueprint_limits_route_exemptions_blueprint_exemptions_endpoint_hintslogging	getLogger_logger)selfr   r   r   r   r   r    r!   H/var/www/html/venv/lib/python3.10/site-packages/flask_limiter/manager.py__init__   s   	zLimitManager.__init__list[Limit]c                 C     t tj| j S N)list	itertoolschainr   r    r!   r!   r"   r   "      zLimitManager.application_limitsc                 C  r%   r&   )r'   r(   r)   r   r*   r!   r!   r"   r   &   r+   zLimitManager.default_limitslimitsc                 C  
   || _ d S r&   )r   r    r,   r!   r!   r"   set_application_limits*      
z#LimitManager.set_application_limitsc                 C  r-   r&   )r   r.   r!   r!   r"   set_default_limits-   r0   zLimitManager.set_default_limitsFroutestrlimitLimitGroup | Noneoverrideboolc                 C  s:   |r|s| j |t | d S t|g| j |< d S d S r&   )r   
setdefaultr   add)r    r2   r4   r6   r!   r!   r"   add_decorated_limit0   s
   z LimitManager.add_decorated_limit	blueprintc                 C  s"   |r| j |t | d S d S r&   )r   r8   r   r9   )r    r;   r4   r!   r!   r"   add_blueprint_limit9   s   z LimitManager.add_blueprint_limitscoper   c                 C     || j |< d S r&   )r   )r    r2   r=   r!   r!   r"   add_route_exemption=      z LimitManager.add_route_exemptionc                 C  r>   r&   )r   )r    r;   r=   r!   r!   r"   add_blueprint_exemption@   r@   z$LimitManager.add_blueprint_exemptionendpointcallablec                 C  s   | j |t | d S r&   )r   r8   r   r9   )r    rB   rC   r!   r!   r"   add_endpoint_hintC   s   zLimitManager.add_endpoint_hintc                 C  s   t | j|S r&   )r7   r   get)r    rB   r!   r!   r"   	has_hintsF   s   zLimitManager.has_hintsNappflask.Flask
str | Nonecallable_namein_middlewaremarked_for_limitingtuple[list[Limit], ...]c                 C  s6  |o|}g }g }	|r;|s(|s|j |d }
|
rt|
nd}n|}|| | | j|t D ]
}|	| | q0|rS|sS|rJtdd |D rS|| || | 	|||}|rd|t
j@ sd| jng }tdd |D }tdd |D }|	rtdd |	D nd}|s|r|s|t
j@ r|r|| j7 }||fS )N c                 s      | ]}|j  V  qd S r&   override_defaults.0r4   r!   r!   r"   	<genexpr>d       z.LimitManager.resolve_limits.<locals>.<genexpr>c                 s      | ]}|j V  qd S r&   )Zmethod_exemptrR   r!   r!   r"   rT   o   s    c                 s  rO   r&   rP   rR   r!   r!   r"   rT   t   s    
c                 s  rO   r&   rP   rR   r!   r!   r"   rT   |   rU   F)view_functionsrE   r   extendr   r   r   allr   exemption_scoper   APPLICATIONr   DEFAULTr   )r    rG   rB   r;   rJ   rK   rL   Zbefore_request_contextr   Zhinted_limits	view_funcnamehintrZ   Z
all_limitsZexplicit_limits_exemptZcombined_defaultsZhinted_limits_request_defaultsr!   r!   r"   resolve_limitsI   s\   		
zLimitManager.resolve_limitsc                 C  s   |j |pdd }|rt|nd}| j|tj}|r!|j|nd }|s'|S |s+J | ||\}}	|tjtj	B  @ s>|	rI|	
 D ]}
||
O }qB||B S )NrN   )rW   rE   r   r   r   NONE
blueprints_blueprint_exemption_scoper\   r[   values)r    rG   rB   r;   r]   r^   Zroute_exemption_scopeblueprint_instanceZblueprint_exemption_scopeZancestor_exemption_scopes	exemptionr!   r!   r"   rZ      s(   

zLimitManager.exemption_scopec                 C  s   g }| j |tjsA|| jv rA| j| D ],}z|D ]}|| qW q ty@ } z| jd| d|  W Y d }~qd }~ww |S )Nz&failed to load ratelimit for function : )	r   rE   r   ra   r   append
ValueErrorr   error)r    rJ   r,   groupr4   er!   r!   r"   r      s   
zLimitManager.decorated_limitsc                   s&  g }|r
|j |nd }|r|j}t|r|dng } ||\}}|tjtjB  @ s j	|t
 }	|	r@tdd |	D s^ j|tjtj@ s^tj fdd| j	|D  n|	}
|
r|
D ],}z|dd |D  W qd ty } z jd| d|  W Y d }~qdd }~ww |S )	N.c                 s  rV   r&   rP   rR   r!   r!   r"   rT      s    
z0LimitManager.blueprint_limits.<locals>.<genexpr>c                 3  s    | ]
} j |g V  qd S r&   )r   rE   )rS   memberr*   r!   r"   rT      s
    
c                 S  sB   g | ]}t |j|j|j|j|j|j|j|j|j	|j
|j|jqS r!   )r   r4   Zkey_funcr=   Z
per_methodmethodserror_messageZexempt_whenrQ   Zdeduct_whenZ	on_breachZcostZsharedrR   r!   r!   r"   
<listcomp>   s"    z1LimitManager.blueprint_limits.<locals>.<listcomp>z'failed to load ratelimit for blueprint rg   )rb   rE   r^   setsplitrc   r   r\   r[   r   r   rY   r   ra   	ANCESTORSr(   r)   intersection
differencerX   ri   r   rj   )r    rG   r;   r,   re   blueprint_nameZblueprint_ancestoryZself_exemptionZancestor_exemptionsZblueprint_self_limitsr   Zlimit_grouprl   r!   r*   r"   r      sb   

zLimitManager.blueprint_limitsrw   0tuple[ExemptionScope, dict[str, ExemptionScope]]c                   sb   |j | j} j|tjtj @ }t|d}dd  j	 D 
|}| fdd|D fS )Nrm   c                 S  s   h | ]\}}|t j@ r|qS r!   )r   ZDESCENDENTS)rS   kfr!   r!   r"   	<setcomp>   s    z:LimitManager._blueprint_exemption_scope.<locals>.<setcomp>c                   s   i | ]}| j |tjqS r!   )r   rE   r   ra   )rS   ry   r*   r!   r"   
<dictcomp>  s    z;LimitManager._blueprint_exemption_scope.<locals>.<dictcomp>)rb   r^   r   rE   r   ra   rt   rr   rs   itemsru   )r    rG   rw   r^   rf   Z	ancestoryZancestor_exemptionr!   r*   r"   rc      s   z'LimitManager._blueprint_exemption_scope)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r$   )r,   r   r   r   )F)r2   r3   r4   r5   r6   r7   r   r   )r;   r3   r4   r5   r   r   )r2   r3   r=   r   r   r   )r;   r3   r=   r   r   r   )rB   r3   rC   r3   r   r   )rB   r3   r   r7   )NNNFF)rG   rH   rB   rI   r;   rI   rJ   rI   rK   r7   rL   r7   r   rM   )rG   rH   rB   rI   r;   rI   r   r   )rJ   r3   r   r$   )rG   rH   r;   r3   r   r$   )rG   rH   rw   r3   r   rx   )__name__
__module____qualname__r#   propertyr   r   r/   r1   r:   r<   r?   rA   rD   rF   r`   rZ   r   r   rc   r!   r!   r!   r"   r
      s2    



	




@

Cr
   )
__future__r   r(   r   collections.abcr   ZflaskZordered_setr   	constantsr   utilr   Zwrappersr   r	   r
   r!   r!   r!   r"   <module>   s    