
    ;OOf'                     *    d dl mZmZ  G d de      Zy)   )	EventLoopExitMainLoopc                   p    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zy)TrioEventLoopzn
    Event loop based on the ``trio`` module.

    ``trio`` is an async library for Python 3.5 and later.
    c                     ddl }d| _        i | _        g | _        || _        d| _        |j                  | _        	 |j                  j                  | _
        y# t        $ r |j                  j                  | _
        Y yw xY w)zConstructor.    N)trio_idle_handle_idle_callbacks_pending_tasks_trio_nurserysleep_sleeplowlevelwait_readable_wait_readableAttributeErrorhazmat)selfr	   s     [/var/www/premiumrankchecker/venv/lib/python3.12/site-packages/urwid/_async_kw_event_loop.py__init__zTrioEventLoop.__init__!   sn    ! 
jj	<"&--"="=D 	<"&++";";D	<s   A $A=<A=c                 <    | j                  | j                  ||      S )aL  Calls `callback()` a given time from now.  No parameters are passed
        to the callback.

        Parameters:
            seconds: time in seconds to wait before calling the callback
            callback: function to call from the event loop

        Returns:
            a handle that may be passed to `remove_alarm()`
        )_start_task_alarm_task)r   secondscallbacks      r   alarmzTrioEventLoop.alarm3   s      0 0'8DD    c                 v    | xj                   dz  c_         || j                  | j                   <   | j                   S )zCalls `callback()` when the event loop enters the idle state.

        There is no such thing as being idle in a Trio event loop so we
        simulate it by repeatedly calling `callback()` with a short delay.
        r   )r
   r   )r   r   s     r   
enter_idlezTrioEventLoop.enter_idle@   s7     	Q2:T../   r   c                 $    | j                  |      S )zeRemoves an alarm.

        Parameters:
            handle: the handle of the alarm to remove
        _cancel_scoper   handles     r   remove_alarmzTrioEventLoop.remove_alarmJ   s     !!&))r   c                 >    	 | j                   |= y# t        $ r Y yw xY w)zuRemoves an idle callback.

        Parameters:
            handle: the handle of the idle callback to remove
        FT)r   KeyErrorr%   s     r   remove_enter_idlezTrioEventLoop.remove_enter_idleR   s.    	$$V,   		s    	c                 $    | j                  |      S )zRemoves a file descriptor being watched for input.

        Parameters:
            handle: the handle of the file descriptor callback to remove

        Returns:
            True if the file descriptor was watched, False otherwise
        r#   r%   s     r   remove_watch_filezTrioEventLoop.remove_watch_file^   s     !!&))r   c                 @    |j                    }|j                          |S )zCancels the given Trio cancellation scope.

        Returns:
            True if the scope was cancelled, False if it was cancelled already
            before invoking this function
        )cancel_calledcancel)r   scopeexisteds      r   r$   zTrioEventLoop._cancel_scopei   s      )))r   c                 Z   | j                    G fdd| j                  j                  j                        } |       }| j                  j                  j                  | j                        5  | j                  j                  | j                  |g       ddd       y# 1 sw Y   yxY w)zStarts the event loop. Exits the loop when any callback raises an
        exception. If ExitMainLoop is raised, exits cleanly.
        c                       e Zd Z fdZy)5TrioEventLoop.run.<locals>.TrioIdleCallbackInstrumentc                 J    |dkD  rj                         D ]	  } |         y y Nr   valuesr   timeoutidle_callbackidle_callbackss      r   before_io_waitzDTrioEventLoop.run.<locals>.TrioIdleCallbackInstrument.before_io_wait   ,    Q;)7)>)>)@ (%( r   N__name__
__module____qualname__r=   r<   s   r   TrioIdleCallbackInstrumentr4   ~       (r   rD   )instrumentsN)	r   r   abc
Instrument
MultiErrorcatch_handle_main_loop_exceptionrun
_main_task)r   rD   emulate_idle_callbacksr<   s      @r   rL   zTrioEventLoop.runt   s    
 --
	()B)B 	( "<!=ZZ""(()I)IJ 	RJJNN4??9O8PNQ	R 	R 	Rs   /)B!!B*c                   K   | j                    G fdd| j                  j                  j                        } |       }	 | j                  j                  j
                  }| j                  j                  j                  }| j                  j                  j                  | j                        5   ||       	 | j                          d{     ||       	 ddd       y# t        $ rC | j                  j                  j
                  }| j                  j                  j                  }Y w xY w7 e#  ||       w xY w# 1 sw Y   yxY ww)aA  Starts the main loop and blocks asynchronously until the main loop
        exits. This allows one to embed an urwid app in a Trio app even if the
        Trio event loop is already running. Example::

            with trio.open_nursery() as nursery:
                event_loop = urwid.TrioEventLoop()

                # [...launch other async tasks in the nursery...]

                loop = urwid.MainLoop(widget, event_loop=event_loop)
                with loop.start():
                    await event_loop.run_async()

                nursery.cancel_scope.cancel()
        c                       e Zd Z fdZy);TrioEventLoop.run_async.<locals>.TrioIdleCallbackInstrumentc                 J    |dkD  rj                         D ]	  } |         y y r6   r7   r9   s      r   r=   zJTrioEventLoop.run_async.<locals>.TrioIdleCallbackInstrument.before_io_wait   r>   r   Nr?   rC   s   r   rD   rQ      rE   r   rD   N)r   r   rG   rH   r   add_instrumentremove_instrumentr   r   rI   rJ   rK   rM   )r   rD   rN   rS   rT   r<   s        @r   	run_asynczTrioEventLoop.run_async   s    " --
	()B)B 	( "<!=	D!ZZ00??N $

 3 3 E E ZZ""(()I)IJ 	:12:oo'''!"89	: 	:  	D!ZZ..==N $

 1 1 C C	D (!"89	: 	:sm   ?EA C& /E2	E<D7D5D7E	E&A	D2/E1D22E5D77
EEE	Ec                 <    | j                  | j                  ||      S )ab  Calls `callback()` when the given file descriptor has some data
        to read. No parameters are passed to the callback.

        Parameters:
            fd: file descriptor to watch for input
            callback: function to call when some input is available

        Returns:
            a handle that may be passed to `remove_watch_file()`
        )r   _watch_task)r   fdr   s      r   
watch_filezTrioEventLoop.watch_file   s      0 0"h??r   c                 ~   K   |5  | j                  |       d{     |        ddd       y7 # 1 sw Y   yxY ww)a0  Asynchronous task that sleeps for a given number of seconds and then
        calls the given callback.

        Parameters:
            scope: the cancellation scope that can be used to cancel the task
            seconds: the number of seconds to wait
            callback: the callback to call
        N)r   )r   r0   r   r   s       r   r   zTrioEventLoop._alarm_task   s?       	++g&&&J	 	&	 	s    =1/1	=1:=c                 \    | j                   j                          t        |t              ry|S )a=  Handles exceptions raised from the main loop, catching ExitMainLoop
        instead of letting it propagate through.

        Note that since Trio may collect multiple exceptions from tasks into a
        Trio MultiError, we cannot simply use a try..catch clause, we need a
        helper function like this.
        N)r   clear
isinstancer   )r   excs     r   rK   z)TrioEventLoop._handle_main_loop_exception   s(     	""$c<(Jr   c                 >  K   	 | j                   j                         4 d{   | _        | j                          | j                   j	                          d{    ddd      d{    d| _        y7 S7 7 # 1 d{  7  sw Y    xY w# d| _        w xY ww)zzMain Trio task that opens a nursery and then sleeps until the user
        exits the app by raising ExitMainLoop.
        N)r   open_nurseryr   _schedule_pending_taskssleep_forever)r   s    r   rM   zTrioEventLoop._main_task   s     	!zz..0 1 1DM,,.jj..0001 1 !DM	101 1 1 1 !DMss   BB A6B 3A<A8A<B )A:*B .B6B 8A<:B <BBB
B 	BBc                     | j                   D ]$  \  }}} | j                  j                  ||g|  & | j                   dd= y)zSchedules all pending asynchronous tasks that were created before
        the nursery to be executed on the nursery soon.
        N)r   r   
start_soon)r   taskr0   argss       r   ra   z%TrioEventLoop._schedule_pending_tasks   sI     "&!4!4 	9D%$DMM$$T5848	9"r   c                     | j                   j                         }| j                  r  | j                  j                  ||g|  |S | j                  j                  |||f       |S )ap  Starts an asynchronous task in the Trio nursery managed by the
        main loop. If the nursery has not started yet, store a reference to
        the task and the arguments so we can start the task when the nursery
        is open.

        Parameters:
            task: a Trio task to run

        Returns:
            a cancellation scope for the Trio task
        )r   CancelScoper   rd   r   append)r   re   rf   r0   s       r   r   zTrioEventLoop._start_task   sa     

&&(==$DMM$$T5848  &&eT':;r   c                    K   |5  |j                   s-| j                  |       d{     |        |j                   s-ddd       y7 !# 1 sw Y   yxY ww)aP  Asynchronous task that watches the given file descriptor and calls
        the given callback whenever the file descriptor becomes readable.

        Parameters:
            scope: the cancellation scope that can be used to cancel the task
            fd: the file descriptor to watch
            callback: the callback to call
        N)r.   r   )r   r0   rX   r   s       r   rW   zTrioEventLoop._watch_task  sY       	 ))))"---
 ))		 	
 .	 	s+   A!A
AA
	AA

AAN)r@   rA   rB   __doc__r   r   r!   r'   r*   r,   r$   rL   rU   rY   r   rK   rM   ra   r   rW    r   r   r   r      s[    <$E!*
	*	R*+:Z@	!#&r   r   N)	main_loopr   r   r   rl   r   r   <module>rn      s   . /yI yr   