
    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Zddl	Z	ddl
mZ ddlZddlmZmZ e	j                   rDddlZddlmZ ddlmZmZ dd	lmZ  e	j2                  d
      Z e	j2                  d      Z ed      Z G d de      Zy)zbZeroMQ based urwid EventLoop implementation.

`ZeroMQ <https://zeromq.org>`_ library is required.
    )annotationsN)count   )	EventLoopExitMainLoop)Callable)ExecutorFuture)	ParamSpecZMQAlarmHandle_T_Specc                       e Zd ZdZ e       Zd fdZ	 	 	 	 	 	 	 	 	 	 ddZddZddZ	e
j                  f	 	 	 	 	 	 	 ddZe
j                  f	 	 	 	 	 	 	 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 )ZMQEventLoopac  
    This class is an urwid event loop for `ZeroMQ`_ applications. It is very
    similar to :class:`SelectEventLoop`, supporting the usual :meth:`alarm`
    events and file watching (:meth:`watch_file`) capabilities, but also
    incorporates the ability to watch zmq queues for events
    (:meth:`watch_queue`).

    .. _ZeroMQ: https://zeromq.org/
    c                   t         |           t        j                  t              j                  | j                  j                        | _        d| _        g | _	        t        j                         | _        i | _        d| _        i | _        y )NTr   )super__init__logging	getLogger__name__getChild	__class__logger_did_something_alarmszmqPoller_poller_queue_callbacks_idle_handle_idle_callbacks)selfr   s    b/home/ubuntu/.cache/pipx/cdb5059130eadca/lib/python3.12/site-packages/urwid/event_loop/zmq_loop.pyr   zZMQEventLoop.__init__A   si    ''1::4>>;R;RS"JLzz|EG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ZMQEventLoop.run_in_executorK   s    ( xt5d5f55r$   c                    t        j                          |z   t        | j                        |f}t        j                  | j
                  |       |S )aK  
        Call *callback* a given time from now. No parameters are passed to
        callback. Returns a handle that may be passed to :meth:`remove_alarm`.

        :param float seconds:
            floating point time to wait before calling callback.

        :param callback:
            function to call from event loop.
        )timenext_alarm_breakheapqheappushr   )r"   secondscallbackhandles       r#   alarmzZMQEventLoop.alarma   s=     ))+'d.?.?)@(Kt||V,r$   c                    	 | j                   j                  |       t        j                  | j                          y# t        $ r Y yw xY w)ze
        Remove an alarm. Returns ``True`` if the alarm exists, ``False``
        otherwise.
        FT)r   remover0   heapify
ValueErrorr"   r4   s     r#   remove_alarmzZMQEventLoop.remove_alarmp   sB    
	LL'MM$,,'
   		s   := 	A	A	c                    || j                   v rt        d|      | j                  j                  ||       || j                   |<   |S )a6  
        Call *callback* when zmq *queue* has something to read (when *flags* is
        set to ``POLLIN``, the default) or is available to write (when *flags*
        is set to ``POLLOUT``). No parameters are passed to the callback.
        Returns a handle that may be passed to :meth:`remove_watch_queue`.

        :param queue:
            The zmq queue to poll.

        :param callback:
            The function to call when the poll is successful.

        :param int flags:
            The condition to monitor on the queue (defaults to ``POLLIN``).
        zalready watching )r   r9   r   register)r"   queuer3   flagss       r#   watch_queuezZMQEventLoop.watch_queue~   sN    * D)))0	:;;eU+'/e$r$   c                    t        |t              rt        j                  |      }| j                  j                  ||       || j                  |j                         <   |S )a  
        Call *callback* when *fd* has some data to read. No parameters are
        passed to the callback. The *flags* are as for :meth:`watch_queue`.
        Returns a handle that may be passed to :meth:`remove_watch_file`.

        :param fd:
            The file-like object, or fileno to monitor.

        :param callback:
            The function to call when the file has data available.

        :param int flags:
            The condition to monitor on the file (defaults to ``POLLIN``).
        )
isinstanceintosfdopenr   r=   r   fileno)r"   fdr3   r?   s       r#   
watch_filezZMQEventLoop.watch_file   sJ    ( b#2Bb%(-5biik*	r$   c                    	 	 | j                   j                  |       | j                  j                  |d       y# | j                  j                  |d       w xY w# t        $ r Y yw xY w)z
        Remove a queue from background polling. Returns ``True`` if the queue
        was being monitored, ``False`` otherwise.
        NFT)r   
unregisterr   popKeyErrorr:   s     r#   remove_watch_queuezZMQEventLoop.remove_watch_queue   sd    
	8''/%%))&$7
  %%))&$7 		s    ; A AA 	A('A(c                   	 	 | j                   j                  |       | j                  j                  |j	                         d       y# | j                  j                  |j	                         d       w xY w# t
        $ r Y yw xY w)z
        Remove a file from background polling. Returns ``True`` if the file was
        being monitored, ``False`` otherwise.
        NFT)r   rJ   r   rK   rF   rL   r:   s     r#   remove_watch_filezZMQEventLoop.remove_watch_file   sq    
	A''/%%))&--/4@
  %%))&--/4@ 		s"   A	 *A8 	,A55A8 8	BBc                v    | xj                   dz  c_         || j                  | j                   <   | j                   S )z
        Add a *callback* to be executed when the event loop detects it is idle.
        Returns a handle that may be passed to :meth:`remove_enter_idle`.
        r   )r    r!   r"   r3   s     r#   
enter_idlezZMQEventLoop.enter_idle   s7    
 	Q2:T../   r$   c                >    	 | j                   |= y# t        $ r Y yw xY w)zq
        Remove an idle callback. Returns ``True`` if *handle* was removed,
        ``False`` otherwise.
        FT)r!   rL   r:   s     r#   remove_enter_idlezZMQEventLoop.remove_enter_idle   s.    
	$$V,   		s    	c                b    t        | j                  j                               D ]	  } |         y )N)listr!   valuesrQ   s     r#   _entering_idlezZMQEventLoop._entering_idle   s*    T1188:; 	HJ	r$   c                   t        j                  t              5  	 	 | j                          # t        j
                  j                  $ r(}|j                  t        j                  k7  r Y d}~Ad}~ww 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 :exc:`ExitMainLoop` is raised, exit cleanly.
        N)	
contextlibsuppressr   _loopr   errorZMQErrorerrnoEINTR)r"   excs     r#   runzZMQEventLoop.run   sl    
   . 	JJL  yy)) yyEKK/ 0		 	s1   A6.A6A3A.)A6.A33A66A?c                   d}| j                   s| j                  rd}| j                   r3d}t        d| j                   d   d   t        j                         z
        }| j                  r!| j                   r| j                   r	|dkD  rd}d}t	        | j
                  j                  |dz              }n#t	        | j
                  j                               }|sS|dk(  r| j                          d| _        n6|dk(  r1t        j                  | j                         \  }}} |        d| _        |D ]  } | j                  |           d| _         y	)
z7
        A single iteration of the event loop.
        waitr   r5   g        idlei  FTN)r   r   maxr-   dictr   pollrX   r0   heappopr   )r"   statetimeoutready_due
_tie_breakr3   r>   s           r#   r\   zZMQEventLoop._loop   s    <<4..G||c4<<?1#5		#CD""DLLT\\gXYk**7T>:;E**,-E##%&+#'!-2]]4<<-H*j(
&*# 	'E(D!!%(*"&D	'r$   )returnNone)
r'   r	   r(   zCallable[_Spec, _T]r)   z
_Spec.argsr*   z_Spec.kwargsro   z
Future[_T])r2   floatr3   Callable[[], typing.Any]ro   r   )r4   r   ro   bool)r>   
zmq.Socketr3   rr   r?   rC   ro   rt   )rG   zint | io.TextIOWrapperr3   rr   r?   rC   ro   io.TextIOWrapper)r4   rt   ro   rs   )r4   ru   ro   rs   )r3   rr   ro   rC   )r4   rC   ro   rs   )r   
__module____qualname____doc__r   r/   r   r+   r5   r;   r   POLLINr@   rH   rM   rO   rR   rT   rX   rb   r\   __classcell__)r   s   @r#   r   r   4   s     7LG66 "6 	6
 6 
6,$ ZZ	 + 	
 
> ZZ	" + 	
 
4  !
'r$   r   )rx   
__future__r   rZ   r_   r0   r   rD   r-   typing	itertoolsr   r   abstract_loopr   r   TYPE_CHECKINGiocollections.abcr   concurrent.futuresr	   r
   typing_extensionsr   TypeVarr   r   r   r    r$   r#   <module>r      s{   (
 #     	    
 2	(3+#V^^$45N		BgEa'9 a'r$   