o
    %h                     @   sv   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
mZ d dlmZ d dlmZmZ eZdgZG dd deZdS )	    )iscoroutine)AnyCallablecastDictOptionalTuple)DeferredensureDeferred)Failure)EventEmitter	PyeeErrorTwistedEventEmitterc                
       sv   e Zd ZdZdeddf fddZdededeed	f d
e	e
ef ddf
ddZdede
deddf fddZ  ZS )r   a  An event emitter class which can run twisted coroutines and handle
    returned Deferreds, in addition to synchronous blocking functions. For
    example:

    ```py
    @ee.on('event')
    @inlineCallbacks
    def async_handler(*args, **kwargs):
        yield returns_a_deferred()
    ```

    or:

    ```py
    @ee.on('event')
    async def async_handler(*args, **kwargs):
        await returns_a_deferred()
    ```


    When async handlers fail, Failures are first emitted on the `failure`
    event. If there are no `failure` handlers, the Failure's associated
    exception is then emitted on the `error` event. If there are no `error`
    handlers, the exception is raised. For consistency, when handlers raise
    errors synchronously, they're captured, wrapped in a Failure and treated
    as an async failure. This is unlike the behavior of EventEmitter,
    which have no special error handling.

    For twisted coroutine event handlers, calling emit is non-blocking.
    In other words, you do not have to await any results from emit, and the
    coroutine is scheduled in a fire-and-forget fashion.

    Similar behavior occurs for "sync" functions which return Deferreds.
    selfreturnNc                    s   t t|   d S )N)superr   __init__r   	__class__ @/var/www/html/myenv/lib/python3.10/site-packages/pyee/twisted.pyr   5   s   zTwistedEventEmitter.__init__fargs.kwargsc                    s   d }z	||i |}W n t y    dt  Y d S w t|r&t|}nt|tr.|}n|s2d S dtdd f fdd}|| d S )Nfailurer   c                    s   | r
  d|  d S d S )Nr   )emit)r   r   r   r   errbackK   s   z.TwistedEventEmitter._emit_run.<locals>.errback)	Exceptionr   r   r   r
   
isinstancer	   
addErrback)r   r   r   r   dresultr   r   r   r   	_emit_run8   s   

zTwistedEventEmitter._emit_runeventerrorc              
      s   |dkrDt |tr*z|  W d S  ty) } z| d| W Y d }~d S d }~ww t |tr7| d| d S | dtd|  d S tttt	| 
|| d S )Nr   r%   zUnexpected failure object: )r   r   raiseExceptionr   r   r   r   r   r   r   _emit_handle_potential_error)r   r$   r%   excr   r   r   r'   Q   s   

z0TwistedEventEmitter._emit_handle_potential_error)__name__
__module____qualname____doc__Selfr   r   r   r   r   strr#   r'   __classcell__r   r   r   r   r      s    #


&N)asyncior   typingr   r   r   r   r   r   twisted.internet.deferr	   r
   twisted.python.failurer   	pyee.baser   r   r-   __all__r   r   r   r   r   <module>   s    