Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gireactor.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. This module provides support for Twisted to interact with the glib
  5. mainloop via GObject Introspection.
  6. In order to use this support, simply do the following::
  7. from twisted.internet import gireactor
  8. gireactor.install()
  9. If you wish to use a GApplication, register it with the reactor::
  10. from twisted.internet import reactor
  11. reactor.registerGApplication(app)
  12. Then use twisted.internet APIs as usual.
  13. On Python 3, pygobject v3.4 or later is required.
  14. """
  15. import gi.pygtkcompat # type: ignore[import]
  16. from gi.repository import GLib # type: ignore[import]
  17. from twisted.internet import _glibbase
  18. from twisted.internet.error import ReactorAlreadyRunning
  19. from twisted.python import runtime
  20. # We require a sufficiently new version of pygobject, so always exists:
  21. _pygtkcompatPresent = True
  22. # Newer version of gi, so we can try to initialize compatibility layer; if
  23. # real pygtk was already imported we'll get ImportError at this point
  24. # rather than segfault, so unconditional import is fine.
  25. gi.pygtkcompat.enable()
  26. # At this point importing gobject will get you gi version, and importing
  27. # e.g. gtk will either fail in non-segfaulty way or use gi version if user
  28. # does gi.pygtkcompat.enable_gtk(). So, no need to prevent imports of
  29. # old school pygtk modules.
  30. if getattr(GLib, "threads_init", None) is not None:
  31. GLib.threads_init()
  32. class GIReactor(_glibbase.GlibReactorBase):
  33. """
  34. GObject-introspection event loop reactor.
  35. @ivar _gapplication: A C{Gio.Application} instance that was registered
  36. with C{registerGApplication}.
  37. """
  38. _POLL_DISCONNECTED = (
  39. GLib.IOCondition.HUP | GLib.IOCondition.ERR | GLib.IOCondition.NVAL
  40. )
  41. _POLL_IN = GLib.IOCondition.IN
  42. _POLL_OUT = GLib.IOCondition.OUT
  43. # glib's iochannel sources won't tell us about any events that we haven't
  44. # asked for, even if those events aren't sensible inputs to the poll()
  45. # call.
  46. INFLAGS = _POLL_IN | _POLL_DISCONNECTED
  47. OUTFLAGS = _POLL_OUT | _POLL_DISCONNECTED
  48. # By default no Application is registered:
  49. _gapplication = None
  50. def __init__(self, useGtk=False):
  51. _gtk = None
  52. if useGtk is True:
  53. from gi.repository import Gtk as _gtk
  54. _glibbase.GlibReactorBase.__init__(self, GLib, _gtk, useGtk=useGtk)
  55. def registerGApplication(self, app):
  56. """
  57. Register a C{Gio.Application} or C{Gtk.Application}, whose main loop
  58. will be used instead of the default one.
  59. We will C{hold} the application so it doesn't exit on its own. In
  60. versions of C{python-gi} 3.2 and later, we exit the event loop using
  61. the C{app.quit} method which overrides any holds. Older versions are
  62. not supported.
  63. """
  64. if self._gapplication is not None:
  65. raise RuntimeError("Can't register more than one application instance.")
  66. if self._started:
  67. raise ReactorAlreadyRunning(
  68. "Can't register application after reactor was started."
  69. )
  70. if not hasattr(app, "quit"):
  71. raise RuntimeError(
  72. "Application registration is not supported in"
  73. " versions of PyGObject prior to 3.2."
  74. )
  75. self._gapplication = app
  76. def run():
  77. app.hold()
  78. app.run(None)
  79. self._run = run
  80. self._crash = app.quit
  81. class PortableGIReactor(_glibbase.PortableGlibReactorBase):
  82. """
  83. Portable GObject Introspection event loop reactor.
  84. """
  85. def __init__(self, useGtk=False):
  86. _gtk = None
  87. if useGtk is True:
  88. from gi.repository import Gtk as _gtk
  89. _glibbase.PortableGlibReactorBase.__init__(self, GLib, _gtk, useGtk=useGtk)
  90. def registerGApplication(self, app):
  91. """
  92. Register a C{Gio.Application} or C{Gtk.Application}, whose main loop
  93. will be used instead of the default one.
  94. """
  95. raise NotImplementedError("GApplication is not currently supported on Windows.")
  96. def install(useGtk=False):
  97. """
  98. Configure the twisted mainloop to be run inside the glib mainloop.
  99. @param useGtk: should GTK+ rather than glib event loop be
  100. used (this will be slightly slower but does support GUI).
  101. """
  102. if runtime.platform.getType() == "posix":
  103. reactor = GIReactor(useGtk=useGtk)
  104. else:
  105. reactor = PortableGIReactor(useGtk=useGtk)
  106. from twisted.internet.main import installReactor
  107. installReactor(reactor)
  108. return reactor
  109. __all__ = ["install"]