o
    m=8j.                     @  s  U d dl mZ d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mZ d d	lmZ d
dlmZ d
dlmZ d
dl m!Z!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Z.edJd$d%Z/edKd(d%Z/dLd+d%Z/ed,d,d,d,d-dMd4d5Z0edJd6d5Z0edKd7d5Z0	dNd8d9d9dd-dOd;d5Z0edPdAdBZ1edQdEdBZ1e)fdRdIdBZ1dS )S    )annotations)AsyncCacheInfoAsyncCacheParametersAsyncLRUCacheWrappercache	lru_cachereduceN)OrderedDict)AsyncIterable	AwaitableCallable	CoroutineHashableIterable)update_wrapper)iscoroutinefunction)	AnyGeneric
NamedTuple	ParamSpec	TypedDictTypeVarcastfinaloverload)WeakKeyDictionary   )current_time)Lock)RunVar
checkpointTSPlru_cache_itemszRunVar[WeakKeyDictionary[AsyncLRUCacheWrapper[Any, Any], OrderedDict[Hashable, tuple[_InitialMissingType, Lock, float | None] | tuple[Any, None, float | None]]]]c                   @  s   e Zd ZdS )_InitialMissingTypeN)__name__
__module____qualname__ r)   r)   V/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/anyio/functools.pyr%   8   s    r%   initial_missingc                   @  s6   e Zd ZU ded< ded< ded< ded< ded< dS )	r   inthitsmisses
int | NonemaxsizecurrsizettlN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ed< dS )r   r/   r0   booltypedalways_checkpointr2   Nr3   r)   r)   r)   r*   r   G   s
   
 r   c                   @  s>   e Zd ZdddZdd	d
ZdddZdddZdddZdS )_LRUMethodWrapperwrapperAsyncLRUCacheWrapper[..., T]instanceobjectc                 C  s   || _ || _d S N)_LRUMethodWrapper__wrapper_LRUMethodWrapper__instance)selfr9   r;   r)   r)   r*   __init__O   s   
z_LRUMethodWrapper.__init__returnr   c                 C  
   | j  S r=   )r>   
cache_infor@   r)   r)   r*   rD   S      
z_LRUMethodWrapper.cache_infor   c                 C  rC   r=   )r>   cache_parametersrE   r)   r)   r*   rG   V   rF   z"_LRUMethodWrapper.cache_parametersNonec                 C  s   | j   d S r=   )r>   cache_clearrE   r)   r)   r*   rI   Y   s   z_LRUMethodWrapper.cache_clearargsr   kwargsr!   c                   sB   | j d u r| j|i |I d H S | j| j g|R i |I d H S r=   )r?   r>   )r@   rJ   rK   r)   r)   r*   __call__\   s   
 z_LRUMethodWrapper.__call__N)r9   r:   r;   r<   rB   r   rB   r   rB   rH   )rJ   r   rK   r   rB   r!   )r&   r'   r(   rA   rD   rG   rI   rL   r)   r)   r)   r*   r8   N   s    



r8   c                   @  sL   e Zd Zd$d	d
Zd%ddZd&ddZd'ddZd(ddZ	d)d*d"d#ZdS )+r   funcCallable[P, Awaitable[T]]r0   r/   r6   r5   r7   r2   c                 C  sP   || _ d| _d| _|d urt|dnd | _d| _|| _|| _|| _t	| | d S Nr   )
__wrapped___hits_missesmax_maxsize	_currsize_typed_always_checkpoint_ttlr   )r@   rP   r0   r6   r7   r2   r)   r)   r*   rA   e   s   zAsyncLRUCacheWrapper.__init__rB   r   c                 C  s   t | j| j| j| j| jS r=   )r   rT   rU   rW   rX   r[   rE   r)   r)   r*   rD   w   s   zAsyncLRUCacheWrapper.cache_infor   c                 C  s   | j | j| j| jdS )Nr0   r6   r7   r2   rW   rY   rZ   r[   rE   r)   r)   r*   rG   |   s
   z%AsyncLRUCacheWrapper.cache_parametersrH   c                 C  s4   t d  }r|| d  d | _ | _| _d S d S rR   )r$   getpoprT   rU   rX   )r@   r   r)   r)   r*   rI      s   z AsyncLRUCacheWrapper.cache_clearrJ   P.argsrK   P.kwargsr!   c           
   	     s  | j dkr| j|i |I d H }|  jd7  _|S |}|r*|tft| d 7 }| jrJ|tdd |D 7 }|rJ|tftdd | D  7 }zt	
 }W n tya   t }t	| Y nw z||  }W n tyx   t  }|| < Y nw z	|| \}}}	W n ty   tt| j dd }}}	|||	f||< Y nw |d u r|	d urt |	kr|  jd8  _tt| j dd }}}	|||	f||< n|  jd7  _|| | jrt I d H  tt|S |4 I d H k || d  }tu r8|  jd7  _| j d ur| j| j kr|jdd	 n|  jd7  _| j|i |I d H }| jd ur.t | j nd }	|d |	f||< n|  jd7  _|| tt|}W d   I d H  |S 1 I d H s[w   Y  |S )
Nr   r   r)   c                 s      | ]}t |V  qd S r=   type).0argr)   r)   r*   	<genexpr>       z0AsyncLRUCacheWrapper.__call__.<locals>.<genexpr>c                 s  rb   r=   rc   )re   valr)   r)   r*   rg      rh   )fast_acquireF)last)rW   rS   rU   r+   sumitemsrY   tuplevaluesr$   r^   LookupErrorr   setKeyErrorr	   r   rZ   r   rX   rT   move_to_endr    r   r!   popitemr[   )
r@   rJ   rK   valuekeyr   cache_entrycached_valuelock
expires_atr)   r)   r*   rL      s|   
 
	



zAsyncLRUCacheWrapper.__call__Nr;   r<   ownertype | None_LRUMethodWrapper[T]c                 C  s   t | |}t|| j |S r=   )r8   r   rS   )r@   r;   r{   r9   r)   r)   r*   __get__   s   
zAsyncLRUCacheWrapper.__get__)
rP   rQ   r0   r/   r6   r5   r7   r5   r2   r/   rM   rN   rO   )rJ   r`   rK   ra   rB   r!   r=   )r;   r<   r{   r|   rB   r}   )	r&   r'   r(   rA   rD   rG   rI   rL   r~   r)   r)   r)   r*   r   c   s    




Rr   c                   @  s<   e Zd ZdddZedddZedddZdddZdS )_LRUCacheWrapperr0   r/   r6   r5   r7   r2   c                 C  s   || _ || _|| _|| _d S r=   r]   )r@   r0   r6   r7   r2   r)   r)   r*   rA      s   
z_LRUCacheWrapper.__init__rP   #Callable[P, Coroutine[Any, Any, T]]rB   AsyncLRUCacheWrapper[P, T]c                C     d S r=   r)   r@   rP   r)   r)   r*   rL         z_LRUCacheWrapper.__call__Callable[..., T]functools._lru_cache_wrapper[T]c                C  r   r=   r)   r   r)   r)   r*   rL      r   f6Callable[P, Coroutine[Any, Any, T]] | Callable[..., T]<AsyncLRUCacheWrapper[P, T] | functools._lru_cache_wrapper[T]c                C  s6   t |rt|| j| j| j| jS tj| j| jd|S )N)r0   r6   )r   r   rW   rY   rZ   r[   	functoolsr   )r@   r   r)   r)   r*   rL      s
   N)r0   r/   r6   r5   r7   r5   r2   r/   rP   r   rB   r   rP   r   rB   r   )r   r   rB   r   )r&   r'   r(   rA   r   rL   r)   r)   r)   r*   r      s    
r   rP   r   rB   r   c                C  r   r=   r)   rP   r)   r)   r*   r      r   r   r   r   c                C  r   r=   r)   r   r)   r)   r*   r        :Callable[..., Any] | Callable[P, Coroutine[Any, Any, Any]]r   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)r0   )r   r   r)   r)   r*   r   
  s   .r\   r0   r/   r6   r5   r7   r2   c                 C  r   r=   r)   r\   r)   r)   r*   r     s   r   c                C  r   r=   r)   r   r)   r)   r*   r     r   c                C  r   r=   r)   r   r)   r)   r*   r   $  r      FCCallable[..., Coroutine[Any, Any, Any]] | Callable[..., Any] | Nonec               C  s8   | du rt ||||S t| std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
    :param ttl: time in seconds after which to invalidate cache entries

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

    Nz#the first argument must be callable)r   callable	TypeError)rP   r0   r6   r7   r2   r)   r)   r*   r   (  s
   initialfunctionCallable[[T, S], Awaitable[T]]iterableIterable[S] | AsyncIterable[S]c                     d S r=   r)   )r   r   r   r)   r)   r*   r   G  s   r   Callable[[T, T], Awaitable[T]]Iterable[T] | AsyncIterable[T]c                  r   r=   r)   )r   r   r)   r)   r*   r   P  s   T | _InitialMissingType?Callable[[T, T], Awaitable[T]] | Callable[[T, S], Awaitable[T]]?Iterable[T] | Iterable[S] | AsyncIterable[T] | AsyncIterable[S]c                  s  d}t |trC| }|tu r)ztt| I dH }W n ty(   tddw tt|}|2 z3 dH W }| ||I dH }d}q06 n<t |t	r{t
|}|tu rfz	ttt|}W n tye   tddw tt|}|D ]}| ||I dH }d}qmntd|st 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   function_calledasync_itru   elementitr)   r)   r*   r   X  sP   



r   r   )rP   r   rB   r   )
r0   r/   r6   r5   r7   r5   r2   r/   rB   r   r=   )rP   r   r0   r/   r6   r5   r7   r5   r2   r/   rB   r   )r   r!   r   r   r   r   rB   r!   )r   r   r   r   rB   r!   )r   r   r   r   r   r   rB   r!   )2
__future__r   __all__r   collectionsr	   collections.abcr
   r   r   r   r   r   r   inspectr   typingr   r   r   r   r   r   r   r   r   weakrefr   _core._eventloopr   _core._synchronizationr   lowlevelr   r    r!   r"   r#   r$   r4   r%   r+   r   r   r8   r   r   r   r   r   r)   r)   r)   r*   <module>   sj    	 ,

~

	