a
    ÝŒxd¨  ã                   @   s|   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„ deƒZdS )é    )ÚAbstractAsyncContextManagerÚasynccontextmanager)ÚTracebackType)ÚAnyÚAsyncGeneratorÚ	AwaitableÚCallableÚDictÚOptionalÚTupleÚTypeN)ÚEventEmitterÚPyeeExceptionÚTrioEventEmitterc                       sÊ   e Zd ZdZdeddœ‡ fdd„Zeeedf e	e
ef eg ed f dœd	d
„Zeeedf e	e
ef ddœdd„Zeed dœdd„ƒZd dœdd„Zeee  ee ee ee dœdd„Z‡  ZS )r   a„  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::

        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.
    Nz)AbstractAsyncContextManager[trio.Nursery])ÚnurseryÚmanagerc                    sL   t t| ƒ ¡  d | _d | _|r2|r*tdƒ‚|| _n|r>|| _n
t ¡ | _d S )Nz?You may either pass a nursery or a nursery manager but not both)Úsuperr   Ú__init__Ú_nurseryÚ_managerr   ÚtrioZopen_nursery)Úselfr   r   ©Ú	__class__© úA/var/www/html/Ranjet/env/lib/python3.9/site-packages/pyee/trio.pyr   2   s    ÿzTrioEventEmitter.__init__.)ÚfÚargsÚkwargsÚreturnc                    s   d dœ‡ ‡‡‡fdd„}|S )N©r   c               
   “   sN   zˆˆ i ˆ¤ŽI d H  W n0 t yH }  zˆ d| ¡ W Y d } ~ n
d } ~ 0 0 d S )NÚerror)Ú	ExceptionÚemit)Úexc©r   r   r   r   r   r   ÚrunnerK   s    z.TrioEventEmitter._async_runner.<locals>.runnerr   )r   r   r   r   r&   r   r%   r   Ú_async_runnerE   s    zTrioEventEmitter._async_runnerc                 C   s(   | j stdƒ‚| j  |  |||¡¡ d S )NzUninitialized trio nursery)r   r   Z
start_soonr'   )r   r   r   r   r   r   r   Ú	_emit_runS   s    zTrioEventEmitter._emit_run)r   Nr    c              	   C  sp   | j dur| V  nZ| jdurd| j4 I dH š"}|| _ | V  W d  ƒI dH  ql1 I dH sX0    Y  ntdƒ‚dS )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   r   r   r   Úcontext]   s    	

2zTrioEventEmitter.contextc                 Ã   s   |   ¡ | _| j ¡ I d H S )N)r)   Ú_contextÚ
__aenter__)r   r   r   r   r+   o   s    þzTrioEventEmitter.__aenter__)ÚtypeÚvalueÚ	tracebackr   c                 Ã   s>   | j d u rtdƒ‚| j  |||¡I d H }d | _ d | _d | _|S )Nz(Attempting to exit uninitialized context)r*   r   Ú	__aexit__r   r   )r   r,   r-   r.   Úrvr   r   r   r/   u   s    
zTrioEventEmitter.__aexit__)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚNurseryr   r   r   r   r	   Ústrr   r'   r(   r   r   r)   r+   r
   r   ÚBaseExceptionr   Úboolr/   Ú__classcell__r   r   r   r   r      s4   "  ýý

û

û
þ
û)Ú
contextlibr   r   Útypesr   Útypingr   r   r   r   r	   r
   r   r   r   Z	pyee.baser   r   Ú__all__r5   r   r   r   r   r   Ú<module>   s   (