
    jj$                        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mZ ddl	m
Z
mZ ej                  r6ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ  ed      Z ej0                  d      ZdZdddZ G d de
      Zy)zKGLib based urwid EventLoop implementation.

PyGObject library is required.
    )annotationsN)GLib   )	EventLoopExitMainLoop)Callable)ExecutorFuture)	FrameType)Literal	ParamSpec_Spec_T)GLibEventLoopc                     y N )_sig_frames     c/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/event_loop/glib_loop.py_ignore_handlerr   3   s        c                       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 ZddZddZddZ xZS )r   z+
    Event loop based on GLib.MainLoop
    c                X   t         |           t        j                  t              j                  | j                  j                        | _        g | _        i | _	        d| _
        d| _        i | _        t        j                         | _        d | _        | j#                          i | _        y )Nr   F)super__init__logging	getLogger__name__getChild	__class__logger_alarms_watch_files_idle_handle_glib_idle_enabled_idle_callbacksr   MainLoop_loop_exc_enable_glib_idle_signal_handlers)selfr!   s    r   r   zGLibEventLoop.__init__<   s    ''1::4>>;R;RS"$,.!""'DF]]_
*.	 02r   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GLibEventLoop.run_in_executorI   s    ( xt5d5f55r   c                      j                   d fd       }t        j                  t        |dz        |      } j                  j                  |       |f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
        c                 4              j                          yNFr+   )callbackr-   s   r   	ret_falsez&GLibEventLoop.alarm.<locals>.ret_falsen   s    J""$r   i  )returnzLiteral[False])handle_exitr   timeout_addintr#   append)r-   secondsr9   r:   fds   ` `  r   alarmzGLibEventLoop.alarm_   sV     
			 
	
 c'D.19=BH~r   c                   t         j                  t         j                  t         j                  t         j                  t         j
                  g}t        j                  ddd      s|j                  t         j                         ||vry|| j                  v r.t        j                  | j                  j                  |             t         j                  j                  k(  rt        nt         j                  j                   k(  rydfd}t        j"                  t        j$                  |||      }|| j                  |<   y)a  
        Sets the signal handler for signal signum.

        .. WARNING::
            Because this method uses the `GLib`-specific `unix_signal_add`
            function, its behaviour is different than `signal.signal().`

            If `signum` is not `SIGHUP`, `SIGINT`, `SIGTERM`, `SIGUSR1`,
            `SIGUSR2` or `SIGWINCH`, this method performs no actions and
            immediately returns None.

            Returns None in all cases (unlike :func:`signal.signal()`).
        ..

        signum -- signal number
        handler -- function (taking signum as its single argument),
        or `signal.SIG_IGN`, or `signal.SIG_DFL`
           6   r   Nc                6     | d        t         j                  S r   )r   SOURCE_CONTINUE)signal_numberhandlers    r   final_handlerz7GLibEventLoop.set_signal_handler.<locals>.final_handler   s    M4('''r   )rH   r>   )signalSIGHUPSIGINTSIGTERMSIGUSR1SIGUSR2r   check_versionr?   SIGWINCHr,   source_removepopHandlersSIG_IGNr   SIG_DFLunix_signal_addPRIORITY_DEFAULT)r-   signumrI   glib_signalsrJ   sources     `   r   set_signal_handlerz GLibEventLoop.set_signal_handlerx   s    0 MMMMNNNNNN
 !!!R+0%T***t4488@Afoo---%G///	(
 %%d&;&;V]TZ[(.f%r   c                    	 | j                   j                  |d          t        j                  |d          y# t        $ r Y yw xY w)z]
        Remove an alarm.

        Returns True if the alarm exists, False otherwise
        r   FT)r#   remover   rS   
ValueErrorr-   handles     r   remove_alarmzGLibEventLoop.remove_alarm   sH    	LLq	*vay)
   		s   69 	AAc                      j                   d fd       }t        j                  |t        j                  |       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
        c                4             j                          yNTr8   )r\   cb_conditionr9   r-   s     r   io_callbackz-GLibEventLoop.watch_file.<locals>.io_callback   s    J""$r   )r;   zLiteral[True])r<   r   io_add_watchIO_INr$   )r-   rA   r9   rh   s   ` ` r   
watch_filezGLibEventLoop.watch_file   sG     
			 
	
 !% 1 1"djj+ N"	r   c                    || j                   v r0t        j                  | j                   |          | j                   |= yy)zg
        Remove an input file.

        Returns True if the input file exists, False otherwise
        TF)r$   r   rS   ra   s     r   remove_watch_filezGLibEventLoop.remove_watch_file   s?     T&&&t0089!!&)r   c                v    | xj                   dz  c_         || j                  | j                   <   | j                   S )zw
        Add a callback for entering idle.

        Returns a handle that may be passed to remove_enter_idle()
        r   )r%   r'   r-   r9   s     r   
enter_idlezGLibEventLoop.enter_idle   s7     	Q2:T../   r   c                j    | j                   ry t        j                  | j                         d| _         y rf   )r&   r   idle_add_glib_idle_callback)r-   s    r   r+   zGLibEventLoop._enable_glib_idle   s(    ""d../"&r   c                ^    | j                   j                         D ]	  } |         d| _        yr7   )r'   valuesr&   ro   s     r   rs   z!GLibEventLoop._glib_idle_callback   s0    ,,335 	HJ	"'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'   KeyErrorra   s     r   remove_enter_idlezGLibEventLoop.remove_enter_idle   s.    	$$V,   		s    	c                   	 | j                   j                          | j                   j                         r| j                   j                          	 | j                  r.| j                  }d| _        |j                  |j                        y# | j                   j                         r| j                   j                          w w xY w)z
        Start the event loop.  Exit the loop when any callback raises
        an exception.  If ExitMainLoop is raised, exit cleanly.
        N)r)   run
is_runningquitr*   with_traceback__traceback__)r-   excs     r   rz   zGLibEventLoop.run   s    
	"JJNNzz$$&

!99))CDI$$S%6%677	  zz$$&

! 's   B 7Cc                H     t        j                        d fd       }|S )a,  
        Decorator that cleanly exits the :class:`GLibEventLoop` if
        :exc:`ExitMainLoop` is thrown inside of the wrapped function. Store the
        exception info if some other exception occurs, it will be reraised after
        the loop quits.

        *f* -- function to be wrapped
        c                     	  | i |S # t         $ r j                  j                          Y yt        $ rE}|_        j                  j                         rj                  j                          Y d }~yd }~ww xY wr7   )r   r)   r|   BaseExceptionr*   r{   )r2   r3   r   fr-   s      r   wrapperz*GLibEventLoop.handle_exit.<locals>.wrapper  sn    &$)&)) "

!
 	 ! &	::((*JJOO%	&s    #A=A=;A88A=)r2   
_Spec.argsr3   _Spec.kwargsr;   z_T | Literal[False])	functoolswraps)r-   r   r   s   `` r   r<   zGLibEventLoop.handle_exit  s'     
			 
		 r   )r;   None)
r0   r	   r1   Callable[_Spec, _T]r2   r   r3   r   r;   z
Future[_T])r@   floatr9   Callable[[], typing.Any]r;   z$tuple[int, Callable[[], typing.Any]])rZ   r>   rI   zECallable[[int, FrameType | None], typing.Any] | int | signal.Handlersr;   r   )r;   bool)rA   r>   r9   r   r;   r>   )rb   r>   r;   r   )r9   r   r;   r>   )r   r   r;   z$Callable[_Spec, _T | Literal[False]])r   
__module____qualname____doc__r   r4   rB   r]   rc   rk   rm   rp   r+   rs   rx   rz   r<   __classcell__)r!   s   @r   r   r   7   s    366 "6 	6
 6 
6, + 
.	25/5/ W5/ 
	5/n(
!'
8 r   r   r   )r   r>   r   zFrameType | Noner;   r   )r   
__future__r   r   r   rK   typinggi.repositoryr   abstract_loopr   r   TYPE_CHECKINGcollections.abcr   concurrent.futuresr	   r
   typesr   typing_extensionsr   r   r   TypeVarr   __all__r   r   r   r   r   <module>r      sh   ,
 #      2	(34gE		B
oI or   