
    9i:                        d dl mZ d dlZd dlZd dlZd dlZd dlmZ d dlm	Z	 d dl
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mZ d	d
lmZmZmZmZmZmZmZ d	dlmZ erd dlm Z m!Z!  ejD                  dd       G d d             Z# ejD                  dd       G d d             Z$ ejD                  dd       G d de$             Z% ejD                  dd       G d de$             Z& ejD                  dd       G d de$             Z'y)    )annotationsN)wraps)TracebackType)TYPE_CHECKINGcastoverload)request)Response)RateLimitItem
parse_many   )CallableIterableIteratorPRSelfSequence)get_qualified_name)LimiterRequestLimitT)equnsafe_hashc                     e Zd ZU dZded<   ded<   ded<   dZd	ed
<   dZded<   dZded<   dZded<   dZ	ded<   dZ
ded<   dZded<   dZded<   dZd	ed<   dZded<   d#dZed$d       Zed%d        Zed$d!       Zd&d"Zy)'RuntimeLimitzV
    Final representation of a rate limit before it is triggered during a request
    r   limitCallable[[], str]key_func!str | Callable[[str], str] | NonescopeFbool
per_methodNSequence[str] | Nonemethodsstr | Callable[[], str] | Noneerror_messageCallable[[], bool] | Noneexempt_whenbool | Noneoverride_defaults!Callable[[Response], bool] | Nonededuct_when0Callable[[RequestLimit], Response | None] | None	on_breachr   zCallable[[], int] | intcostsharedztuple[RuntimeLimit, ...] | Nonemeta_limitsc                    | j                   r7t        | j                   D cg c]  }|j                          c}      | _         y y c c}w N)r$   tuplelowerselfks     H/var/www/html/venv/lib/python3.12/site-packages/flask_limiter/_limits.py__post_init__zRuntimeLimit.__post_init__+   s2    << T\\!B!'')!BCDL !Bs   Ac                <    | j                   r| j                         S y)zCheck if the limit is exempt.F)r(   r7   s    r9   	is_exemptzRuntimeLimit.is_exempt/   s     ##%%    c                d    t        | j                        r| j                         S | j                  S )z!How much to deduct from the limit)callabler/   r<   s    r9   deduction_amountzRuntimeLimit.deduction_amount8   s$     'tyy1tyy{@tyy@r>   c                v    | j                   duxr* t        j                  j                         | j                   vS )z4Check if the limit is not applicable for this methodN)r$   r	   methodr5   r<   s    r9   method_exemptzRuntimeLimit.method_exempt>   s.     ||4'VGNN,@,@,B$,,,VVr>   c                   t        | j                        r#| j                  t        j                  xs d      n| j                  }|r| j                  r|}n
| d| }n|}| j
                  r|sJ |d|j                          z  }|S )z
        Derive final bucket (scope) for this limit given the endpoint and request method.
        If the limit is shared between multiple routes, the scope does not include the endpoint.
         :)r@   r    r	   endpointr0   r"   upper)r7   rH   rC   limit_scoper    s        r9   	scope_forzRuntimeLimit.scope_forD   s    
 =ETZZ<Pdjj!1!1!7R8VZV`V`{{##*Ak]3E??M6q())Er>   returnNone)rM   r!   )rM   int)rH   strrC   z
str | NonerM   rP   )__name__
__module____qualname____doc____annotations__r"   r$   r&   r(   r*   r,   r.   r/   r0   r1   r:   propertyr=   rA   rD   rK    r>   r9   r   r      s     ,,J$(G!(48M18-1K*1%*{*59K29BFI?F$%D
!%FD37K07D   A A
 W W
r>   r   c                  x   e Zd ZU dZded<   dZded<   dZded<   dZd	ed
<   dZded<   dZ	ded<   dZ
ded<   dZded<   dZded<   dZded<   dZded<   dZded<    ej"                  dd      Zded<    ej"                  dddd      Zd ed!<    ej"                  d"      Zded#<   d(d$Zd)d%Zed*d&       Zd+d'Zy),Limita  
    The definition of a rate limit to be used by the extension as a default limit::


        def default_key_function():
            return request.remote_addr

        def username_key_function():
            return request.headers.get("username", "guest")

        limiter = flask_limiter.Limiter(
            default_key_function,
            default_limits = [
                # 10/second by username
                flask_limiter.Limit("10/second", key_function=username_key_function),
                # 100/second by ip (i.e. default_key_function)
                flask_limiter.Limit("100/second),

            ]
        )
        limit.init_app(app)

    - For application wide limits see :class:`ApplicationLimit`
    - For meta limits see :class:`MetaLimit`
    zCallable[[], str] | strlimit_providerNCallable[[], str] | Nonekey_functionr   r    zCallable[[], int] | int | Noner/   Fr!   r0   r#   r$   r"   r%   r&   z4Iterable[Callable[[], str] | str | MetaLimit] | Noner1   r'   r(   r+   r,   r-   r.   )defaultinitr)   r*   T)r^   hashkw_onlyreprzweakref.ProxyType[Limiter]limiterr]   	finalizedc                    | j                   r6t        | j                   D cg c]  }|j                          c}      | _         | j                  rt        | j                        | _        y y c c}w r3   )r$   r4   r5   r1   r6   s     r9   r:   zLimit.__post_init__   sQ    << T\\!B!'')!BCDL$T%5%56D  "Cs   A+c              #    K   t        | j                        r| j                         n| j                  }|rt        |      ng }d}| j                  rft	        t        j                  | j                  D cg c]7  }t        t        |t              st        |      j                  |       n|      9 c}       }|D ]  }t        || j                  | j                  | j                  | j                  | j                   | j"                  | j$                  | j&                  | j(                  | j*                  xs d| j,                  |        y c c}w w)NrW   r   )r    r"   r$   r&   r(   r,   r*   r.   r/   r0   r1   )r@   rZ   r   r1   r4   	itertoolschainlist
isinstance	MetaLimitbind_parentr   limit_byr    r"   r$   r&   r(   r,   r*   r.   r/   r0   )r7   	limit_strlimit_itemsr1   
meta_limitr   s         r9   __iter__zLimit.__iter__   s%    -5d6I6I-JD'')PTPcPc	/8j+b02 +/*:*: ' #-j)#D &j1==dC!+	K ! 	Ejj??"00 ,, ,,"&"8"8..YY^!{{' 	s   A1E3<E/B!Ec                J    | j                   xs | j                  j                  S r3   )r\   rb   	_key_funcr<   s    r9   rm   zLimit.limit_by   s      :DLL$:$::r>   c                    t        j                  |      | _        | j                  xs dD cg c]#  }t	        |t
              r|j                  |      % c} | S c c}w )z
        Returns an instance of the limit definition that binds to a weak reference of an instance
        of :class:`Limiter`.

        :meta private:
        rW   )weakrefproxyrb   r1   rj   rk   bind)r7   rb   rp   s      r9   rw   z
Limit.bind   sV     }}W- #..4"	
*i0 OOG$	
 	
s   (ArL   )rM   zIterator[RuntimeLimit]rM   r   )r7   r   rb   r   rM   r   )rQ   rR   rS   rT   rU   r\   r    r/   r0   r$   r"   r&   r1   r(   r,   r.   dataclassesfieldr*   rb   rd   r:   rq   rV   rm   rw   rW   r>   r9   rY   rY   Z   s   8 ,+ .2L*1 04E,3 ,0D
(/FD %)G!( J 59M18HLKEL .2K*1 6:K29
 CGI?F &7[%6%6u5%Q{Q +<+*;*;5+G'  (k''5It57"H ; ;r>   rY   )r   r`   c                       e Zd ZU dZdZded<    ej                  d      Zded<   d fdZ	dd	Z
	 	 	 	 	 	 	 	 dd
Zedd       Zedd       ZddZ xZS )
RouteLimita  
    A variant of :class:`Limit` that can be used to to decorate a flask route or blueprint directly
    instead of by using :meth:`Limiter.limit` or :meth:`Limiter.shared_limit`.

    Decorating individual routes::

        limiter = flask_limiter.Limiter(.....)
        limiter.init_app(app)

        @app.route("/")
        @flask_limiter.RouteLimit("2/second", limiter=limiter)
        def view_function():
            ...

    Fr)   r*   )r_   zdataclasses.InitVar[Limiter]rb   c                D    | j                  |       t        | 	          y r3   )rw   superr:   )r7   rb   	__class__s     r9   r:   zRouteLimit.__post_init__  s    		'r>   c                   t        j                  d      }|d   j                   d|d   j                   d|d   j                   }| j
                  j                  j                  || d       | j
                  j                  j                  | j
                  j                         |       | j
                  j                  d|       y )	N   )r   r   rG   T)overrideFin_middlewarecallable_name)	tracebackextract_stackfilenamenamelinenorb   limit_manageradd_decorated_limitadd_endpoint_hintidentify_request_check_request_limit)r7   tbqualified_locations      r9   	__enter__zRouteLimit.__enter__  s    $$1- "1/qAAbell^L
 	""667I4Z^6_""44LL))+-?	
 	))M_)`r>   c                     y r3   rW   )r7   exc_type	exc_valuer   s       r9   __exit__zRouteLimit.__exit__  s    
 r>   c                     y r3   rW   r7   objs     r9   __call__zRouteLimit.__call__%  s    ?Br>   c                     y r3   rW   r   s     r9   r   zRouteLimit.__call__(  s    69r>   c                    t        t        j                        rj                  nt	              t        t        j                        r' j
                  j                  j                          y  j
                  j                  j                          j
                  j                  j                          t              d fd       }t        |d j
                         |S )Nc                    t        dd       j                  k(  sj                  j                         }|r`t        j                  j
                  j                  |d       }|r4t        |      k(  s&j                  j                  j                  |       j                  j                  d       t        t        t        j                  j                        | i |      S )N__wrapper-limiter-instanceFr   )getattrrb   r   flaskcurrent_appview_functionsgetr   r   r   r   r   r   ensure_sync)ar8   identity	view_funcr   r   r7   s       r9   __innerz$RouteLimit.__call__.<locals>.__inner9  s    s$@$G4<<W#||<<>H$)$5$5$D$D$H$HSW$X	$-?	-Jd-R LL66HHSWXLL55EY]5^Au00<<SA1JJKKr>   r   )r   zP.argsr8   zP.kwargsrM   r   )rj   r   	Blueprintr   r   rb   r   add_blueprint_limit_marked_for_limitingaddr   r   setattr)r7   r   _RouteLimit__innerr   s   `` @r9   r   zRouteLimit.__call__+  s    c5??+88D%c*Dc5??+LL&&::4FLL--11$7LL&&::4F3ZL L( G94<<HNr>   )rb   r   rM   rN   rL   )r   ztype[BaseException] | Noner   zBaseException | Noner   zTracebackType | NonerM   rN   )r   Callable[P, R]rM   r   )r   zflask.BlueprintrM   rN   )r   z Callable[P, R] | flask.BlueprintrM   zCallable[P, R] | None)rQ   rR   rS   rT   r*   rU   ry   rz   rb   r:   r   r   r   r   __classcell__)r   s   @r9   r|   r|      s    , &+{*,=K,=,=5,IG)I a, ( (	
 
 B B9 9%r>   r|   )r`   r   c                  t    e Zd ZU dZ ej
                  d      Zded<    ej
                  dd      Zd	ed
<   y)ApplicationLimitz
    Variant of :class:`Limit` to be used for declaring an application wide limit that can be passed
    to :class:`Limiter` as one of the members of :paramref:`Limiter.application_limits`
    globalrc   r   r    FTr^   r]   r!   r0   N)	rQ   rR   rS   rT   ry   rz   r    rU   r0   rW   r>   r9   r   r   S  s=     0A{/@/@/RE,R %;$$%>FD>r>   r   c                  2   e Zd ZU dZ ej
                  d      Zded<    ej
                  dd      Zd	ed
<    ej
                  dd      Z	ded<    ej
                  dd      Z
ded<    ej
                  dd      Zded<   dZded<   edd       ZddZy)rk   a  
    Variant of :class:`Limit` to be used for declaring a meta limit that can be passed to
    either :class:`Limiter` as one of the  members of :paramref:`Limiter.meta_limits` or to another
    instance of :class:`Limit` as a member of :paramref:`Limit.meta_limits`
    metarc   r   r    FNr   z4Sequence[Callable[[], str] | str | MetaLimit] | Noner1   zLimit | Noneparent_limitTr!   r0   r+   r,   r[   r\   c                    | j                   xs< | j                  xr | j                  j                   xs | j                  j                  S r3   )r\   r   rb   rs   r<   s    r9   rm   zMetaLimit.limit_by  sH      &   /!!..& ||%%		
r>   c                    || _         | S )zt
        Binds this meta limit to be associated as a child of the ``parent`` limit.

        :meta private:
        )r   )r7   parents     r9   rl   zMetaLimit.bind_parent  s     #r>   rx   )r7   r   r   rY   rM   r   )rQ   rR   rS   rT   ry   rz   r    rU   r1   r   r0   r,   r\   rV   rm   rl   rW   r>   r9   rk   rk   b  s     0A{/@/@/PE,P IZHYHYDIKE  "3!2!2t!LL,L %;$$%>FD> 6G[5F5FE[_5`K2` .2L*1
 
r>   rk   )(
__future__r   ry   rg   r   ru   	functoolsr   typesr   typingr   r   r   r   r	   flask.wrappersr
   limitsr   r   _typingr   r   r   r   r   r   r   utilr   flask_limiterr   r   	dataclassr   rY   r|   r   rk   rW   r>   r9   <module>r      s   "       0 0   # , G G G $3 $D1? ? 2?D $D1Q Q 2Qh 46` ` 7`F t6?u ? 7? t63 3 73r>   