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{®Gáz„?)Ú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
