U
    
W[                     @   s  d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ erNdZn6ddlmZ dZzed	  W n ek
r   d
ZY nX ddddgZdd Zese  n6ddlZej  ddlma etdddk	rt  G dd de	jZG dd de	jZ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.
    )divisionabsolute_import)_PY3)ReactorAlreadyRunning)	_glibbase)runtimeT)theSystemPathzgi.pygtkcompatFZgobjectZglibZgioZgtkc                   C   sD   t td ddlma ttdddk	r0t  t jg dtd dS )za
    Make sure pygtk and gi aren't loaded at the same time, and import Glib if
    possible.
    z|Introspected and static glib/gtk bindings must not be mixed; can't import gireactor since pygtk2 module is already imported.r   GLibthreads_initN )ZpreventImports)r   ZensureNotImported_PYGTK_MODULESgi.repositoryr
   getattrr    r   r   </usr/lib/python3/dist-packages/twisted/internet/gireactor.py
_oldGiInit1   s    r   Nr	   r   c                   @   s^   e Zd ZdZejjejjB ejjB Z	ejj
ZejjZee	B Zee	B 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   s0   d }|dkrddl m} tjj| t||d d S NTr   )GtkuseGtk)r   r   r   GlibReactorBase__init__r
   selfr   Z_gtkr   r   r   r   p   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 )N)Zholdrunr   appr   r   r      s    z+GIReactor.registerGApplication.<locals>.run)_gapplicationRuntimeErrorZ_startedr   hasattrZ_runr   Z_crash)r   r   r   r   r   r   registerGApplicationx   s    


zGIReactor.registerGApplication)F)__name__
__module____qualname____doc__r
   ZIOConditionZHUPZERRZNVALZ_POLL_DISCONNECTEDINZ_POLL_INZOUTZ	_POLL_OUTZINFLAGSZOUTFLAGSr    r   r#   r   r   r   r   r   Z   s   
r   c                   @   s"   e Zd ZdZdddZdd ZdS )	PortableGIReactorz<
    Portable GObject Introspection event loop reactor.
    Fc                 C   s0   d }|dkrddl m} tjj| t||d d S r   )r   r   r   PortableGlibReactorBaser   r
   r   r   r   r   r      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#      s    z&PortableGIReactor.registerGApplicationN)F)r$   r%   r&   r'   r   r#   r   r   r   r   r)      s   
	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: should GTK+ rather than glib event loop be
        used (this will be slightly slower but does support GUI).
    posixr   r   )installReactor)r   platformZgetTyper   r)   Ztwisted.internet.mainr-   )r   Zreactorr-   r   r   r   install   s    
r/   )F) r'   Z
__future__r   r   Ztwisted.python.compatr   Ztwisted.internet.errorr   Ztwisted.internetr   Ztwisted.pythonr   Z_pygtkcompatPresentZtwisted.python.modulesr   KeyErrorr   r   Zgi.pygtkcompatZgiZpygtkcompatenabler   r
   r   r   r   r   r*   r)   r/   __all__r   r   r   r   <module>   s4   

;
