o
    j(                     @  s   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlZd dl	m
Z
 ddlmZmZ ddlmZ dd	lmZ er@dd
lmZ G dd dZdS )    )annotationsN)Iterable)TYPE_CHECKING)
OrderedSet   )ApplicationLimitRuntimeLimit)ExemptionScope)get_qualified_name)Limitc                   @  s   e Zd ZdEddZedFddZedFddZdGddZdHddZdIdJd d!Z	dKd#d$Z
dLd'd(ZdMd)d*ZdNd-d.ZdOd/d0Z	1	1	1		dPdQd9d:ZdRd;d<ZdSd=d>ZdTd?d@ZdUdCdDZd1S )VLimitManagerapplication_limitslist[ApplicationLimit]default_limitslist[Limit]decorated_limitsdict[str, OrderedSet[Limit]]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$   K/var/www/ai-app/venv/lib/python3.10/site-packages/flask_limiter/_manager.py__init__   s   	zLimitManager.__init__list[RuntimeLimit]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  r0   r)   )r   r1   r$   r$   r%   set_default_limits1   r3   zLimitManager.set_default_limitsFroutestrlimitLimit | Noneoverrideboolc                 C  s:   |r|s| j |t | d S t|g| j |< d S d S r)   )r   
setdefaultr   add)r#   r5   r7   r9   r$   r$   r%   add_decorated_limit4   s
   z LimitManager.add_decorated_limit	blueprintc                 C  s"   |r| j |t | d S d S r)   )r   r;   r   r<   )r#   r>   r7   r$   r$   r%   add_blueprint_limit;   s   z LimitManager.add_blueprint_limitscoper	   c                 C     || j |< d S r)   )r   )r#   r5   r@   r$   r$   r%   add_route_exemption?      z LimitManager.add_route_exemptionc                 C  rA   r)   )r   )r#   r>   r@   r$   r$   r%   add_blueprint_exemptionB   rC   z$LimitManager.add_blueprint_exemptionendpointcallablec                 C  s   | j |t | d S r)   )r   r;   r   r<   )r#   rE   rF   r$   r$   r%   add_endpoint_hintE   s   zLimitManager.add_endpoint_hintc                 C  s   t | j|S r)   )r:   r   get)r#   rE   r$   r$   r%   	has_hintsH   s   zLimitManager.has_hintsNappflask.Flask
str | Nonecallable_namein_middlewaremarked_for_limitingtuple[list[RuntimeLimit], ...]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.0r7   r$   r$   r%   	<genexpr>f       z.LimitManager.resolve_limits.<locals>.<genexpr>c                 s      | ]}|j V  qd S r)   )method_exemptrU   r$   r$   r%   rW   q       c                 s  rR   r)   rS   rU   r$   r$   r%   rW   v   rX   c                 s  rR   r)   rS   rU   r$   r$   r%   rW   |   rX   F)view_functionsrH   r
   extendr   r   r   allr   exemption_scoper	   APPLICATIONr   DEFAULTr   )r#   rJ   rE   r>   rM   rN   rO   before_request_contextr   hinted_limits	view_funcnamehintr_   
all_limitsexplicit_limits_exemptcombined_defaultshinted_limits_request_defaultsr$   r$   r%   resolve_limitsK   sT   	
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 )NrQ   )r\   rH   r
   r   r	   NONE
blueprints_blueprint_exemption_scopera   r`   values)r#   rJ   rE   r>   rd   re   route_exemption_scopeblueprint_instanceblueprint_exemption_scopeancestor_exemption_scopes	exemptionr$   r$   r%   r_      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   rH   r	   rl   r   append
ValueErrorr"   error)r#   rM   r/   groupr7   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  rY   r)   rS   rU   r$   r$   r%   rW      r[   z0LimitManager.blueprint_limits.<locals>.<genexpr>c                 3  s    | ]
} j |g V  qd S r)   )r   rH   )rV   memberr-   r$   r%   rW      s
    
c                 S  sB   g | ]}t |j|j|j|j|j|j|j|j|j	|j
|j|jqS r$   )r   r7   key_funcr@   
per_methodmethodserror_messageexempt_whenrT   deduct_when	on_breachcostsharedrU   r$   r$   r%   
<listcomp>   s"    z1LimitManager.blueprint_limits.<locals>.<listcomp>z'failed to load ratelimit for blueprint ru   )rm   rH   re   setsplitrn   r	   ra   r`   r   r   r^   r   rl   	ANCESTORSr+   r,   intersection
differencer]   rw   r"   rx   )r#   rJ   r>   r/   rq   blueprint_nameblueprint_ancestoryself_exemptionancestor_exemptionsblueprint_self_limitsr   limit_grouprz   r$   r-   r%   r      sP   

zLimitManager.blueprint_limitsr   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 )Nr{   c                 S  s   h | ]\}}|t j@ r|qS r$   )r	   DESCENDENTS)rV   kfr$   r$   r%   	<setcomp>   s
    z:LimitManager._blueprint_exemption_scope.<locals>.<setcomp>c                   s   i | ]}| j |tjqS r$   )r   rH   r	   rl   )rV   r   r-   r$   r%   
<dictcomp>   s    z;LimitManager._blueprint_exemption_scope.<locals>.<dictcomp>)rm   re   r   rH   r	   rl   r   r   r   itemsr   )r#   rJ   r   re   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'   )r/   r   r   r   )r/   r   r   r   )F)r5   r6   r7   r8   r9   r:   r   r   )r>   r6   r7   r8   r   r   )r5   r6   r@   r	   r   r   )r>   r6   r@   r	   r   r   )rE   r6   rF   r6   r   r   )rE   r6   r   r:   )NNNFF)rJ   rK   rE   rL   r>   rL   rM   rL   rN   r:   rO   r:   r   rP   )rJ   rK   rE   rL   r>   rL   r   r	   )rM   r6   r   r'   )rJ   rK   r>   r6   r   r'   )rJ   rK   r   r6   r   r   )__name__
__module____qualname__r&   propertyr   r   r2   r4   r=   r?   rB   rD   rG   rI   rk   r_   r   r   rn   r$   r$   r$   r%   r      s0    








:

9r   )
__future__r   r+   r    collections.abcr   typingr   flaskordered_setr   _limitsr   r   	constantsr	   utilr
   rQ   r   r   r$   r$   r$   r%   <module>   s    