a
    lc                     @   s   d 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 ddlmZm	Z	 ddl
mZmZmZmZmZmZmZ ddlmZ g dZerdd	lmZ eZed
gdf edddZed
gdf edddZG dd deZG dd
 d
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.
    N)AbstractEventLoop)BaseSelectorSelectorKey)TYPE_CHECKINGAnyCallableListMappingOptionalTuple   )get_event_loop)new_eventloop_with_inputhookset_eventloop_with_inputhookInputHookSelectorInputHookContext)FileDescriptorLiker   )	inputhookreturnc                 C   s   t t | }t|}|S )z;
    Create a new event loop with the given inputhook.
    )r   	selectorsDefaultSelectorasyncioSelectorEventLoop)r   selectorloop r   ^/var/www/html/django/DPS/env/lib/python3.9/site-packages/prompt_toolkit/eventloop/inputhook.pyr   2   s    
r   c                 C   s   t | }t| |S )zL
    Create a new event loop with the given inputhook, and activate it.
    )r   r   set_event_loop)r   r   r   r   r   r   =   s    
r   c                   @   s   e Zd ZdZeedgdf ddddZddded	d
ddZdd	dddZ	ddded	d
ddZ
dee eed  dddZddddZed dddZdS )r   z
    Usage:

        selector = selectors.SelectSelector()
        loop = asyncio.SelectorEventLoop(InputHookSelector(selector, inputhook))
        asyncio.set_event_loop(loop)
    r   N)r   r   r   c                 C   s    || _ || _t \| _| _d S N)r   r   ospipe_r_w)selfr   r   r   r   r   __init__Q   s    zInputHookSelector.__init__r   
_EventMaskr   )fileobjeventsdatar   c                 C   s   | j j|||dS N)r(   )r   registerr#   r&   r'   r(   r   r   r   r*   X   s    zInputHookSelector.register)r&   r   c                 C   s   | j |S r   )r   
unregister)r#   r&   r   r   r   r,   ]   s    zInputHookSelector.unregisterc                 C   s   | j j||d dS r)   )r   modifyr+   r   r   r   r-   `   s    zInputHookSelector.modify)r   r%   )timeoutr   c                    s   t tt dg dkr$jjdS d d d d fdd}tj|d}|  td fd	d
}	t
j| z0tjdkrtjgg g d  tjd W n ty   Y n0 |  d usJ S )N_readyr   r.   Fr   c                      s$   j jdtjd d d S )Nr0      xT)r   selectr   writer"   r   readyresultr#   r.   r   r   run_selectorq   s    z.InputHookSelector.select.<locals>.run_selector)targetc                      s    S r   r   r   )r6   r   r   input_is_readyz   s    z0InputHookSelector.select.<locals>.input_is_readywin32i   )lengetattrr   r   r3   	threadingThreadstartboolr   r   r!   sysplatformr   readOSErrorjoin)r#   r.   r8   thr:   r   r5   r   r3   e   s$    
zInputHookSelector.selectr1   c                 C   s8   | j rt| j  t| j d | _ | _| j  dS )z%
        Clean up resources.
        N)r!   r   closer"   r   r#   r   r   r   rI      s
    zInputHookSelector.close)r   r   c                 C   s
   | j  S r   )r   get_maprJ   r   r   r   rK      s    zInputHookSelector.get_map)N)N)N)__name__
__module____qualname____doc__r   r   r$   r   r*   r,   r-   r
   floatr   r   r3   rI   r	   rK   r   r   r   r   r   H   s,   	   
9r   c                   @   s8   e Zd ZdZeeg ef ddddZedddZdS )	r   z0
    Given as a parameter to the inputhook.
    N)filenor:   r   c                 C   s   || _ || _d S r   )_filenor:   )r#   rQ   r:   r   r   r   r$      s    zInputHookContext.__init__r1   c                 C   s   | j S r   )rR   rJ   r   r   r   rQ      s    zInputHookContext.fileno)	rL   rM   rN   rO   intr   rA   r$   rQ   r   r   r   r   r      s   )rO   r   r   r3   r   rB   r>   r   r   r   typingr   r   r   r   r	   r
   r   utilsr   __all__Z	_typeshedr   rS   r%   r   r   r   r   r   r   r   r   <module>   s,   $e