a
    ”˜ëjÝ*  ã                   @  sp  U d dl mZ dZd dlZd dlZd dlmZ d dlmZm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZmZmZ d d	lmZ d
dlmZ d
dlmZm Z  ej!dkrÎd dlm"Z" nd dl#m"Z" edƒZ$edƒZ%e"dƒZ&edƒZ'de(d< G dd„ dƒZ)e)ƒ Z*de(d< G dd„ deƒZ+G dd„ deƒZ,G dd„ dee$ ƒZ-eG dd„ dee&e$f ƒƒZ.G dd „ d ee$ ƒZ/ed!d"d#œd$d%„ƒZ0ed&d'd#œd(d%„ƒZ0d)d*d#œd+d%„Z0ed,d,d,d-œd.d/d/d0d1œd2d3„ƒZ1ed!d"d#œd4d3„ƒZ1ed&d'd#œd5d3„ƒZ1dId6d7d7d-œd8d.d/d/d9d:œd;d3„Z1edd<d=dd>œd?d@„ƒZ2edAdBddCœdDd@„ƒZ2e*fdEdFdGdd>œdHd@„Z2dS )Jé    )Úannotations)ÚAsyncCacheInfoÚAsyncCacheParametersÚAsyncLRUCacheWrapperÚcacheÚ	lru_cacheÚreduceN)ÚOrderedDict)ÚAsyncIterableÚ	AwaitableÚCallableÚ	CoroutineÚHashableÚIterable)Úupdate_wrapper)Úiscoroutinefunction)ÚAnyÚGenericÚ
NamedTupleÚ	TypedDictÚTypeVarÚcastÚfinalÚoverload)ÚWeakKeyDictionaryé   )ÚLock)ÚRunVarÚ
checkpoint)é   é   )Ú	ParamSpecÚTÚSÚPÚlru_cache_itemsz…RunVar[WeakKeyDictionary[AsyncLRUCacheWrapper[Any, Any], OrderedDict[Hashable, tuple[_InitialMissingType, Lock] | tuple[Any, None]]]]c                   @  s   e Zd ZdS )Ú_InitialMissingTypeN)Ú__name__Ú
__module__Ú__qualname__© r*   r*   úN/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/anyio/functools.pyr&   8   s   r&   Úinitial_missingc                   @  s.   e Zd ZU ded< ded< ded< ded< dS )r   ÚintÚhitsÚmissesú
int | NoneÚmaxsizeÚcurrsizeN©r'   r(   r)   Ú__annotations__r*   r*   r*   r+   r   ?   s   
r   c                   @  s&   e Zd ZU ded< ded< ded< dS )r   r0   r1   ÚboolÚtypedÚalways_checkpointNr3   r*   r*   r*   r+   r   F   s   
r   c                   @  sX   e Zd Zdddœdd„Zddœdd	„Zd
dœdd„Zddœdd„Zddddœdd„ZdS )Ú_LRUMethodWrapperzAsyncLRUCacheWrapper[..., T]Úobject)ÚwrapperÚinstancec                 C  s   || _ || _d S ©N)Ú_LRUMethodWrapper__wrapperÚ_LRUMethodWrapper__instance)Úselfr:   r;   r*   r*   r+   Ú__init__M   s    z_LRUMethodWrapper.__init__r   ©Úreturnc                 C  s
   | j  ¡ S r<   )r=   Ú
cache_info©r?   r*   r*   r+   rC   Q   s    z_LRUMethodWrapper.cache_infor   c                 C  s
   | j  ¡ S r<   )r=   Úcache_parametersrD   r*   r*   r+   rE   T   s    z"_LRUMethodWrapper.cache_parametersÚNonec                 C  s   | j  ¡  d S r<   )r=   Úcache_clearrD   r*   r*   r+   rG   W   s    z_LRUMethodWrapper.cache_clearr   r"   ©ÚargsÚkwargsrB   c                 Ï  s@   | j d u r | j|i |¤ŽI d H S | j| j g|¢R i |¤ŽI d H S r<   )r>   r=   )r?   rI   rJ   r*   r*   r+   Ú__call__Z   s    
z_LRUMethodWrapper.__call__N)r'   r(   r)   r@   rC   rE   rG   rK   r*   r*   r*   r+   r8   L   s
   r8   c                   @  sp   e Zd Zdddddœdd„Zddœd	d
„Zddœdd„Zddœdd„Zddddœdd„Zdddddœdd„ZdS )r   zCallable[P, Awaitable[T]]r0   r5   ©Úfuncr1   r6   r7   c                 C  sJ   || _ d| _d| _|d ur$t|dƒnd | _d| _|| _|| _t| |ƒ d S ©Nr   )	Ú__wrapped__Ú_hitsÚ_missesÚmaxÚ_maxsizeÚ	_currsizeÚ_typedÚ_always_checkpointr   )r?   rM   r1   r6   r7   r*   r*   r+   r@   c   s    zAsyncLRUCacheWrapper.__init__r   rA   c                 C  s   t | j| j| j| jƒS r<   )r   rP   rQ   rS   rT   rD   r*   r*   r+   rC   s   s    zAsyncLRUCacheWrapper.cache_infor   c                 C  s   | j | j| jdœS )N©r1   r6   r7   ©rS   rU   rV   rD   r*   r*   r+   rE   v   s    ýz%AsyncLRUCacheWrapper.cache_parametersrF   c                 C  s0   t  d ¡ }r,| | d ¡ d | _ | _| _d S rN   )r%   ÚgetÚpoprP   rQ   rT   )r?   r   r*   r*   r+   rG   }   s    z AsyncLRUCacheWrapper.cache_clearzP.argszP.kwargsr"   rH   c           	   	   Ï  sV  | j dkr2| j|i |¤ŽI d H }|  jd7  _|S |}|rR|tft| ¡ dƒ 7 }| jr’|tdd„ |D ƒƒ7 }|r’|tftdd„ | ¡ D ƒƒ 7 }zt	 
¡ }W n" tyÀ   tƒ }t	 |¡ Y n0 z||  }W n  tyî   tƒ  }|| < Y n0 z|| \}}W n4 t�y4   tt| j d� }}||f||< Y n0 |d u �rv|  jd7  _| |¡ | j�rltƒ I d H  tt|ƒS |4 I d H š¸ || d  }tu �r|  jd7  _| j d u�rÐ| j| j k�rÐ|jdd	� n|  jd7  _| j|i |¤ŽI d H }|d f||< n"|  jd7  _| |¡ tt|ƒ}W d   ƒI d H  �qR1 I d H �sH0    Y  |S )
Nr   r   r*   c                 s  s   | ]}t |ƒV  qd S r<   ©Útype)Ú.0Úargr*   r*   r+   Ú	<genexpr>�   ó    z0AsyncLRUCacheWrapper.__call__.<locals>.<genexpr>c                 s  s   | ]}t |ƒV  qd S r<   r[   )r]   Úvalr*   r*   r+   r_   ’   r`   )Zfast_acquireF)Úlast)rS   rO   rQ   r,   ÚsumÚitemsrU   ÚtupleÚvaluesr%   rY   ÚLookupErrorr   ÚsetÚKeyErrorr	   r   rV   rP   Úmove_to_endr   r   r"   rT   Úpopitem)	r?   rI   rJ   ÚvalueÚkeyr   Úcache_entryZcached_valueÚlockr*   r*   r+   rK   ‚   sZ    
 þ



8zAsyncLRUCacheWrapper.__call__Nr9   ztype | Nonez_LRUMethodWrapper[T])r;   ÚownerrB   c                 C  s   t | |ƒ}t|| jƒ |S r<   )r8   r   rO   )r?   r;   rp   r:   r*   r*   r+   Ú__get__Æ   s    
zAsyncLRUCacheWrapper.__get__)N)	r'   r(   r)   r@   rC   rE   rG   rK   rq   r*   r*   r*   r+   r   a   s   E ÿr   c                   @  sV   e Zd Zddddœdd„Zedddœd	d
„ƒZedddœdd
„ƒZdddœdd
„ZdS )Ú_LRUCacheWrapperr0   r5   rW   c                 C  s   || _ || _|| _d S r<   rX   )r?   r1   r6   r7   r*   r*   r+   r@   Ï   s    z_LRUCacheWrapper.__init__ú#Callable[P, Coroutine[Any, Any, T]]úAsyncLRUCacheWrapper[P, T]©rM   rB   c                C  s   d S r<   r*   ©r?   rM   r*   r*   r+   rK   Ô   s    z_LRUCacheWrapper.__call__úCallable[..., T]úfunctools._lru_cache_wrapper[T]c                C  s   d S r<   r*   rv   r*   r*   r+   rK   Ù   s    z6Callable[P, Coroutine[Any, Any, T]] | Callable[..., T]ú<AsyncLRUCacheWrapper[P, T] | functools._lru_cache_wrapper[T])ÚfrB   c                C  s2   t |ƒrt|| j| j| jƒS tj| j| jd�|ƒS )N)r1   r6   )r   r   rS   rU   rV   Ú	functoolsr   )r?   rz   r*   r*   r+   rK   Þ   s
    ÿN)r'   r(   r)   r@   r   rK   r*   r*   r*   r+   rr   Î   s   rr   rs   rt   ru   c                C  s   d S r<   r*   ©rM   r*   r*   r+   r   é   s    r   rw   rx   c                C  s   d S r<   r*   r|   r*   r*   r+   r   ï   s    z6Callable[..., T] | Callable[P, Coroutine[Any, Any, T]]ry   c                C  s   t dd�| ƒS )z’
    A convenient shortcut for :func:`lru_cache` with ``maxsize=None``.

    This is the asynchronous equivalent to :func:`functools.cache`.

    N)r1   )r   r|   r*   r*   r+   r   ó   s    	.rW   r0   r5   z_LRUCacheWrapper[Any])r1   r6   r7   rB   c                 C  s   d S r<   r*   rW   r*   r*   r+   r   ÿ   s    r   c                C  s   d S r<   r*   r|   r*   r*   r+   r     s    c                C  s   d S r<   r*   r|   r*   r*   r+   r     s    é€   Fz=Callable[P, Coroutine[Any, Any, T]] | Callable[..., T] | NonezTAsyncLRUCacheWrapper[P, T] | functools._lru_cache_wrapper[T] | _LRUCacheWrapper[Any])rM   r1   r6   r7   rB   c               C  s<   | du rt t |||ƒS t| ƒs(tdƒ‚t t |||ƒ| ƒS )a–  
    An asynchronous version of :func:`functools.lru_cache`.

    If a synchronous function is passed, the standard library
    :func:`functools.lru_cache` is applied instead.

    :param always_checkpoint: if ``True``, every call to the cached function will be
        guaranteed to yield control to the event loop at least once

    .. note:: Caches and locks are managed on a per-event loop basis.

    Nz#the first argument must be callable)rr   r   ÚcallableÚ	TypeErrorr"   rL   r*   r*   r+   r     s
    zCallable[[T, S], Awaitable[T]]zIterable[S] | AsyncIterable[S])ÚinitialÚfunctionÚiterablerB   c                Ã  s   d S r<   r*   )r�   r‚   r€   r*   r*   r+   r   .  s    r   zCallable[[T, T], Awaitable[T]]zIterable[T] | AsyncIterable[T])r�   r‚   rB   c                Ã  s   d S r<   r*   )r�   r‚   r*   r*   r+   r   7  s    zT | _InitialMissingTypez?Callable[[T, T], Awaitable[T]] | Callable[[T, S], Awaitable[T]]z?Iterable[T] | Iterable[S] | AsyncIterable[T] | AsyncIterable[S]c                Ã  s   d}t |tƒrŠ| ¡ }|tu rVztt| ¡ I dH ƒ}W q` tyR   tdƒd‚Y q`0 n
tt|ƒ}|2 z 3 dH W }| ||ƒI dH }d}qd6 n€t |t	ƒ�rt
|ƒ}|tu rØzttt|ƒƒ}W qâ tyÔ   tdƒd‚Y qâ0 n
tt|ƒ}|D ]}| ||ƒI dH }d}qæntdƒ‚|�stƒ I dH  |S )at  
    Asynchronous version of :func:`functools.reduce`.

    :param function: a coroutine function that takes two arguments: the accumulated
        value and the next element from the iterable
    :param iterable: an iterable or async iterable
    :param initial: the initial value (if missing, the first element of the iterable is
        used as the initial value)

    FNz0reduce() of empty sequence with no initial valueTz9reduce() argument 2 must be an iterable or async iterable)Ú
isinstancer
   Ú	__aiter__r,   r   r"   Ú	__anext__ÚStopAsyncIterationr   r   ÚiterÚnextÚStopIterationr   )r�   r‚   r€   Zfunction_calledZasync_itrl   ÚelementÚitr*   r*   r+   r   ?  sF    
ÿþ


ÿþ

)N)3Ú
__future__r   Ú__all__r{   ÚsysÚcollectionsr	   Úcollections.abcr
   r   r   r   r   r   r   Úinspectr   Útypingr   r   r   r   r   r   r   r   Úweakrefr   Z_core._synchronizationr   Zlowlevelr   r   Úversion_infor!   Ztyping_extensionsr"   r#   r$   r%   r4   r&   r,   r   r   r8   r   rr   r   r   r   r*   r*   r*   r+   Ú<module>   sf   	 (

û
lÿ ÿúü