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.

gtk2reactor.py 3.6KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # -*- test-case-name: twisted.internet.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. This module provides support for Twisted to interact with the glib/gtk2
  6. mainloop.
  7. In order to use this support, simply do the following::
  8. from twisted.internet import gtk2reactor
  9. gtk2reactor.install()
  10. Then use twisted.internet APIs as usual. The other methods here are not
  11. intended to be called directly.
  12. """
  13. # System Imports
  14. import sys
  15. # Twisted Imports
  16. from twisted.internet import _glibbase
  17. from twisted.python import runtime
  18. # Certain old versions of pygtk and gi crash if imported at the same
  19. # time. This is a problem when running Twisted's unit tests, since they will
  20. # attempt to run both gtk2 and gtk3/gi tests. However, gireactor makes sure
  21. # that if we are in such an old version, and gireactor was imported,
  22. # gtk2reactor will not be importable. So we don't *need* to enforce that here
  23. # as well; whichever is imported first will still win. Moreover, additional
  24. # enforcement in this module is unnecessary in modern versions, and downright
  25. # problematic in certain versions where for some reason importing gtk also
  26. # imports some subset of gi. So we do nothing here, relying on gireactor to
  27. # prevent the crash.
  28. try:
  29. if not hasattr(sys, "frozen"):
  30. # Don't want to check this for py2exe
  31. import pygtk # type: ignore[import]
  32. pygtk.require("2.0")
  33. except (ImportError, AttributeError):
  34. pass # maybe we're using pygtk before this hack existed.
  35. import gobject # type: ignore[import]
  36. if hasattr(gobject, "threads_init"):
  37. # recent versions of python-gtk expose this. python-gtk=2.4.1
  38. # (wrapping glib-2.4.7) does. python-gtk=2.0.0 (wrapping
  39. # glib-2.2.3) does not.
  40. gobject.threads_init()
  41. class Gtk2Reactor(_glibbase.GlibReactorBase):
  42. """
  43. PyGTK+ 2 event loop reactor.
  44. """
  45. _POLL_DISCONNECTED = gobject.IO_HUP | gobject.IO_ERR | gobject.IO_NVAL
  46. _POLL_IN = gobject.IO_IN
  47. _POLL_OUT = gobject.IO_OUT
  48. # glib's iochannel sources won't tell us about any events that we haven't
  49. # asked for, even if those events aren't sensible inputs to the poll()
  50. # call.
  51. INFLAGS = _POLL_IN | _POLL_DISCONNECTED
  52. OUTFLAGS = _POLL_OUT | _POLL_DISCONNECTED
  53. def __init__(self, useGtk=True):
  54. _gtk = None
  55. if useGtk is True:
  56. import gtk as _gtk # type: ignore[import]
  57. _glibbase.GlibReactorBase.__init__(self, gobject, _gtk, useGtk=useGtk)
  58. class PortableGtkReactor(_glibbase.PortableGlibReactorBase):
  59. """
  60. Reactor that works on Windows.
  61. Sockets aren't supported by GTK+'s input_add on Win32.
  62. """
  63. def __init__(self, useGtk=True):
  64. _gtk = None
  65. if useGtk is True:
  66. import gtk as _gtk
  67. _glibbase.PortableGlibReactorBase.__init__(self, gobject, _gtk, useGtk=useGtk)
  68. def install(useGtk=True):
  69. """
  70. Configure the twisted mainloop to be run inside the gtk mainloop.
  71. @param useGtk: should glib rather than GTK+ event loop be
  72. used (this will be slightly faster but does not support GUI).
  73. """
  74. reactor = Gtk2Reactor(useGtk)
  75. from twisted.internet.main import installReactor
  76. installReactor(reactor)
  77. return reactor
  78. def portableInstall(useGtk=True):
  79. """
  80. Configure the twisted mainloop to be run inside the gtk mainloop.
  81. """
  82. reactor = PortableGtkReactor()
  83. from twisted.internet.main import installReactor
  84. installReactor(reactor)
  85. return reactor
  86. if runtime.platform.getType() == "posix":
  87. install = install
  88. else:
  89. install = portableInstall
  90. __all__ = ["install"]