Advertisement
Guest User

Untitled

a guest
May 23rd, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.20 KB | None | 0 0
  1. # -*- test-case-name: twisted.internet.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4.  
  5.  
  6. """
  7. This module provides support for Twisted to interact with the glib/gtk2
  8. mainloop.
  9.  
  10. In order to use this support, simply do the following::
  11.  
  12.    from twisted.internet import gtk2reactor
  13.    gtk2reactor.install()
  14.  
  15. Then use twisted.internet APIs as usual.  The other methods here are not
  16. intended to be called directly.
  17. """
  18.  
  19. # System Imports
  20. import sys
  21.  
  22. if 'gi' in sys.modules:
  23.     raise ImportError(
  24.         "Introspected and static glib/gtk bindings must not be mixed; can't "
  25.         "import gtk2reactor since gi module is already imported.")
  26.  
  27. # Disable gi imports to avoid potential problems.
  28. sys.modules['gi'] = None
  29.  
  30. try:
  31.     if not hasattr(sys, 'frozen'):
  32.         # Don't want to check this for py2exe
  33.         import pygtk
  34.         pygtk.require('2.0')
  35. except (ImportError, AttributeError):
  36.     pass # maybe we're using pygtk before this hack existed.
  37.  
  38. import gobject
  39. if hasattr(gobject, "threads_init"):
  40.     # recent versions of python-gtk expose this. python-gtk=2.4.1
  41.     # (wrapping glib-2.4.7) does. python-gtk=2.0.0 (wrapping
  42.     # glib-2.2.3) does not.
  43.     gobject.threads_init()
  44.  
  45. # Twisted Imports
  46. from twisted.internet import _glibbase
  47. from twisted.python import runtime
  48.  
  49.  
  50. class Gtk2Reactor(_glibbase.GlibReactorBase):
  51.     """
  52.    PyGTK+ 2 event loop reactor.
  53.    """
  54.     _POLL_DISCONNECTED = gobject.IO_HUP | gobject.IO_ERR | gobject.IO_NVAL
  55.     _POLL_IN = gobject.IO_IN
  56.     _POLL_OUT = gobject.IO_OUT
  57.  
  58.     # glib's iochannel sources won't tell us about any events that we haven't
  59.     # asked for, even if those events aren't sensible inputs to the poll()
  60.     # call.
  61.     INFLAGS = _POLL_IN | _POLL_DISCONNECTED
  62.     OUTFLAGS = _POLL_OUT | _POLL_DISCONNECTED
  63.  
  64.     def __init__(self, useGtk=True):
  65.         _gtk = None
  66.         if useGtk is True:
  67.             import gtk as _gtk
  68.  
  69.         _glibbase.GlibReactorBase.__init__(self, gobject, _gtk, useGtk=useGtk)
  70.  
  71.  
  72.  
  73. class PortableGtkReactor(_glibbase.PortableGlibReactorBase):
  74.     """
  75.    Reactor that works on Windows.
  76.  
  77.    Sockets aren't supported by GTK+'s input_add on Win32.
  78.    """
  79.     def __init__(self, useGtk=True):
  80.         _gtk = None
  81.         if useGtk is True:
  82.             import gtk as _gtk
  83.  
  84.         _glibbase.PortableGlibReactorBase.__init__(self, gobject, _gtk,
  85.                                                    useGtk=useGtk)
  86.  
  87.  
  88. def install(useGtk=True):
  89.     """
  90.    Configure the twisted mainloop to be run inside the gtk mainloop.
  91.  
  92.    @param useGtk: should glib rather than GTK+ event loop be
  93.        used (this will be slightly faster but does not support GUI).
  94.    """
  95.     reactor = Gtk2Reactor(useGtk)
  96.     from twisted.internet.main import installReactor
  97.     installReactor(reactor)
  98.     return reactor
  99.  
  100.  
  101. def portableInstall(useGtk=True):
  102.     """
  103.    Configure the twisted mainloop to be run inside the gtk mainloop.
  104.    """
  105.     reactor = PortableGtkReactor()
  106.     from twisted.internet.main import installReactor
  107.     installReactor(reactor)
  108.     return reactor
  109.  
  110.  
  111. if runtime.platform.getType() != 'posix':
  112.     install = portableInstall
  113.  
  114.  
  115. __all__ = ['install']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement