
    g                         d dl mZ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Zd dlmZmZ dgZej&                  Z G d de      Zy)    )AbstractAsyncContextManagerasynccontextmanager)TracebackType)AnyAsyncGenerator	AwaitableCallableDictOptionalTupleTypeN)EventEmitter	PyeeErrorTrioEventEmitterc                        e Zd ZdZ	 	 ddee   ded   f fdZdedee	d	f   d
e
ee	f   deg ed   f   fdZdedee	d	f   d
e
ee	f   ddfdZeded   fd       ZddZdeee      dee   dee   dee   fdZ xZS )r   ar  An event emitter class which can run trio tasks in a trio nursery.

    By default, this class will lazily create both a nursery manager (the
    object returned from `trio.open_nursery()` and a nursery (the object
    yielded by using the nursery manager as an async context manager). It is
    also possible to supply an existing nursery manager via the `manager`
    argument, or an existing nursery via the `nursery` argument.

    Instances of TrioEventEmitter are themselves async context managers, so
    that they may manage the lifecycle of the underlying trio nursery. For
    example, typical usage of this library may look something like this::

    ```py
    async with TrioEventEmitter() as ee:
        # Underlying nursery is instantiated and ready to go
        @ee.on('data')
        async def handler(data):
            print(data)

        ee.emit('event')

    # Underlying nursery and manager have been cleaned up
    ```

    Unlike the case with the EventEmitter, all exceptions raised by event
    handlers are automatically emitted on the `error` event. This is
    important for trio coroutines specifically but is also handled for
    synchronous functions for consistency.

    For trio coroutine event handlers, calling emit is non-blocking. In other
    words, you should not attempt to await emit; the coroutine is scheduled
    in a fire-and-forget fashion.
    Nnurserymanagerz)AbstractAsyncContextManager[trio.Nursery]c                     t         t        |           d | _        d | _        |r|rt        d      || _        y |r|| _        y t        j                         | _        y )Nz?You may either pass a nursery or a nursery manager but not both)superr   __init___nursery_managerr   trioopen_nursery)selfr   r   	__class__s      >/var/www/openai/venv/lib/python3.12/site-packages/pyee/trio.pyr   zTrioEventEmitter.__init__4   sZ    
 	.0+/OSX  $DM#DM --/DM    fargs.kwargsreturnc                       d fd}|S )Nc                     K   	  i  d {    y 7 # t         $ r} j                  d|        Y d } ~ y d } ~ ww xY ww)Nerror)	Exceptionemit)excr    r   r!   r   s    r   runnerz.TrioEventEmitter._async_runner.<locals>.runnerM   s?     ((((( (		'3''(s0   A   A  	=8A =A )r"   N )r   r   r    r!   r)   s   ```` r   _async_runnerzTrioEventEmitter._async_runnerG   s    	( 	( r   c                     | j                   st        d      | j                   j                  | j                  |||             y )NzUninitialized trio nursery)r   r   
start_soonr+   )r   r   r    r!   s       r   	_emit_runzTrioEventEmitter._emit_runU   s8     }}899  !3!3AtV!DEr   )r   Nc                   K   | j                   |  y| j                  2| j                  4 d{   }|| _         |  ddd      d{    yt        d      7 ,7 # 1 d{  7  sw Y   yxY ww)a  Returns an async contextmanager which manages the underlying
        nursery to the EventEmitter. The `TrioEventEmitter`'s
        async context management methods are implemented using this
        function, but it may also be used directly for clarity.
        Nz(Uninitialized nursery or nursery manager)r   r   r   )r   r   s     r   contextzTrioEventEmitter.context_   s^      ==$J]]&}}} '
 %}} FGG	 %}}}}sD   /A7AA7A"A7A A7 A7"A4(A+)A40A7c                 |   K   | j                         | _        | j                  j                          d {   S 7 w)N)r0   _context
__aenter__)r   s    r   r3   zTrioEventEmitter.__aenter__q   s1     LLN 	 ]]--////s   3<:<typevalue	tracebackc                    K   | j                   t        d      | j                   j                  |||       d {   }d | _         d | _        d | _        |S 7 w)Nz(Attempting to exit uninitialized context)r2   r   	__aexit__r   r   )r   r4   r5   r6   rvs        r   r8   zTrioEventEmitter.__aexit__w   sY      == FGG==**4	BB		 Cs   8AAA)NN)r"   r   )__name__
__module____qualname____doc__r   Nurseryr   r	   r   r   r
   strr   r+   r.   r   r   r0   r3   r   BaseExceptionr   boolr8   __classcell__)r   s   @r   r   r      s)    H &*IM0'"0 EF0& CHo S#X	
 
"io%	&FF CHoF S#X	F
 
F H	0	1H H"0tM*+ & M*	
 
$r   )
contextlibr   r   typesr   typingr   r   r   r	   r
   r   r   r   r   	pyee.baser   r   __all__r>   r   r*   r   r   <module>rH      s>    H  X X X  -
 ,,r| rr   