U
    ˆj•  ã                   @   s¦   d Z ddlmZmZ zddlmZmZm	Z
 W n( ek
rT   ddlmZmZm
Z
 Y nX 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   ú>/tmp/pip-unpacked-wheel-j5h_c8_q/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   sB   zddl }|  |j|j¡ W n tk
r2   Y dS X | j ¡  dS )zO
        wx sometimes removes our own signal handlers, so re-add them.
        r   N)ÚsignalÚSIGINTÚdefault_int_handlerÚImportErrorZ_signalsÚinstall)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|ƒ 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   Ú_runInMainThreadb   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   Ú_stopWxn   s    
zWxReactor._stopWxTc                    s"  t ƒ ˆ _tˆ dƒs(t d¡ ˆ  tƒ ¡ ˆ jˆ j|d� |rJˆ  	dˆ j
¡ ˆ  ddˆ j¡ ˆ  dd‡ fdd„¡ tj ¡ rŽtˆ jƒ}| d	¡ ˆ j ¡  ˆ j}ˆ `ˆ j�sˆ  ¡  | ¡  zˆ jjd
d�}W n tk
rä   Y qºY qºX |dkrò�qz
|ƒ  W qº tk
�r   t ¡  Y qºX 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>�   ó    zWxReactor.run.<locals>.<lambda>é   g{®Gáz„?)ÚtimeoutN)r   r(   r'   r   Úmsgr   r   Z
interleaver+   Z	callLaterr#   ZaddSystemEventTriggerr,   r   ÚplatformZisMacOSXr   r   ZStartZMainLoopr$   r&   r   Úgetr   ÚBaseExceptionÚerr)r   r-   Útr   r*   r   r   r   Úrunw   sJ    
ÿ ÿ  
ÿ





zWxReactor.runN)T)r   r   r   r   r$   r   r#   r&   r+   r,   r:   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.mainr;   )Zreactorr;   r   r   r   r"   ±   s    r"   N)r   Úqueuer   r   Zwxr   r   r   r   r   r	   r!   ZwxPython.wxZtwisted.internetr
   Ztwisted.pythonr   r   r   r%   r   r"   Ú__all__r   r   r   r   Ú<module>   s   t