ó
±xYc           @  sn   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	 d e
 f d	 „  ƒ  YZ d S(   u5  
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.
iÿÿÿÿ(   t   unicode_literalsN(   t
   is_windowsi   (   t
   select_fdsu   InputHookContextt   InputHookContextc           B  s;   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   u0   
    Given as a parameter to the inputhook.
    c         C  s@   t  | ƒ s t ‚ | |  _ d  |  _ t j ƒ  \ |  _ |  _ d  S(   N(	   t   callablet   AssertionErrort	   inputhookt   Nonet   _input_is_readyt   ost   pipet   _rt   _w(   t   selfR   (    (    sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyt   __init__(   s    		c         C  s   |  j  d t ƒ S(   u6   
        Return True when the input is ready.
        t   wait(   R   t   False(   R   (    (    sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyt   input_is_ready0   s    c         C  s   |  j  S(   u\   
        File descriptor that will become ready when the event loop needs to go on.
        (   R   (   R   (    (    sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyt   fileno6   s    c           s˜   ˆ  ˆ _  ‡  ‡ f d †  } t j d | ƒ j ƒ  ˆ j ˆ ƒ y9 t ƒ  sc t ˆ j g d d ƒn  t	 j
 ˆ j d ƒ Wn t k
 rŠ n Xd ˆ _  d S(   uM   
        Call the inputhook. (Called by a prompt-toolkit eventloop.)
        c             s$   ˆ  d t  ƒ t j ˆ j d ƒ d  S(   NR   t   x(   t   TrueR	   t   writeR   (    (   t   input_is_ready_funcR   (    sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyt   threadC   s    t   targett   timeouti   N(   R   t	   threadingt   Threadt   startR   R   R   R   R   R	   t   readt   OSError(   R   R   R   (    (   R   R   sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyt   call_inputhook<   s    		c         C  s@   |  j  r, t j |  j  ƒ t j |  j ƒ n  d |  _  |  _ d S(   u%   
        Clean up resources.
        N(   R   R	   t   closeR   R   (   R   (    (    sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyR    c   s    	(   t   __name__t
   __module__t   __doc__R   R   R   R   R    (    (    (    sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyR   $   s   				'(   u   InputHookContext(   R#   t
   __future__R    R	   R   t   prompt_toolkit.utilsR   t   selectR   t   __all__t   objectR   (    (    (    sA   lib/python2.7/site-packages/prompt_toolkit/eventloop/inputhook.pyt   <module>   s   