Ë
    ±ŒjT(  ã                  óÂ   — d dl mZ d dlZd dl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 d dlmZ d dlmZmZmZ d d	lmZmZmZmZmZmZ d d
lmZ  G d„ deee«      Zy)é    )ÚannotationsN)ÚIterable)ÚceilÚfloor)Ú
ModuleType)Úparse_storage_uri)ÚConfigurationError)ÚSlidingWindowCounterSupportÚStorageÚTimestampedSlidingWindow)ÚAnyÚCallableÚMemcachedClientPÚPÚRÚcast)Úget_dependencyc                  óJ  ‡ — e Zd ZdZdgZ	 dgZ	 d	 	 	 	 	 	 	 dˆ fd„Ze	 	 dd„«       Z	 	 	 	 	 	 	 	 dd„Z		 	 	 	 	 	 	 	 dd„Z
edd„«       Zdd	„Zdd
„Zdd„Z	 	 d	 	 	 	 	 	 	 	 	 dd„Zd d„Zd!d„Zd"d„Zd#d„Z	 d$	 	 	 	 	 	 	 	 	 d%d„Z	 	 	 	 	 	 d&d„Zd'd„Z	 	 	 	 	 	 	 	 	 	 d(d„Zˆ xZS ))ÚMemcachedStoragez[
    Rate limit storage with memcached as backend.

    Depends on :pypi:`pymemcache`.
    Ú	memcachedÚ
pymemcachec                óâ  •— t        |«      }|  |j                  r|j                  g| _        n|j                  | _        | j                  d   j
                  | _        t        |j                  dd«      «      | _	        t        |j                  dd«      «      | _
        t        t        t        t        t        t        t         f      t        t           z  gt"        f   |j                  d| j$                  «      «      | _        || _        t+        | j                  «      st-        d| j                  › �«      ‚t/        j0                  «       | _        d| j2                  _        t6        ‰| �q  ||¬	«       y)
an  
        :param uri: memcached location of the form
         ``memcached://host:port,host:port``,
         ``memcached:///var/tmp/path/to/sock``
        :param wrap_exceptions: Whether to wrap storage exceptions in
         :exc:`limits.errors.StorageError` before raising it.
        :param options: all remaining keyword arguments are passed
         directly to the constructor of :class:`pymemcache.client.base.PooledClient`
         or :class:`pymemcache.client.hash.HashClient` (if there are more than
         one hosts specified)
        :raise ConfigurationError: when :pypi:`pymemcache` is not available
        r   Úlibraryzpymemcache.clientÚcluster_libraryzpymemcache.client.hashÚclient_getterz5memcached prerequisite not available. please install N)Úwrap_exceptions)r   ÚpathÚhostsÚ	locationsÚdependenciesÚmoduleÚ
dependencyÚstrÚpopr   r   r   r   r   ÚlistÚtupleÚintr   Ú
get_clientr   Úoptionsr   r	   Ú	threadingÚlocalÚlocal_storageÚstorageÚsuperÚ__init__)ÚselfÚurir   r)   Ústorage_uri_optionsÚ	__class__s        €úb/var/www/html/Fitness-lenito-AI-main/venv/lib/python3.12/site-packages/limits/storage/memcached.pyr/   zMemcachedStorage.__init__'   s1  ø€ ô$ 0°Ó4ÐÙØ×#Ò#Ø-×2Ñ2Ð3ˆD�Jà,×6Ñ6ˆDŒJà×+Ñ+¨LÑ9×@Ñ@ˆŒÜ˜7Ÿ;™; yÐ2EÓFÓGˆŒÜ"Ø�K‰KÐ)Ð+CÓDó 
ˆÔô "Ü”j¤$¤u¬S´#¨X¡Ñ"7¼$¼s¹)Ñ"CÐDÔFVÐVÑWØ�K‰K˜¨¯©Ó9ó
ˆÔð ˆŒä˜dŸl™lÔ+Ü$ØGÈÏÉÀ~ÐVóð ô 'Ÿ_™_Ó.ˆÔØ%)ˆ×ÑÔ"Ü‰Ñ˜¨oÐÕ>ó    c                ó.   — | j                   j                  S ©N)r"   ÚMemcacheError©r0   s    r4   Úbase_exceptionsz MemcachedStorage.base_exceptionsS   s   € ð �‰×,Ñ,Ð,r5   c                óŒ   — t        t        t        |«      dkD  r |j                  |fi |¤Ž«      S  |j                  |i |¤Ž«      S )zˆ
        returns a memcached client.

        :param module: the memcached module
        :param hosts: list of memcached hosts
        é   )r   r   ÚlenÚ
HashClientÚPooledClient)r0   r!   r   Úkwargss       r4   r(   zMemcachedStorage.get_clientY   sY   € ô Üô �u“: ’>ð "�×!Ñ! %Ñ2¨6Ñ2ó
ð 	
ð
 )�V×(Ñ(¨%Ð:°6Ñ:ó
ð 	
r5   c                óš   — d|v r@t        j                  |«      }d|j                  v s|j                  s|j	                  d«        ||i |¤ŽS )NÚnoreply)ÚinspectÚgetfullargspecÚargsÚvarkwr$   )r0   ÚfuncrE   r@   Úargspecs        r4   Úcall_memcached_funcz$MemcachedStorage.call_memcached_funcl   sJ   € ð ˜ÑÜ×,Ñ,¨TÓ2ˆGà §¡Ñ-°·²Ø—
‘
˜9Ô%á�TÐ$˜VÑ$Ð$r5   c                ó¼  — t        | j                  d«      r| j                  j                  s�t        t	        | j
                  «      dkD  r| j                  n| j                  «      d   }|st        d| j                  › �«      ‚ | j                  || j
                  fi | j                  ¤Ž| j                  _        t        t        | j                  j                  «      S )zQ
        lazily creates a memcached client instance using a thread local
        r-   r<   r   zUnable to import )Úhasattrr,   r-   r   r=   r   r   r   r	   r   r)   r   r   )r0   r"   s     r4   r-   zMemcachedStorage.storagew   s½   € ô ˜×*Ñ*¨IÔ6¸4×;MÑ;M×;UÒ;UÜ'Ü(+¨D¯J©J«¸!Ò(;�×$Ò$ÀÇÁóàñˆJñ Ü(Ð+<¸T×=QÑ=QÐ<RÐ)SÓTÐTØ);¨×);Ñ);Ø˜DŸJ™Jñ*Ø*.¯,©,ñ*ˆD×ÑÔ&ô Ô$ d×&8Ñ&8×&@Ñ&@ÓAÐAr5   c                óL   — t        | j                  j                  |d«      «      S )zB
        :param key: the key to get the counter value for
        Ú0)r'   r-   Úget©r0   Úkeys     r4   rN   zMemcachedStorage.getŠ   s    € ô �4—<‘<×#Ñ# C¨Ó-Ó.Ð.r5   c                ó8   — | j                   j                  |«      S )z‡
        Return multiple counters at once

        :param keys: the keys to get the counter values for

        :meta private:
        )r-   Úget_many)r0   Úkeyss     r4   rR   zMemcachedStorage.get_many�   s   € ð �|‰|×$Ñ$ TÓ*Ð*r5   c                ó:   — | j                   j                  |«       y)z>
        :param key: the key to clear rate limits for
        N)r-   ÚdeleterO   s     r4   ÚclearzMemcachedStorage.clearš   s   € ð 	�‰×Ñ˜CÕ r5   c                óº  — | j                  | j                  j                  ||d¬«      x}	 �|S | j                  | j                  j                  ||t	        |«      d¬«      s | j                  j                  ||«      xs |S |rW| j                  | j                  j
                  | j                  |«      |t        j                  «       z   t	        |«      d¬«       |S )a¸  
        increments the counter for a given rate limit key

        :param key: the key to increment
        :param expiry: amount in seconds for the key to expire in
         window every hit.
        :param amount: the number to increment by
        :param set_expiration_key: set the expiration key with the expiration time if needed. If set to False, the key will still expire, but memcached cannot provide the expiration time.
        F©rB   )ÚexpirerB   )rI   r-   ÚincrÚaddr   ÚsetÚ_expiration_keyÚtime)r0   rP   ÚexpiryÚamountÚset_expiration_keyÚvalues         r4   rZ   zMemcachedStorage.incr    sÜ   € ð" ×-Ñ-Ø—‘×!Ñ! 3¨¸ð .ó ð ˆEð ð	ð
 ˆLà×+Ñ+Ø—‘× Ñ  # v¬t°F«|ÀUð ,ô ð —|‘|×(Ñ(¨¨fÓ5Ò?¸Ð?á%Ø×,Ñ,ØŸ™×(Ñ(Ø×,Ñ,¨SÓ1Ø¤§¡£Ñ,Ü# F›|Ø %ð -ô ð ˆMr5   c                ó”   — t        | j                  j                  | j                  |«      «      xs t	        j                  «       «      S )z;
        :param key: the key to get the expiry for
        )Úfloatr-   rN   r]   r^   rO   s     r4   Ú
get_expiryzMemcachedStorage.get_expiryÇ   s4   € ô
 �T—\‘\×%Ñ% d×&:Ñ&:¸3Ó&?Ó@ÒOÄDÇIÁIÃKÓPÐPr5   c                ó   — |dz   S )zÜ
        Return the expiration key for the given counter key.

        Memcached doesn't natively return the expiration time or TTL for a given key,
        so we implement the expiration time on a separate key.
        z/expires© rO   s     r4   r]   z MemcachedStorage._expiration_keyÎ   s   € ð �ZÑÐr5   c                ó`   — 	 | j                  | j                  j                  d«       y#  Y yxY w)zq
        Check if storage is healthy by calling the ``get`` command
        on the key ``limiter-check``
        zlimiter-checkTF)rI   r-   rN   r9   s    r4   ÚcheckzMemcachedStorage.check×   s.   € ð
	Ø×$Ñ$ T§\¡\×%5Ñ%5°ÔGàøð	Ùús   ‚&) ©-c                ó   — t         ‚r7   )ÚNotImplementedErrorr9   s    r4   ÚresetzMemcachedStorage.resetã   s   € Ü!Ð!r5   c                óÐ  — ||kD  ryt        j                   «       }| j                  |||«      \  }}| j                  ||||¬«      \  }}	}
}||	z  |z  |
z   }t        |«      |z   |kD  ry| j	                  |d|z  |d¬«      }
t        d|	t        j                   «       |z
  z
  «      }||z  |z  |
z   }t        |«      |kD  r*| j                  | j                  j                  ||d¬«       yy)NF)Únowé   )r`   ra   r   TrX   )	r^   Úsliding_window_keysÚ_get_sliding_window_infor   rZ   ÚminrI   r-   Údecr)r0   rP   Úlimitr_   r`   rn   Úprevious_keyÚcurrent_keyÚprevious_countÚprevious_ttlÚcurrent_countÚ_Úweighted_countÚactualised_previous_ttls                 r4   Úacquire_sliding_window_entryz-MemcachedStorage.acquire_sliding_window_entryæ   s   € ð �EŠ>ØÜ�i‰i‹kˆØ$(×$<Ñ$<¸SÀ&È#Ó$NÑ!ˆ�kØ9=×9VÑ9VØ˜+ v°3ð :Wó :
Ñ6ˆ˜ m°Qð (¨,Ñ6¸Ñ?À-ÑOˆÜ�Ó  6Ñ)¨EÒ1Øð
 !ŸI™IØ˜Q ™Z°È5ð &ó ˆMô '*¨!¨\¼T¿Y¹Y»[È3Ñ=NÑ-OÓ&PÐ#àÐ!8Ñ8¸6ÑAÀMÑQð ô �^Ó$ uÒ,ð ×(Ñ(Ø—L‘L×%Ñ%ØØØ ð	 )ô ð Ør5   c                ó~   — t        j                   «       }| j                  |||«      \  }}| j                  ||||«      S r7   )r^   rp   rq   ©r0   rP   r_   rn   ru   rv   s         r4   Úget_sliding_windowz#MemcachedStorage.get_sliding_window  s@   € ô �i‰i‹kˆØ$(×$<Ñ$<¸SÀ&È#Ó$NÑ!ˆ�kØ×,Ñ,¨\¸;ÈÐPSÓTÐTr5   c                óœ   — t        j                   «       }| j                  |||«      \  }}| j                  |«       | j                  |«       y r7   )r^   rp   rV   r   s         r4   Úclear_sliding_windowz%MemcachedStorage.clear_sliding_window  s>   € Ü�i‰i‹kˆØ$(×$<Ñ$<¸SÀ&È#Ó$NÑ!ˆ�kØ�
‰
�<Ô Ø�
‰
�;Õr5   c                ó  — | j                  ||g«      }t        |j                  |d«      «      t        |j                  |d«      «      }}|dk(  rt        d«      }nd||z
  |z  dz  z
  |z  }d||z  dz  z
  |z  |z   }	||||	fS )Nr   r<   )rR   r'   rN   rd   )
r0   ru   rv   r_   rn   Úresultrw   ry   rx   Úcurrent_ttls
             r4   rq   z)MemcachedStorage._get_sliding_window_info  s    € ð —‘ ¨kÐ:Ó;ˆä�—
‘
˜<¨Ó+Ó,Ü�—
‘
˜;¨Ó*Ó+ð &ˆð
 ˜QÒÜ  ›8‰Là 3¨¡<°6Ñ"9¸QÑ!>Ñ?À6ÑIˆLØ˜S 6™\¨QÑ.Ñ/°6Ñ9¸FÑBˆØ˜|¨]¸KÐGÐGr5   )F)r1   r#   r   Úboolr)   z$str | Callable[[], MemcachedClientP]ÚreturnÚNone)r‡   z-type[Exception] | tuple[type[Exception], ...])r!   r   r   zlist[tuple[str, int]]r@   r#   r‡   r   )rG   zCallable[P, R]rE   zP.argsr@   zP.kwargsr‡   r   )r‡   r   )rP   r#   r‡   r'   )rS   zIterable[str]r‡   zdict[str, Any])rP   r#   r‡   rˆ   )r<   T)
rP   r#   r_   rd   r`   r'   ra   r†   r‡   r'   )rP   r#   r‡   rd   )rP   r#   r‡   r#   )r‡   r†   )r‡   z
int | None)r<   )
rP   r#   rt   r'   r_   r'   r`   r'   r‡   r†   )rP   r#   r_   r'   r‡   útuple[int, float, int, float])rP   r#   r_   r'   r‡   rˆ   )
ru   r#   rv   r#   r_   r'   rn   rd   r‡   r‰   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚSTORAGE_SCHEMEÚDEPENDENCIESr/   Úpropertyr:   r(   rI   r-   rN   rR   rV   rZ   re   r]   ri   rl   r}   r€   r‚   rq   Ú__classcell__)r3   s   @r4   r   r      s¾  ø„ ñð "�]€NØ*Ø �>€Lð
 !&ð*?àð*?ð ð*?ð 8ð	*?ð
 
õ*?ðX ð-à	6ò-ó ð-ð

Ø ð
Ø)>ð
ØJMð
à	ó
ð&	%Ø"ð	%Ø+1ð	%Ø=Eð	%à	
ó	%ð òBó ðBó$/ó+ó!ð Ø#'ð%àð%ð ð%ð ð	%ð
 !ð%ð 
ó%óNQó ó
ó"ð ð'àð'ð ð'ð ð	'ð
 ð'ð 
ó'ðRUØðUØ #ðUà	&óUó ðHØðHØ.1ðHØ;>ðHØEJðHà	&÷Hr5   r   )Ú
__future__r   rC   r*   r^   Úcollections.abcr   Úmathr   r   Útypesr   Úlimits._storage_schemer   Úlimits.errorsr	   Úlimits.storage.baser
   r   r   Úlimits.typingr   r   r   r   r   r   Úlimits.utilr   r   rg   r5   r4   Ú<module>r›      sO   ðÝ "ã Û Û Ý $ß Ý å 4Ý ,÷ñ ÷
÷ õ 'ôNH�wÐ ;Ð=Uõ NHr5   