
    e/                        S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJ	r	  S SK
JrJr  S SKJr  SSKJrJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJrJrJr  Sr\R                  r " S S\5      r  " S S\!5      r"g)    )unicode_literalsN)InputStream)DummyContextin_main_thread)Input   )	EventLoopINPUT_TIMEOUT)EventLoopCallbacks)InputHookContext)PosixStdinReader)TimeIt)AutoSelectorSelector	fd_to_int)PosixEventLoopc                   d    \ rS rSrSrS\4S jrS rSS jrS r	S r
SS	 jrS
 rS rS rS rSrg)r      z1
Event loop for posix systems (Linux, Mac os X).
Nc                    Ub  [        U5      (       d   e[        U[        5      (       d   eSU l        SU l        SU l        S U l        / U l        0 U l        U" 5       U l	        [        R                  " 5       U l        [        R                  " U R                  S   [        R                  [        R                  5        U(       a  [!        U5      U l        g S U l        g )NFr   )callable
issubclassr   runningclosed_running
_callbacks_calls_from_executor	_read_fdsselectorospipe_schedule_pipefcntlF_SETFL
O_NONBLOCKr   _inputhook_context)self	inputhookr   s      1lib/third_party/prompt_toolkit/eventloop/posix.py__init__PosixEventLoop.__init__   s     HY$7$777(H----$&! 
 !ggiD''*EMM2==I BK"29"=PT    c                 L  ^ ^^^ [        U[        5      (       d   e[        U[        5      (       d   eT R                  (       a   eT R                  (       a  [        S5      eST l        UT l        [        UR                  5      m[        /m[        UR                  5       5      m[        5       (       a  [        T R                  5      nO
[        5       nUUU U4S jnT R!                  X5        T R!                  T R"                  S   S5        U   T R                  (       Ga"  T R$                  (       aB  ['        5        nUU 4S jnT R$                  R)                  U5        SSS5        WR*                  nOSnTS   c  SnO[-        STS   U-
  5      nT R/                  U5      n	U	(       Ga`  / n
/ nSnU	 H  nUT R"                  S   :X  a  T R0                   HX  u  pUc  U
R3                  U5        M  U=(       d
    [5        5       nX:  a  U
R3                  U5        MF  UR3                  X45        MZ     / T l        [6        R8                  " T R"                  S   S5        M  T R:                  R=                  U5      nU(       d  M  U
R3                  U5        M     [>        R@                  " U
5        [>        R@                  " U5        U
(       a-  U
 H
  nU" 5         M     U H  u  nnT RC                  UUS9  M     O9U H  u  nnU" 5         M     O%TRE                  5         URG                  5         STS'   T R                  (       a  GM"  SSS5        T RI                  U5        T RI                  T R"                  S   5        ST l        g! , (       d  f       GN*= f! , (       d  f       NW= f)	z
The input 'event loop'.
zEvent loop already closed.Tc                     > TR                  5       n TR                  U 5        [        TS'   TR                  (       a  TR	                  5         gg)zRead user input. r   N)readfeedr
   r   stop)datacurrent_timeoutinputstreamr&   stdin_readers    r(   read_from_stdin+PosixEventLoop.run.<locals>.read_from_stdinO   sF      $$&DT" "/OA ""		 #r+   r   Nc                 R   > TR                  U (       a  TS   5      / :g  $ S5      / :g  $ )zETrue when there is input ready. The inputhook should return control. r   )_ready_for_reading)waitr2   r&   s    r(   ready!PosixEventLoop.run.<locals>.readyd   s1    #'#:#:QU?1;M#]ac#cc[\#]ac#ccr+   i   )_max_postpone_until)%
isinstancer   r   r   r   	Exceptionr   r   feed_keyr
   r   filenor   call_on_sigwinchreceived_winchr   
add_readerr!   r%   r   call_inputhookdurationmaxr8   r   append_nowr   r.   r   getrandomshufflecall_from_executorflushinput_timeoutremove_reader)r&   stdin	callbacksctxr5   inputhook_timerr:   inputhook_durationremaining_timeoutfdstaskslow_priority_tasksnowfdcmax_postpone_untilhandlert_r2   r3   r4   s   `                  @@@r(   runPosixEventLoop.run2   s    %'''')%78888==  ;;899#!)"4"45(/ (7 "4#6#67C.C	 	 	/++A.5---**_d //>>uE	 "
 *9)A)A&)*& #1%-(,%(+Aq/ADV/V(W% --.?@  E)+&C! !4!4Q!779=9R9R 5#5#=$)LLO +.-C'9'?(-Q(:(A(A1BY(Z :S 9;D5 GGD$7$7$:DA&*nn&8&8&<G&w %W 5- "2 NN5)NN#56 !&AC "' 6H1A1 33AK]3^ 6H %7DAqC %7  %%' ++-)-OA&m --- r 	5!4..q12q "	 Ss,   .N=#N DNB?N
N	N
N#c                 <    U R                   R                  U5      nU$ )z9
Return the file descriptors that are ready for reading.
)r   select)r&   timeoutrV   s      r(   r8   !PosixEventLoop._ready_for_reading   s     mm""7+
r+   c                 4   ^  U 4S jnT R                  U5        g)z7
Notify the event loop that SIGWINCH has been received
c                  ^   > T R                   (       a  T R                   R                  5         g g N)r   terminal_size_changedr&   s   r(   process_winch4PosixEventLoop.received_winch.<locals>.process_winch   s    557 r+   NrL   )r&   rk   s   ` r(   rB   PosixEventLoop.received_winch   s    	8 	.r+   c                 4   ^ U4S jnU R                  U5        g)z
Run a long running function in a background thread.
(This is recommended for code that could block the event loop.)
Similar to Twisted's ``deferToThread``.
c                  J   > [         R                  " T S9R                  5         g )N)target)	threadingThreadstartcallbacks   r(   start_executor6PosixEventLoop.run_in_executor.<locals>.start_executor   s    H-335r+   Nrm   )r&   rv   rw   s    ` r(   run_in_executorPosixEventLoop.run_in_executor   s    	6/r+   c                    Ub  [        U[        5      (       d   eU R                  R                  X45        U R                  (       a&   [
        R                  " U R                  S   S5        gg! [        [        [        4 a     gf = f)aP  
Call this function in the main event loop.
Similar to Twisted's ``callFromThread``.

:param _max_postpone_until: `None` or `time.time` value. For interal
    use. If the eventloop is saturated, consider this task to be low
    priority and postpone maximum until this timestamp. (For instance,
    repaint is done using low priority.)
Nr      x)
r=   floatr   rG   r!   r   writeAttributeError
IndexErrorOSError)r&   rv   r<   s      r(   rL   !PosixEventLoop.call_from_executor   s}     #*j9Le.T.TTT!!((()HI,,Q/6  #J8 
 s   	$A/ /BBc                     SU l         g)z
Stop the event loop.
FN)r   rj   s    r(   r0   PosixEventLoop.stop   s     r+   c                    SU l         U R                  nS U l        U(       a2  [        R                  " US   5        [        R                  " US   5        U R                  (       a  U R                  R                  5         g g )NTr   r   )r   r!   r   closer%   )r&   schedule_pipes     r(   r   PosixEventLoop.close  sc     ++"HH]1%&HH]1%&""##))+ #r+   c                 l    [        U5      nX R                  U'   U R                  R                  U5        g)z,Add read file descriptor to the event loop. N)r   r   r   register)r&   rZ   rv   s      r(   rC   PosixEventLoop.add_reader  s)    r]%rr"r+   c                     [        U5      nXR                  ;   a  U R                  U	 U R                  R                  U5        g)z1Remove read file descriptor from the event loop. N)r   r   r   
unregister)r&   rZ   s     r(   rO   PosixEventLoop.remove_reader  s3    r]r"  $r+   )	r   r   r%   r   r   r!   r   r   r   rh   )__name__
__module____qualname____firstlineno____doc__r   r)   r`   r8   rB   ry   rL   r0   r   rC   rO   __static_attributes__ r+   r(   r   r      sC     "& U(IV/$0&.,#%r+   r   c                   *    \ rS rSrSrS rS rS rSrg)rA   i$  zj
Context manager which Installs a SIGWINCH callback.
(This signal occurs when the terminal size changes.)
c                     Xl         S U l        g rh   rv   previous_callback)r&   rv   s     r(   r)   call_on_sigwinch.__init__)  s     !%r+   c                 b   ^  [         R                   " [         R                  U 4S j5      T l        g )Nc                  $   > TR                  5       $ rh   ru   )ar&   s    r(   <lambda>,call_on_sigwinch.__enter__.<locals>.<lambda>.  s    4==?r+   )signalSIGWINCHr   rj   s   `r(   	__enter__call_on_sigwinch.__enter__-  s    !'v@Z![r+   c                     U R                   c&  [        R                  " [        R                  S5        g [        R                  " [        R                  U R                   5        g )Nr   )r   r   r   )r&   r   kws      r(   __exit__call_on_sigwinch.__exit__0  s:    !!) MM&//1-MM&//4+A+ABr+   r   N)	r   r   r   r   r   r)   r   r   r   r   r+   r(   rA   rA   $  s    &\Cr+   rA   )#
__future__r   r"   r   rJ   r   rr   time#prompt_toolkit.terminal.vt100_inputr   prompt_toolkit.utilsr   r   prompt_toolkit.inputr   baser	   r
   rQ   r   r'   r   posix_utilsr   utilsr   rc   r   r   r   __all__rH   r   objectrA   r   r+   r(   <module>r      sg    '  	     ; = & * ) ' )  5 5 yyG%Y G%TCv Cr+   