U
    s@g                     @   s   d Z zddlmZmZ W n$ ek
r<   ddlmZmZ Y nX zddlmZmZ	m
Z W n( ek
r~   ddlmZm	Z	mZ Y nX ddlmZmZ ddl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)App	CallAfterTimer)wxAppwxCallAfterwxTimer)logruntime)_threadedselectc                   @   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   1   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   registerWxAppG   s    zWxReactor.registerWxAppc                 C   s@   zddl }| |j|j W n tk
r2   Y dS X |   dS )zO
        wx sometimes removes our own signal handlers, so re-add them.
        r   N)signalSIGINTdefault_int_handlerImportErrorZ_handleSignals)r   r   r   r   r   _installSignalHandlersAgainN   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%   [   s    zWxReactor.stopc                 C   s$   t | drt| n| 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   _runInMainThreade   s    

zWxReactor._runInMainThreadc                 C   s   t | dr| j  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   ZExitMainLoopr   r   r   r   _stopWxr   s    
zWxReactor._stopWxTc                    s  t   _t ds*td  td  j j|d |rL 	d j
  dd j  dd fdd	 tj rt j}|d
  j   j} ` js   |  z jjdd}W n tk
r   Y qY qX |dkrqz
|  W q   t  Y qX qdS )z$
        Start the reactor.
        r   zMregisterWxApp() was not called on reactor, registering my own wxApp instance.F)installSignalHandlersr   ZafterZshutdownc                      s    j d S r   )r'   r(   r   r   r   r   <lambda>       zWxReactor.run.<locals>.<lambda>   g{Gz?)ZtimeoutN)r   r'   r&   r
   msgr   r   Z
interleaver*   Z	callLaterr"   ZaddSystemEventTriggerr+   r   platformZisMacOSXr   r   ZStartZMainLoopr#   r%   r   getr   err)r   r,   tr   r)   r   r   r   run|   s@    








zWxReactor.runN)T)r   r   r   r   r#   r   r"   r%   r*   r+   r5   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   Ztwisted.internet.mainr6   )Zreactorr6   r   r   r   install   s    r7   N)r   Zqueuer   r   r!   Zwxr   r   r   r   r   r	   ZwxPython.wxZtwisted.pythonr
   r   Ztwisted.internetr   r   r$   r   r7   __all__r   r   r   r   <module>   s   t
