U
    ª€lj®  ã                   @   s    d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 eeddƒdk	rXe ¡  G d	d
„ d
ejƒZG dd„ dejƒZdeeeef dœdd„ZdgZdS )aÕ  
This module provides support for Twisted to interact with the glib
mainloop via GObject Introspection.

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

    from twisted.internet import gireactor
    gireactor.install()

If you wish to use a GApplication, register it with the reactor::

    from twisted.internet import reactor
    reactor.registerGApplication(app)

Then use twisted.internet APIs as usual.

On Python 3, pygobject v3.4 or later is required.
é    )ÚUnion)ÚGLib)Ú	_glibbase)ÚReactorAlreadyRunning)ÚruntimeÚthreads_initNc                   @   s&   e Zd ZdZdZddd„Zdd„ ZdS )	Ú	GIReactorz¥
    GObject-introspection event loop reactor.

    @ivar _gapplication: A C{Gio.Application} instance that was registered
        with C{registerGApplication}.
    NFc                 C   s   t j | td ¡ d S ©N)r   ÚGlibReactorBaseÚ__init__r   ©ÚselfÚuseGtk© r   ú>/tmp/pip-unpacked-wheel-aghyhbke/twisted/internet/gireactor.pyr   /   s    zGIReactor.__init__c                    sV   | j dk	rtdƒ‚| jr tdƒ‚tˆ dƒs2tdƒ‚ˆ | _ ‡ fdd„}|| _ˆ j| _dS )aƒ  
        Register a C{Gio.Application} or C{Gtk.Application}, whose main loop
        will be used instead of the default one.

        We will C{hold} the application so it doesn't exit on its own. In
        versions of C{python-gi} 3.2 and later, we exit the event loop using
        the C{app.quit} method which overrides any holds. Older versions are
        not supported.
        Nz2Can't register more than one application instance.z5Can't register application after reactor was started.ÚquitzPApplication registration is not supported in versions of PyGObject prior to 3.2.c                      s   ˆ   ¡  ˆ  d ¡ d S r	   )ZholdÚrunr   ©Úappr   r   r   I   s    z+GIReactor.registerGApplication.<locals>.run)Ú_gapplicationÚRuntimeErrorÚ_startedr   ÚhasattrZ_runr   Z_crash)r   r   r   r   r   r   ÚregisterGApplication2   s    

ÿ
ÿzGIReactor.registerGApplication)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   $   s   
r   c                       s8   e Zd ZdZd‡ fdd„	Zdd„ Zddœd	d
„Z‡  ZS )ÚPortableGIReactorz<
    Portable GObject Introspection event loop reactor.
    Fc                    s   t ƒ jtd |d� d S )N©r   )Úsuperr   r   r   ©Ú	__class__r   r   r   W   s    zPortableGIReactor.__init__c                 C   s   t dƒ‚dS )z‡
        Register a C{Gio.Application} or C{Gtk.Application}, whose main loop
        will be used instead of the default one.
        z3GApplication is not currently supported on Windows.N)ÚNotImplementedError)r   r   r   r   r   r   Z   s    z&PortableGIReactor.registerGApplicationN)Úreturnc                 C   s   dS )z5
        For compatibility only. Do nothing.
        Nr   )r   r   r   r   Úsimulatea   s    zPortableGIReactor.simulate)F)r   r   r   r   r   r   r%   Ú__classcell__r   r   r!   r   r   R   s   r   F)r   r$   c                 C   s<   t j ¡ dkrt| d�}n
t| d�}ddlm} ||ƒ |S )zÃ
    Configure the twisted mainloop to be run inside the glib mainloop.

    @param useGtk: A hint that the Gtk GUI will or will not be used.  Currently
        does not modify any behavior.
    Úposixr   r   )ÚinstallReactor)r   ÚplatformÚgetTyper   r   Ztwisted.internet.mainr(   )r   Zreactorr(   r   r   r   Úinstallg   s    
r+   )F)r   Útypingr   Zgi.repositoryr   Ztwisted.internetr   Ztwisted.internet.errorr   Ztwisted.pythonr   Úgetattrr   r
   r   r   Úboolr+   Ú__all__r   r   r   r   Ú<module>   s   .