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.

reactors.py 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- test-case-name: twisted.test.test_application -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Plugin-based system for enumerating available reactors and installing one of
  6. them.
  7. """
  8. from typing import Iterable, cast
  9. from zope.interface import Attribute, Interface, implementer
  10. from twisted.internet.interfaces import IReactorCore
  11. from twisted.plugin import IPlugin, getPlugins
  12. from twisted.python.reflect import namedAny
  13. class IReactorInstaller(Interface):
  14. """
  15. Definition of a reactor which can probably be installed.
  16. """
  17. shortName = Attribute(
  18. """
  19. A brief string giving the user-facing name of this reactor.
  20. """
  21. )
  22. description = Attribute(
  23. """
  24. A longer string giving a user-facing description of this reactor.
  25. """
  26. )
  27. def install() -> None:
  28. """
  29. Install this reactor.
  30. """
  31. # TODO - A method which provides a best-guess as to whether this reactor
  32. # can actually be used in the execution environment.
  33. class NoSuchReactor(KeyError):
  34. """
  35. Raised when an attempt is made to install a reactor which cannot be found.
  36. """
  37. @implementer(IPlugin, IReactorInstaller)
  38. class Reactor:
  39. """
  40. @ivar moduleName: The fully-qualified Python name of the module of which
  41. the install callable is an attribute.
  42. """
  43. def __init__(self, shortName: str, moduleName: str, description: str):
  44. self.shortName = shortName
  45. self.moduleName = moduleName
  46. self.description = description
  47. def install(self) -> None:
  48. namedAny(self.moduleName).install()
  49. def getReactorTypes() -> Iterable[IReactorInstaller]:
  50. """
  51. Return an iterator of L{IReactorInstaller} plugins.
  52. """
  53. return getPlugins(IReactorInstaller)
  54. def installReactor(shortName: str) -> IReactorCore:
  55. """
  56. Install the reactor with the given C{shortName} attribute.
  57. @raise NoSuchReactor: If no reactor is found with a matching C{shortName}.
  58. @raise Exception: Anything that the specified reactor can raise when installed.
  59. """
  60. for installer in getReactorTypes():
  61. if installer.shortName == shortName:
  62. installer.install()
  63. from twisted.internet import reactor
  64. return cast(IReactorCore, reactor)
  65. raise NoSuchReactor(shortName)