o
    si(                     @  s   d dl mZ d dlZd dlZd dlmZmZmZmZ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)DictIterableListOptionalTuple)
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%   T/var/www/html/pca-backend/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$   r0   r%   r%   r&   set_application_limits*      
z#LimitManager.set_application_limitsc                 C  r1   r*   )r   r2   r%   r%   r&   set_default_limits-   r4   zLimitManager.set_default_limitsFroutestrlimitOptional[LimitGroup]overrideboolc                 C  s:   |r|s| j |t | d S t|g| j |< d S d S r*   )r   
setdefaultr   add)r$   r6   r8   r:   r%   r%   r&   add_decorated_limit0   s
   z LimitManager.add_decorated_limit	blueprintc                 C  s"   |r| j |t | d S d S r*   )r   r<   r   r=   )r$   r?   r8   r%   r%   r&   add_blueprint_limit9   s   z LimitManager.add_blueprint_limitscoper
   c                 C     || j |< d S r*   )r   )r$   r6   rA   r%   r%   r&   add_route_exemption=      z LimitManager.add_route_exemptionc                 C  rB   r*   r   )r$   r?   rA   r%   r%   r&   add_blueprint_exemption@   rD   z$LimitManager.add_blueprint_exemptionendpointcallablec                 C  s   | j |t | d S r*   )r    r<   r   r=   )r$   rG   rH   r%   r%   r&   add_endpoint_hintC   s   zLimitManager.add_endpoint_hintc                 C  s   t | j|S r*   )r;   r    get)r$   rG   r%   r%   r&   	has_hintsF   s   zLimitManager.has_hintsNappflask.FlaskOptional[str]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.0r8   r%   r%   r&   	<genexpr>d       z.LimitManager.resolve_limits.<locals>.<genexpr>c                 s      | ]}|j V  qd S r*   )method_exemptrW   r%   r%   r&   rY   o   s    c                 s  rT   r*   rU   rW   r%   r%   r&   rY   t   s    
c                 s  rT   r*   rU   rW   r%   r%   r&   rY   |   rZ   F)view_functionsrJ   r   extendr   r    r   allr   exemption_scoper
   APPLICATIONr   DEFAULTr   )r$   rL   rG   r?   rO   rP   rQ   before_request_contextr   hinted_limits	view_funcnamehintr`   
all_limitsexplicit_limits_exemptcombined_defaultshinted_limits_request_defaultsr%   r%   r&   resolve_limitsI   s\   		
zLimitManager.resolve_limitsc                 C  s   |j |pdd }|rt|nd}| j| }|r|j|nd }|s$|S |s(J | ||\}}	|tjtjB  @ s;|	rF|		 D ]}
||
O }q?||B S )NrS   )
r]   rJ   r   r   
blueprints_blueprint_exemption_scoper
   rb   ra   values)r$   rL   rG   r?   re   rf   route_exemption_scopeblueprint_instanceblueprint_exemption_scopeancestor_exemption_scopes	exemptionr%   r%   r&   r`      s(   


zLimitManager.exemption_scopec                 C  s   g }| j | s>|| jv r>| 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   r   append
ValueErrorr#   error)r$   rO   r0   groupr8   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@ s[tj fdd| j	|D  n|	}
|
r|
D ],}z|dd |D  W qa ty } z jd| d|  W Y d }~qad }~ww |S )	N.c                 s  r[   r*   rU   rW   r%   r%   r&   rY      s    
z0LimitManager.blueprint_limits.<locals>.<genexpr>c                 3  s    | ]
} j |g V  qd S r*   )r   rJ   )rX   memberr.   r%   r&   rY      s
    
c                 S  sB   g | ]}t |j|j|j|j|j|j|j|j|j	|j
|j|jqS r%   )r   r8   key_funcrA   
per_methodmethodserror_messageexempt_whenrV   deduct_when	on_breachcostsharedrW   r%   r%   r&   
<listcomp>   s"    z1LimitManager.blueprint_limits.<locals>.<listcomp>z'failed to load ratelimit for blueprint ru   )rm   rJ   rf   setsplitrn   r
   rb   ra   r   r   r_   r   	ANCESTORSr,   r-   intersection
differencer^   rw   r#   rx   )r$   rL   r?   r0   rq   blueprint_nameblueprint_ancestoryself_exemptionancestor_exemptionsblueprint_self_limitsr   limit_grouprz   r%   r.   r&   r      s^   

zLimitManager.blueprint_limitsr   0Tuple[ExemptionScope, Dict[str, ExemptionScope]]c                   s\   |j | j} j| tj @ }t|d}dd  j D |}| fdd|D fS )Nr{   c                 S  s   h | ]\}}|t j@ r|qS r%   )r
   DESCENDENTS)rX   kfr%   r%   r&   	<setcomp>   s    z:LimitManager._blueprint_exemption_scope.<locals>.<setcomp>c                   s   i | ]}| j | qS r%   rE   )rX   r   r.   r%   r&   
<dictcomp>   s    z;LimitManager._blueprint_exemption_scope.<locals>.<dictcomp>)	rm   rf   r   r
   r   r   r   itemsr   )r$   rL   r   rf   rt   	ancestoryancestor_exemptionr%   r.   r&   rn      s   z'LimitManager._blueprint_exemption_scope)r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r(   )r0   r   r   r   )F)r6   r7   r8   r9   r:   r;   r   r   )r?   r7   r8   r9   r   r   )r6   r7   rA   r
   r   r   )r?   r7   rA   r
   r   r   )rG   r7   rH   r7   r   r   )rG   r7   r   r;   )NNNFF)rL   rM   rG   rN   r?   rN   rO   rN   rP   r;   rQ   r;   r   rR   )rL   rM   rG   rN   r?   rN   r   r
   )rO   r7   r   r(   )rL   rM   r?   r7   r   r(   )rL   rM   r   r7   r   r   )__name__
__module____qualname__r'   propertyr   r   r3   r5   r>   r@   rC   rF   rI   rK   rl   r`   r   r   rn   r%   r%   r%   r&   r      s2    



	




@

Ar   )
__future__r   r,   r!   typingr   r   r   r   r   flaskordered_setr   	constantsr
   utilr   wrappersr   r   r   r%   r%   r%   r&   <module>   s    