B
    't\                 @   sR   d Z ddlmZ ddlZddlZddlmZ ddlmZ dgZ	G dd de
ZdS )	a5  
Similar to `PyOS_InputHook` of the Python API. Some eventloops can have an
inputhook to allow easy integration with other event loops.

When the eventloop of prompt-toolkit is idle, it can call such a hook. This
hook can call another eventloop that runs for a short while, for instance to
keep a graphical user interface responsive.

It's the responsibility of this hook to exit when there is input ready.
There are two ways to detect when input is ready:

- Call the `input_is_ready` method periodically. Quit when this returns `True`.

- Add the `fileno` as a watch to the external eventloop. Quit when file descriptor
  becomes readable. (But don't read from it.)

  Note that this is not the same as checking for `sys.stdin.fileno()`. The
  eventloop of prompt-toolkit allows thread-based executors, for example for
  asynchronous autocompletion. When the completion for instance is ready, we
  also want prompt-toolkit to gain control again in order to display that.

An alternative to using input hooks, is to create a custom `EventLoop` class that
controls everything.
    )unicode_literalsN)
is_windows   )
select_fdsInputHookContextc               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )r   z0
    Given as a parameter to the inputhook.
    c             C   s   d | _ t \| _| _d S )N)_input_is_readyospipe_r_w)self r   Alib/python3.7/site-packages/prompt_toolkit/eventloop/inputhook.py__init__(   s    zInputHookContext.__init__c             C   s   | j ddS )z6
        Return True when the input is ready.
        F)wait)r   )r   r   r   r   input_is_ready,   s    zInputHookContext.input_is_readyc             C   s   | j S )z\
        File descriptor that will become ready when the event loop needs to go on.
        )r
   )r   r   r   r   fileno2   s    zInputHookContext.filenoc                s   t  stt |st _ fdd}tj|d  | y(t s\tjgdd t	
jd W n tk
r   Y nX d_dS )aH  
        Call the inputhook. (Called by a prompt-toolkit eventloop.)

        :param input_is_ready_func: A callable which returns True when there is
            input ready for the main event loop to process. This means that we
            should quit our input hook. This callable takes a boolean `wait`.
            Wen `True` this needs to be a blocking call which only returns when
            there is input ready.
        :param inputhook: This is a callable that runs the inputhook. This
            function should take this object (the `InputHookContext`) as input.
        c                  s    dd t jd d S )NT)r      x)r   writer   r   )input_is_ready_funcr   r   r   threadJ   s    
z/InputHookContext.call_inputhook.<locals>.thread)targetN)Ztimeouti   )callableAssertionErrorr   	threadingZThreadstartr   r   r
   r   readOSError)r   r   Z	inputhookr   r   )r   r   r   call_inputhook8   s    zInputHookContext.call_inputhookc             C   s.   | j rt| j  t| j d | _ | _dS )z%
        Clean up resources.
        N)r
   r   closer   )r   r   r   r   r   j   s    zInputHookContext.closeN)	__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   $   s   2)r#   Z
__future__r   r   r   Zprompt_toolkit.utilsr   Zselectr   __all__objectr   r   r   r   r   <module>   s   