
    jjn                       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	 ddl
mZ ddlmZmZ ej                  r2ddlmZmZ dd	lmZmZ dd
lmZmZ  ej2                  d      Z ed      ZdZ G d de      Zy)z,Select based urwid EventLoop implementation.    )annotationsN)suppress)count   )	EventLoopExitMainLoop)CallableIterator)ExecutorFuture)Literal	ParamSpec_T_Spec)SelectEventLoopc                       e Zd ZdZd fdZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 ddZddZddZddZ	ddZ
dd	Zdd
ZddZddZ xZS )r   zF
    Event loop based on :func:`selectors.DefaultSelector.select`
    c                   t         |           t        j                  t              j                  | j                  j                        | _        g | _        i | _	        d| _
        i | _        t               | _        d| _        y )Nr   F)super__init__logging	getLogger__name__getChild	__class__logger_alarms_watch_files_idle_handle_idle_callbacksr   
_tie_break_did_something)selfr   s    e/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/event_loop/select_loop.pyr   zSelectEventLoop.__init__6   se    ''1::4>>;R;RSJLAC!"DF).$)    c                .     |j                   |g|i |S )a  Run callable in executor.

        :param executor: Executor to use for running the function
        :type executor: concurrent.futures.Executor
        :param func: function to call
        :type func: Callable
        :param args: positional arguments to function
        :type args: object
        :param kwargs: keyword arguments to function
        :type kwargs: object
        :return: future object for the function call outcome.
        :rtype: concurrent.futures.Future
        )submit)r"   executorfuncargskwargss        r#   run_in_executorzSelectEventLoop.run_in_executor@   s    ( xt5d5f55r$   c                    t        j                          |z   }|t        | j                        |f}t        j                  | j
                  |       |S )a#  
        Call callback() a given time from now.  No parameters are
        passed to callback.

        Returns a handle that may be passed to remove_alarm()

        seconds -- floating point time to wait before calling callback
        callback -- function to call from event loop
        )timenextr    heapqheappushr   )r"   secondscallbacktmhandles        r#   alarmzSelectEventLoop.alarmV   s@     YY[7"d4??+X6t||V,r$   c                    	 | j                   j                  |       t        j                  | j                          y# t        $ r Y yw xY w)z]
        Remove an alarm.

        Returns True if the alarm exists, False otherwise
        FT)r   remover/   heapify
ValueErrorr"   r4   s     r#   remove_alarmzSelectEventLoop.remove_alarmi   sB    	LL'MM$,,'
   		s   := 	A	A	c                $    || j                   |<   |S )a"  
        Call callback() when fd has some data to read.  No parameters
        are passed to callback.

        Returns a handle that may be passed to remove_watch_file()

        fd -- file descriptor to watch for input
        callback -- function to call when input is available
        r   )r"   fdr2   s      r#   
watch_filezSelectEventLoop.watch_filex   s     !)"	r$   c                <    || j                   v r| j                   |= yy)zg
        Remove an input file.

        Returns True if the input file exists, False otherwise
        TFr=   r:   s     r#   remove_watch_filez!SelectEventLoop.remove_watch_file   s&     T&&&!!&)r$   c                v    | xj                   dz  c_         || j                  | j                   <   | j                   S )zq
        Add a callback for entering idle.

        Returns a handle that may be passed to remove_idle()
        r   )r   r   r"   r2   s     r#   
enter_idlezSelectEventLoop.enter_idle   s7     	Q2:T../   r$   c                >    	 | j                   |= y# t        $ r Y yw xY w)z[
        Remove an idle callback.

        Returns True if the handle was removed.
        FT)r   KeyErrorr:   s     r#   remove_enter_idlez!SelectEventLoop.remove_enter_idle   s.    	$$V,   		s    	c                P    | j                   j                         D ]	  } |         y)z9
        Call all the registered idle callbacks.
        N)r   valuesrC   s     r#   _entering_idlezSelectEventLoop._entering_idle   s'     ,,335 	HJ	r$   c                    t        j                  t              5  d| _        	 t        t              5  | j                          ddd       )# 1 sw Y   
xY w# 1 sw Y   yxY w)z
        Start the event loop.  Exit the loop when any callback raises
        an exception.  If ExitMainLoop is raised, exit cleanly.
        TN)
contextlibr   r   r!   InterruptedError_loop)r"   s    r#   runzSelectEventLoop.run   sY    
   . 	!"&D./ !JJL! ! !	! 	!s"   AA	AA	AA!c                   d}t        j                         5 }| j                  j                         D ]&  \  }}|j	                  |t         j
                  |       ( | j                  s| j                  rd}| j                  r5| j                  d   d   }|}t        ||t        j                         z
        }| j                  r!| j                  r| j                  r	|dkD  rd}d}| j                  j                  d|       |j                  |      D cg c]  \  }}|	 }	}}nL| j                  r>| j                  j                  d       |j                         D cg c]  \  }}|	 }	}}ng }	ddd       	s|dk(  r3| j                  j                  d       | j                          d| _        nQ|Ot        j                  | j                        \  }}
}| j                  j                  d	|        |        d
| _        | j                  j                  d       |	D ]  }|j!                          d
| _         yc c}}w c c}}w # 1 sw Y   xY w)z6
        A single iteration of the event loop
        Ng        r   idlezWaiting for input: timeout=zWaiting for input: timeoutzNo input, entering IDLEFz'No input in timeout, calling scheduled TzProcessing input)	selectorsDefaultSelectorr   itemsregister
EVENT_READr   r!   maxr-   r   debugselectrJ   r/   heappopdata)r"   r3   selectorr>   r2   timeouttimeout_event_readyr    alarm_callbackrecords                r#   rN   zSelectEventLoop._loop   s    .2&&( 	H $ 1 1 7 7 9 FH!!"i&:&:HEF ||t22<<#||Aq1H!B!'8diik+ABG&&RY\]R]!GB!!$?{"KL/7w/GH85!HH""!!">?/7/@A85!AA/	2 V|!!";<##%&+#16t||1L.J!!$KNK]"^_ &*#,- 	'FKKM"&D	'+ I B+	 	s*   C>I
H>!>I
I+I
>I

I)returnNone)
r'   r   r(   zCallable[_Spec, _T]r)   z
_Spec.argsr*   z_Spec.kwargsrd   z
Future[_T])r1   floatr2   Callable[[], typing.Any]rd   +tuple[float, int, Callable[[], typing.Any]])r4   rh   rd   bool)r>   intr2   rg   rd   rj   )r4   rj   rd   ri   )r2   rg   rd   rj   )r   
__module____qualname____doc__r   r+   r5   r;   r?   rA   rD   rG   rJ   rO   rN   __classcell__)r   s   @r#   r   r   1   s    *66 "6 	6
 6 
6, + 
5	&	!
	!.'r$   r   )rm   
__future__r   rL   r/   r   rR   r-   typingr   	itertoolsr   abstract_loopr   r   TYPE_CHECKINGcollections.abcr	   r
   concurrent.futuresr   r   typing_extensionsr   r   TypeVarr   r   __all__r    r$   r#   <module>rz      sg   , 3 "         2	234		BgE
u'i u'r$   