B
    '˜t\i  ã               @   sj   d Z ddlmZ ddlmZmZ ddlmZ ddlm	Z	 ddl
Z
ddgZG d	d„ deeeƒƒZd
d„ ZdS )aR  
Base event loop interface.

The naming convention is kept similar to asyncio as much as possible.

A special thanks to asyncio (tulip), Twisted, Tornado and Trollius for setting
a good example on how to implement event loops. Possible, in the future, we'll
run entirely on top of asyncio, but right now, we're still supporting Python 2.
é    )Úunicode_literals)ÚABCMetaÚabstractmethod)Úwith_metaclass)ÚloggerNÚ	EventLoopÚget_traceback_from_contextc               @   s¨   e Zd ZdZdd„ Zed"dd„ƒZd#dd„Zed	d
„ ƒZedd„ ƒZ	edd„ ƒZ
dd„ Zdd„ Zed$dd„ƒZed%dd„ƒZdd„ Zdd„ Zdd„ Zdd„ Zd d!„ ZdS )&r   z
    Eventloop interface.
    c             C   s
   d | _ d S )N)Ú_exception_handler)Úself© r   ú<lib/python3.7/site-packages/prompt_toolkit/eventloop/base.pyÚ__init__   s    zEventLoop.__init__Nc             C   s   dS )zz
        Keep running until this future has been set.
        Return the Future's result, or raise its exception.
        Nr   )r
   ÚfutureÚ	inputhookr   r   r   Úrun_until_complete   s    zEventLoop.run_until_completec             C   s   |   ¡ }| j||d dS )z#
        Run loop forever.
        )r   N)Úcreate_futurer   )r
   r   Úfr   r   r   Úrun_forever$   s    zEventLoop.run_foreverc             C   s   dS )zj
        Clean up of resources. Eventloop cannot be reused a second time after
        this call.
        Nr   )r
   r   r   r   Úclose+   s    zEventLoop.closec             C   s   dS )zn
        Start watching the file descriptor for read availability and then call
        the callback.
        Nr   )r
   ÚfdÚcallbackr   r   r   Ú
add_reader2   s    zEventLoop.add_readerc             C   s   dS )zJ
        Stop watching the file descriptor for read availability.
        Nr   )r
   r   r   r   r   Úremove_reader9   s    zEventLoop.remove_readerc             C   s   t ‚dS )z`
        Add a Windows Handle to the event loop.
        (Only applied to win32 loops.)
        N)ÚNotImplementedError)r
   Úhandler   r   r   r   Úadd_win32_handle?   s    zEventLoop.add_win32_handlec             C   s   t ‚dS )ze
        Remove a Windows Handle from the event loop.
        (Only applied to win32 loops.)
        N)r   )r
   r   r   r   r   Úremove_win32_handleF   s    zEventLoop.remove_win32_handleFc             C   s   dS )z½
        Run a long running function in a background thread. (This is
        recommended for code that could block the event loop.)
        Similar to Twisted's ``deferToThread``.
        Nr   )r
   r   Z_daemonr   r   r   Úrun_in_executorM   s    zEventLoop.run_in_executorc             C   s   dS )ar  
        Call this function in the main event loop. Similar to Twisted's
        ``callFromThread``.

        :param _max_postpone_until: `None` or `time.time` value. For internal
            use. If the eventloop is saturated, consider this task to be low
            priority and postpone maximum until this timestamp. (For instance,
            repaint is done using low priority.)

            Note: In the past, this used to be a datetime.datetime instance,
                  but apparently, executing `time.time` is more efficient: it
                  does fewer system calls. (It doesn't read /etc/localtime.)
        Nr   )r
   r   Z_max_postpone_untilr   r   r   Úcall_from_executorU   s    zEventLoop.call_from_executorc             C   s   ddl m} || dS )z€
        Create a `Future` object that is attached to this loop.
        This is the preferred way of creating futures.
        é   )ÚFuture)Zloop)r   r    )r
   r    r   r   r   r   e   s    zEventLoop.create_futurec             C   s   |dkst |ƒst‚|| _dS )z,
        Set the exception handler.
        N)ÚcallableÚAssertionErrorr	   )r
   Zhandlerr   r   r   Úset_exception_handlerm   s    zEventLoop.set_exception_handlerc             C   s   | j S )z/
        Return the exception handler.
        )r	   )r
   r   r   r   Úget_exception_handlert   s    zEventLoop.get_exception_handlerc             C   sp   | j r:y|   |¡ W ql tk
r6   tjddd Y qlX n2y|  |¡ W n" tk
rj   tjddd Y nX dS )z‡
        Call the current event loop exception handler.
        (Similar to ``asyncio.BaseEventLoop.call_exception_handler``.)
        z&Exception in default exception handlerT)Úexc_infozeException in default exception handler while handling an unexpected error in custom exception handlerN)r	   Ú	Exceptionr   ÚerrorÚdefault_exception_handler)r
   Úcontextr   r   r   Úcall_exception_handlerz   s    z EventLoop.call_exception_handlerc       	      C   sœ   |  d¡}|sd}|  d¡}|dk	r<t|ƒ}t|ƒ||f}nd}|g}x<t|ƒD ]0}|dkr^qP|| }t|ƒ}| d ||¡¡ qPW tjd 	|¡|d	 dS )
z[
        Default exception handling.

        Thanks to asyncio for this function!
        Úmessagez!Unhandled exception in event loopÚ	exceptionNF)r+   r,   z{}: {}Ú
)r%   )
Úgetr   ÚtypeÚsortedÚreprÚappendÚformatr   r'   Újoin)	r
   r)   r+   r,   Útbr%   Z	log_linesÚkeyÚvaluer   r   r   r(   Ž   s     

z#EventLoop.default_exception_handler)N)N)F)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r   r   r#   r$   r*   r(   r   r   r   r   r      s&   
c             C   s.   |   d¡}|r*t|dƒr|jS t ¡ d S dS )z4
    Get the traceback object from the context.
    r,   Ú__traceback__é   N)r.   Úhasattrr<   Úsysr%   )r)   r,   r   r   r   r   ª   s    

)r;   Z
__future__r   Úabcr   r   Zsixr   Zprompt_toolkit.logr   r?   Ú__all__Úobjectr   r   r   r   r   r   Ú<module>	   s    