
    Mh                       U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddlm	Z	m
Z
 ddlmZmZ ddlmZmZmZmZ g dZerddlmZ dd	lmZ eZ G d
 d          Zeegdf         Zded<   ddZddZ G d de          ZdS )a8  
Similar to `PyOS_InputHook` of the Python API, we can plug in an input hook in
the asyncio event loop.

The way this works is by using a custom 'selector' that runs the other event
loop until the real selector is ready.

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

The inputhook itself is a callable that receives an `InputHookContext`. This
callable should run the other event loop, and return when the main loop has
stuff to do. There are two ways to detect when to return:

- 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.
    )annotationsN)AbstractEventLoopget_running_loop)BaseSelectorSelectorKey)TYPE_CHECKINGAnyCallableMapping)new_eventloop_with_inputhookset_eventloop_with_inputhookInputHookSelectorInputHookContext	InputHook)FileDescriptorLike)	TypeAliasc                  "    e Zd ZdZddZdd	Zd
S )r   z0
    Given as a parameter to the inputhook.
    filenointinput_is_readyCallable[[], bool]returnNonec                "    || _         || _        d S N)_filenor   )selfr   r   s      b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/prompt_toolkit/eventloop/inputhook.py__init__zInputHookContext.__init__:   s    ,    c                    | j         S r   )r   r   s    r   r   zInputHookContext.fileno>   s
    |r    N)r   r   r   r   r   r   )r   r   )__name__
__module____qualname____doc__r   r    r    r   r   r   5   sF         - - - -     r    r   r   r   	inputhook"Callable[[InputHookContext], None]r   r   c                p    t          t          j                    |           }t          j        |          }|S )z;
    Create a new event loop with the given inputhook.
    )r   	selectorsDefaultSelectorasyncioSelectorEventLoop)r(   selectorloops      r   r   r   E   s1     !!:!<!<iHHH$X..DKr    c                L    t          |           }t          j        |           |S )zL
    Create a new event loop with the given inputhook, and activate it.
    )r   r-   set_event_loop)r(   r0   s     r   r   r   P   s'     (	22D4   Kr    c                  V    e Zd ZdZddZ	 dddZddZ	 dddZ	 dddZd dZ	d!dZ
d	S )"r   z
    Usage:

        selector = selectors.SelectSelector()
        loop = asyncio.SelectorEventLoop(InputHookSelector(selector, inputhook))
        asyncio.set_event_loop(loop)
    r/   r   r(   r)   r   r   c                b    || _         || _        t          j                    \  | _        | _        d S r   )r/   r(   ospipe_r_w)r   r/   r(   s      r   r   zInputHookSelector.__init__f   s*     !"799r    Nfileobjr   events
_EventMaskdatar	   r   c                <    | j                             |||          S N)r<   )r/   registerr   r9   r:   r<   s       r   r?   zInputHookSelector.registerm   s!     }%%gvD%AAAr    c                6    | j                             |          S r   )r/   
unregister)r   r9   s     r   rB   zInputHookSelector.unregisterr   s    }''000r    c                <    | j                             ||d           S r>   )r/   modifyr@   s       r   rD   zInputHookSelector.modifyu   s!     }##GV$#???r    timeoutfloat | None$list[tuple[SelectorKey, _EventMask]]c                J    t          t          t                      dg                     dk    r j                                      S dd d fd}t          j        |          }|                                 dfd
}                     t           j
        |                     	 t          j        dk    rt	          j         j
        gg g d            t          j         j
        d           n# t          $ r Y nw xY w|                                 J S )N_readyr   rE   Fr   r   c                 v    j                                       t          j        j        d           d d S )NrJ      xT)r/   selectr5   writer8   )readyresultr   rE   s   r   run_selectorz.InputHookSelector.select.<locals>.run_selector   s8    ]))')::FHTWd###EEEr    )targetboolc                      S r   r'   )rO   s   r   r   z0InputHookSelector.select.<locals>.input_is_ready   s    Lr    win32i   r   r   )r   rS   )lengetattrr   r/   rM   	threadingThreadstartr(   r   r7   sysplatformr5   readOSErrorjoin)r   rE   rQ   thr   rO   rP   s   ``   @@r   rM   zInputHookSelector.selectz   sf   
 w'))8R8899A==='''888	 	 	 	 	 	 	 	 	 \222



	 	 	 	 	 	 	'@@AAA	 |w&&twiR666GDGT"""" 	 	 	 D		 				!!!s   3AC; ;
DDc                    | j         r2t          j        | j                    t          j        | j                   dx| _         | _        | j                                         dS )z%
        Clean up resources.
        N)r7   r5   closer8   r/   r"   s    r   rd   zInputHookSelector.close   sZ     7 	HTWHTW$'r    (Mapping[FileDescriptorLike, SelectorKey]c                4    | j                                         S r   )r/   get_mapr"   s    r   rg   zInputHookSelector.get_map   s    }$$&&&r    )r/   r   r(   r)   r   r   r   )r9   r   r:   r;   r<   r	   r   r   )r9   r   r   r   )rE   rF   r   rG   rV   )r   re   )r#   r$   r%   r&   r   r?   rB   rD   rM   rd   rg   r'   r    r   r   r   ]   s         % % % % LPB B B B B
1 1 1 1 LP@ @ @ @ @ '+7 7 7 7 7r	 	 	 	' ' ' ' ' 'r    r   )r(   r)   r   r   )r&   
__future__r   r-   r5   rM   r+   r\   rY   r   r   r   r   typingr   r	   r
   r   __all__	_typeshedr   typing_extensionsr   r   r;   r   r   __annotations__r   r   r   r'   r    r   <module>rn      s    2 # " " " " "  				      



     7 7 7 7 7 7 7 7 / / / / / / / / 8 8 8 8 8 8 8 8 8 8 8 8    ,,,,,,++++++J
 
 
 
 
 
 
 
  !1 2D 89	 9 9 9 9   
 
 
 
b' b' b' b' b' b' b' b' b' b'r    