
    %j                    R   d Z ddlmZ ddlZddlmZmZ ddl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 d	d
lmZmZ d	dlmZ  ed          Z G d dee                   Z G d d          Zedd            Zedd            Zedd            Zed d            Zd!dZdS )"z9App-scoped shared dependencies: Shared and SharedContext.    )annotationsN)	AwaitableCallable)AbstractAsyncContextManagerAbstractContextManagerAsyncExitStack)
ContextVar)TracebackType)AnyClassVarTypeVarcastoverload   )DependencyFactory_FunctionalDependency)get_dependency_parametersRc                  "    e Zd ZdZddZd	dZdS )
_SharedzApp-scoped dependency resolved once and reused across all calls.

    Unlike _Depends (which resolves per-call), _Shared dependencies initialize
    once within a SharedContext and the same instance is provided to all
    subsequent resolutions.
    returnr   c                ,  K   t           j                                        }| j        |v r|| j                 S |                                  d {V }t           j                                        4 d {V  | j        |v r|| j                 cd d d           d {V  S t           j                                        } | j        di |}|                     ||           d {V }||| j        <   |cd d d           d {V  S # 1 d {V swxY w Y   d S )N )SharedContextresolvedgetfactory_resolve_parameterslockstack_resolve_factory_value)selfr   	argumentsr    	raw_valueresolved_values         [/home/rurouni/.hermes/hermes-agent/venv/lib/python3.11/site-packages/uncalled_for/shared.py
__aenter__z_Shared.__aenter__   s      )--//<8##DL))2244444444	 %))++ 		" 		" 		" 		" 		" 		" 		" 		"|x''-		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" "'++--E$11y11I#'#>#>ui#P#PPPPPPPN%3HT\"!		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		" 		"s   5DAD
DDdict[str, Any]c                   K   t           j                                        }i }t          | j                  }|                                D ]#\  }}|                    |           d {V ||<   $|S N)r   r    r   r   r   itemsenter_async_context)r"   r    r#   
parameters	parameter
dependencys         r&   r   z_Shared._resolve_parameters1   s      #''))$&	.t|<<
%/%5%5%7%7 	O 	O!Iz).)B)B:)N)N#N#N#N#N#N#NIi      N)r   r   )r   r(   )__name__
__module____qualname____doc__r'   r   r   r0   r&   r   r      sF         " " " "&     r0   r   c                      e Zd ZU dZ ed          Zded<    ed          Zded<    ed          Zd	ed
<   ddZ	ddZ
dS )r   a  Manages app-scoped Shared dependency lifecycle.

    Use as an async context manager to establish a scope for Shared
    dependencies. All Shared factories resolved within this scope will
    be cached and reused. Context managers are cleaned up when the
    SharedContext exits.

    Example::

        async with SharedContext():
            async with resolved_dependencies(my_func) as dependencies:
                # Shared dependencies are resolved once and cached here
                ...
            async with resolved_dependencies(my_func) as dependencies:
                # Same Shared instances reused
                ...
        # Shared context managers are cleaned up here
    shared_resolvedz7ClassVar[ContextVar[dict[DependencyFactory[Any], Any]]]r   shared_lockz"ClassVar[ContextVar[asyncio.Lock]]r   shared_stackz$ClassVar[ContextVar[AsyncExitStack]]r    r   c                r  K   t                      | _        | j                                         d {V  t          j                            i           | _        t          j                            t          j	                              | _
        t          j                            | j                  | _        | S r*   )r   _stackr'   r   r   set_resolved_tokenr   asyncioLock_lock_tokenr    _stack_token)r"   s    r&   r'   zSharedContext.__aenter__V   s      $&&k$$&&&&&&&&&,599"==(-11',..AA)/33DK@@r0   exc_typetype[BaseException] | None	exc_valueBaseException | None	tracebackTracebackType | NoneNonec                &  K   | j                             |||           d {V  t          j                            | j                   t          j                            | j                   t          j                            | j	                   d S r*   )
r:   	__aexit__r   r    resetr@   r   r?   r   r<   )r"   rA   rC   rE   s       r&   rI   zSharedContext.__aexit__`   s       k##HiCCCCCCCCC!!$"3444  !1222$$T%9:::::r0   N)r   r   )rA   rB   rC   rD   rE   rF   r   rG   )r1   r2   r3   r4   r	   r   __annotations__r   r    r'   rI   r   r0   r&   r   r   <   s          & IS
I IH     0:z-/H/HDHHHH2<*^2L2LELLLL   
; 
; 
; 
; 
; 
;r0   r   r   -Callable[..., AbstractAsyncContextManager[R]]r   c                    d S r*   r   r   s    r&   SharedrO   m   s    ILr0   (Callable[..., AbstractContextManager[R]]c                    d S r*   r   rN   s    r&   rO   rO   o   s    DGCr0   Callable[..., Awaitable[R]]c                    d S r*   r   rN   s    r&   rO   rO   q   s    7:sr0   Callable[..., R]c                    d S r*   r   rN   s    r&   rO   rO   s   s    ,/Cr0   DependencyFactory[R]c                F    t          t          t          |                     S )ub  Declare an app-scoped dependency shared across all calls.

    The factory initializes once within a ``SharedContext`` and the value is
    reused for all subsequent resolutions. Factories may be:

    - A sync function returning a value
    - An async function returning a value
    - A sync generator (context manager) yielding a value
    - An async generator (async context manager) yielding a value

    Context managers are cleaned up when the SharedContext exits.
    Identity is the factory function — multiple ``Shared(same_factory)``
    declarations anywhere resolve to the same cached value.
    )r   r   r   rN   s    r&   rO   rO   u   s     77##$$$r0   )r   rL   r   r   )r   rP   r   r   )r   rR   r   r   )r   rT   r   r   )r   rV   r   r   )r4   
__future__r   r=   collections.abcr   r   
contextlibr   r   r   contextvarsr	   typesr
   typingr   r   r   r   r   
functionalr   r   introspectionr   r   r   r   rO   r   r0   r&   <module>r`      s   ? ? " " " " " "  / / / / / / / /         
 # " " " " "       9 9 9 9 9 9 9 9 9 9 9 9 9 9 @ @ @ @ @ @ @ @ 4 4 4 4 4 4GCLL# # # # ##A& # # #L.; .; .; .; .; .; .; .;b 
 L L L 
 L	 G G G 
 G	 : : : 
 :	 / / / 
 /% % % % % %r0   