o
    b                     @   s   d Z ddlmZmZ zddlmZmZm	Z
 W n ey)   ddlmZmZm
Z
 Y nw ddlmZ ddlmZmZ G dd de
ZG d	d
 d
ejZdd ZdgZdS )aa  
This module provides wxPython event loop support for Twisted.

In order to use this support, simply do the following::

    |  from twisted.internet import wxreactor
    |  wxreactor.install()

Then, when your root wxApp has been created::

    | from twisted.internet import reactor
    | reactor.registerWxApp(yourApp)
    | reactor.run()

Then use twisted.internet APIs as usual. Stop the event loop using
reactor.stop(), not yourApp.ExitMainLoop().

IMPORTANT: tests will fail when run under this reactor. This is
expected and probably does not reflect on the reactor's ability to run
real applications.
    )EmptyQueue)	CallAfterPySimpleAppTimer)wxPySimpleAppwxCallAfterwxTimer)_threadedselect)logruntimec                   @   s    e Zd ZdZdd Zdd ZdS )ProcessEventsTimerz
    Timer that tells wx to process pending events.

    This is necessary on macOS, probably due to a bug in wx, if we want
    wxCallAfters to be handled when modal dialogs, menus, etc.  are open.
    c                 C   s   t |  || _d S N)r	   __init__wxappselfr    r   </usr/lib/python3/dist-packages/twisted/internet/wxreactor.pyr   2   s   

zProcessEventsTimer.__init__c                 C   s   | j   dS )z5
        Called repeatedly by wx event loop.
        N)r   ProcessPendingEventsr   r   r   r   Notify6   s   zProcessEventsTimer.NotifyN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   *   s    r   c                   @   sF   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dddZ
dS )	WxReactorz[
    wxPython reactor.

    wxPython drives the event loop, select() runs in a thread.
    Fc                 C   s
   || _ dS )z;
        Register wxApp instance with the reactor.
        N)r   r   r   r   r   registerWxAppF   s   
zWxReactor.registerWxAppc                 C   s>   zddl }| |j|j W n
 ty   Y dS w |   dS )zO
        wx sometimes removes our own signal handlers, so re-add them.
        r   N)signalSIGINTdefault_int_handlerImportError_handleSignals)r   r   r   r   r   _installSignalHandlersAgainL   s   z%WxReactor._installSignalHandlersAgainc                 C   s    | j rdS d| _ tj|  dS )z#
        Stop the reactor.
        NT)	_stoppingr
   ThreadedSelectReactorstopr   r   r   r   r&   Y   s   zWxReactor.stopc                 C   s&   t | drt| dS | j| dS )zm
        Schedule function to run in main wx/Twisted thread.

        Called by the select() thread.
        r   N)hasattrr   
_postQueueput)r   fr   r   r   _runInMainThreadb   s   
zWxReactor._runInMainThreadc                 C   s   t | dr| j  dS dS )z
        Stop the wx event loop if it hasn't already been stopped.

        Called during Twisted event loop shutdown.
        r   N)r'   r   ExitMainLoopr   r   r   r   _stopWxn   s   
zWxReactor._stopWxTc                    s  t   _t dstd  t   j j|d |r% 	d j
  dd j  dd fdd tj rGt j}|d	  j   j} ` js   |  	 z	 jjdd}W n	 tyo   Y q\w |du rvdS z|  W n ty   t  Y nw q]dS )z$
        Start the reactor.
        r   zMregisterWxApp() was not called on reactor, registering my own wxApp instance.)installSignalHandlersr   aftershutdownc                      s    j d S r   )r(   r)   r   r   r   r   <lambda>   s    zWxReactor.run.<locals>.<lambda>      g{Gz?)timeoutN)r   r(   r'   r   msgr   r   
interleaver+   	callLaterr#   addSystemEventTriggerr-   r   platformisMacOSXr   r   StartMainLoopr$   r&   r   getr   BaseExceptionerr)r   r.   tr   r*   r   r   r   runw   sN   





zWxReactor.runN)T)r   r   r   r   r$   r   r#   r&   r+   r-   rA   r   r   r   r   r   =   s    		r   c                  C   s   t  } ddlm} ||  | S )zP
    Configure the twisted mainloop to be run inside the wxPython mainloop.
    r   )installReactor)r   twisted.internet.mainrB   )reactorrB   r   r   r   install   s   rE   N)r   queuer   r   wxr   r   r   r   r   r	   r!   wxPython.wxtwisted.internetr
   twisted.pythonr   r   r   r%   r   rE   __all__r   r   r   r   <module>   s   t
