
                         V    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Z	 G d d	e
      Zy)
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                   .    e Zd ZdZd Zd Zd Zd Zd Zy)r   z0
    Given as a parameter to the inputhook.
    c                 |    t        |      sJ || _        d | _        t        j                         \  | _        | _        y )N)callable	inputhook_input_is_readyospipe_r_w)selfr   s     5lib/third_party/prompt_toolkit/eventloop/inputhook.py__init__zInputHookContext.__init__(   s3    	""""#779    c                 &    | j                  d      S )z6
        Return True when the input is ready.
        Fwait)r   r   s    r   input_is_readyzInputHookContext.input_is_ready0   s     ###//r   c                     | j                   S )z\
        File descriptor that will become ready when the event loop needs to go on.
        )r   r   s    r   filenozInputHookContext.fileno6   s     wwr   c                 L     _          fd}t        j                  |      j                           j	                          	 t               st         j                  gd       t        j                   j                  d       d _         y# t        $ r
 Y d _         yw xY w)zM
        Call the inputhook. (Called by a prompt-toolkit eventloop.)
        c                  X      d       t        j                  j                  d       y )NTr      x)r   writer   )input_is_ready_funcr   s   r   threadz/InputHookContext.call_inputhook.<locals>.threadC   s    T*HHTWWd#r   )targetN)timeouti   )r   	threadingThreadstartr   r   r   r   r   readOSError)r   r    r!   s   `` r   call_inputhookzInputHookContext.call_inputhook<   s      3	$ 	'--/ 	t	 <DGG9d3GGDGGT"  $  	 #	s   AB 	B#"B#c                     | j                   r>t        j                  | j                          t        j                  | j                         dx| _         | _        y)z%
        Clean up resources.
        N)r   r   closer   r   s    r   r+   zInputHookContext.closec   s9     77HHTWWHHTWW  $'r   N)	__name__
__module____qualname____doc__r   r   r   r)   r+    r   r   r   r   $   s!    %0%$N!r   r   )r/   
__future__r   r   r$   prompt_toolkit.utilsr   selectr   __all__objectr   r0   r   r   <module>r6      s0   0 ( 	  + 
G!v G!r   