
    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Zddlm	Z	m
Z
 ej                  r,ddlmZ ddlmZ ddlmZ  ed	      Z ej&                  d
      ZdZ G d de	      Zy)z-Asyncio based urwid EventLoop implementation.    )annotationsN   )	EventLoopExitMainLoop)Callable)Executor)	ParamSpec_Spec_T)AsyncioEventLoopc                       e Zd ZdZdd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ddZddZ xZS )r   a  
    Event loop based on the standard library ``asyncio`` module.

    .. warning::
        Under Windows, AsyncioEventLoop globally enforces WindowsSelectorEventLoopPolicy
        as a side-effect of creating a class instance.
        Original event loop policy is restored in destructor method.

    .. note::
        If you make any changes to the urwid state outside of it
        handling input or responding to alarms (for example, from asyncio.Task
        running in background), and wish the screen to be
        redrawn, you must call :meth:`MainLoop.draw_screen` method of the
        main loop manually.

        A good way to do this:
            asyncio.get_event_loop().call_soon(main_loop.draw_screen)
    N)loopc                  t         |           t        j                  t              j                  | j                  j                        | _        t        j                  d d dk  rd| _
        d | _        |r|| _        nt        j                         | _        t        j                  dk(  rnt!        | j                  t        j"                        sJ| j                  j%                  d       t        j&                  t        j"                                d| _
        nd| _
        t        j(                         | _        n+d | _        |r|| _        n	 t        j,                         | _        d | _        d | _        d| _        i | _        y # t.        $ rK t        j0                  t        j2                        | _        | j*                  j5                         | _        Y pw xY w)	N         Fwin32z?Set WindowsSelectorEventLoopPolicy as asyncio event loop policyT)loop_factoryr   )super__init__logging	getLogger__name__getChild	__class__loggersysversion_info_event_loop_policy_altered_original_event_loop_policy_loopasyncioget_event_loop_policyplatform
isinstanceWindowsSelectorEventLoopPolicydebugset_event_loop_policyget_event_loop_runnerget_running_loopRuntimeErrorRunnerSelectorEventLoopget_loop_exc_idle_asyncio_handle_idle_handle_idle_callbacks)selfr   kwargsr   s      f/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/event_loop/asyncio_loop.pyr   zAsyncioEventLoop.__init__B   sn   ''1::4>>;R;RSBQ')49D+W[D,8<
3:3P3P3R0<<7*:44g6\6\4 KK%%&gh11>>@ 7;D36;D3$335
 37DL8<
9!(!9!9!;DJ
 +/	@D!!"DF $ 9#*>>w?X?X#YDL!%!6!6!8DJ9s   F AGGc                    t         j                  d d dk  r-| j                  r t        j                  | j
                         y y | j                  | j                  j                          y y )Nr   r   )r   r   r    r#   r)   r!   r+   close)r5   s    r7   __del__zAsyncioEventLoop.__del__m   sX    BQ')..--d.N.NO /\\%LL  &    c                H     t        j                        d fd       }|S )z@
        Wrap the callback to also call _entering_idle.
        c                     j                   s+j                  j                  dj                        _          | i |S )Nr   )r2   r"   
call_later_entering_idle)argsr6   callbackr5   s     r7   wrapperz1AsyncioEventLoop._also_call_idle.<locals>.wrappery   s;    ,,,0JJ,A,A!TEXEX,Y)T,V,,r;   )r@   
_Spec.argsr6   _Spec.kwargsreturnr   )	functoolswraps)r5   rA   rB   s   `` r7   _also_call_idlez AsyncioEventLoop._also_call_idlet   s'    
 
	"	- 
#	-
 r;   c                z    	 | j                   j                         D ]	  } |         	 d| _        y# d| _        w xY w)z9
        Call all the registered idle callbacks.
        N)r4   valuesr2   r5   rA   s     r7   r?   zAsyncioEventLoop._entering_idle   s=    	- 00779 
 )-D%D%s   &1 	:c                h    | j                   j                  |t        j                  |g|i |      S )a$  Run callable in executor.

        :param executor: Executor to use for running the function. Default asyncio executor is used if None.
        :type executor: concurrent.futures.Executor | None
        :param func: function to call
        :type func: Callable
        :param args: arguments to function (positional only)
        :type args: object
        :param kwargs: keyword arguments to function (keyword only)
        :type kwargs: object
        :return: future object for the function call outcome.
        :rtype: asyncio.Future
        )r"   run_in_executorrF   partial)r5   executorfuncr@   r6   s        r7   rM   z AsyncioEventLoop.run_in_executor   s1    ( zz))(I4E4Ed4\T4\U[4\]]r;   c                X    | j                   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 -- time in seconds to wait before calling callback
        callback -- function to call from event loop
        )r"   r>   rH   )r5   secondsrA   s      r7   alarmzAsyncioEventLoop.alarm   s&     zz$$Wd.B.B8.LMMr;   c                H    |j                          }|j                          |S )z]
        Remove an alarm.

        Returns True if the alarm exists, False otherwise
        )	cancelledcancel)r5   handleexisteds      r7   remove_alarmzAsyncioEventLoop.remove_alarm   s#     &&((r;   c                \    | j                   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
        )r"   
add_readerrH   )r5   fdrA   s      r7   
watch_filezAsyncioEventLoop.watch_file   s(     	

b$"6"6x"@A	r;   c                8    | j                   j                  |      S )zg
        Remove an input file.

        Returns True if the input file exists, False otherwise
        )r"   remove_readerr5   rW   s     r7   remove_watch_filez"AsyncioEventLoop.remove_watch_file   s     zz''//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   )r3   r4   rK   s     r7   
enter_idlezAsyncioEventLoop.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)r4   KeyErrorr`   s     r7   remove_enter_idlez"AsyncioEventLoop.remove_enter_idle   s.    	$$V,   		s    	c                    |j                  d      x}rV|j                          | j                  r!| j                  j                          d | _        t	        |t
              s|| _        y y |j                  |       y )N	exception)getstopr2   rV   r&   r   r1   default_exception_handler)r5   r   contextexcs       r7   _exception_handlerz#AsyncioEventLoop._exception_handler   sj    ++k**3*IIK(( ))002,0)c<0	 1 **73r;   c                    | j                   j                  | j                         | j                   j                          | j                  r.| j                  }d| _        |j                  |j                        y)zStart the event loop.

        Exit the loop when any callback raises an exception.
        If ExitMainLoop is raised, exit cleanly.
        N)r"   set_exception_handlerrn   run_foreverr1   with_traceback__traceback__)r5   rm   s     r7   runzAsyncioEventLoop.run   s_     	

(()@)@A

 99))CDI$$S%6%677 r;   )r   z asyncio.AbstractEventLoop | NonerE   None)rE   ru   )rA   Callable[_Spec, _T]rE   rv   )
rO   zExecutor | NonerP   rv   r@   rC   r6   rD   rE   zasyncio.Future[_T])rR   floatrA   Callable[[], typing.Any]rE   zasyncio.TimerHandle)rE   bool)r\   intrA   rx   rE   rz   )rW   rz   rE   ry   )rA   rx   rE   rz   )r   zasyncio.AbstractEventLoop)r   
__module____qualname____doc__r   r:   rH   r?   rM   rS   rY   r]   ra   rc   rf   rn   rt   __classcell__)r   s   @r7   r   r   .   s    & DH )GV!-^!^ "^ 	^
 ^ 
^,
N0
!
4 8r;   r   )r}   
__future__r   r#   rF   r   r   typingabstract_loopr   r   TYPE_CHECKINGcollections.abcr   concurrent.futuresr   typing_extensionsr	   r
   TypeVarr   __all__r    r;   r7   <module>r      s^   , 4 "    
  2	(++gE		B
Q8y Q8r;   