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.

__init__.py 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. from txaio._version import __version__
  27. from txaio.interfaces import IFailedFuture, ILogger
  28. version = __version__
  29. # This is the API
  30. # see tx.py for Twisted implementation
  31. # see aio.py for asyncio implementation
  32. class _Config(object):
  33. """
  34. This holds all valid configuration options, accessed as
  35. class-level variables. For example, if you were using asyncio:
  36. .. sourcecode:: python
  37. txaio.config.loop = asyncio.get_event_loop()
  38. ``loop`` is populated automatically (while importing one of the
  39. framework-specific libraries) but can be changed before any call
  40. into this library. Currently, it's only used by :meth:`call_later`
  41. If using asyncio, you must set this to an event-loop (by default,
  42. we use asyncio.get_event_loop). If using Twisted, set this to a
  43. reactor instance (by default we "from twisted.internet import
  44. reactor" on the first call to call_later)
  45. """
  46. #: the event-loop object to use
  47. loop = None
  48. __all__ = (
  49. 'with_config', # allow mutliple custom configurations at once
  50. 'using_twisted', # True if we're using Twisted
  51. 'using_asyncio', # True if we're using asyncio
  52. 'use_twisted', # sets the library to use Twisted, or exception
  53. 'use_asyncio', # sets the library to use asyncio, or exception
  54. 'config', # the config instance, access via attributes
  55. 'create_future', # create a Future (can be already resolved/errored)
  56. 'create_future_success',
  57. 'create_future_error',
  58. 'create_failure', # return an object implementing IFailedFuture
  59. 'as_future', # call a method, and always return a Future
  60. 'is_future', # True for Deferreds in tx and Futures, @coroutines in asyncio
  61. 'reject', # errback a Future
  62. 'resolve', # callback a Future
  63. 'cancel', # cancel a Future
  64. 'add_callbacks', # add callback and/or errback
  65. 'gather', # return a Future waiting for several other Futures
  66. 'is_called', # True if the Future has a result
  67. 'call_later', # call the callback after the given delay seconds
  68. 'failure_message', # a printable error-message from a IFailedFuture
  69. 'failure_traceback', # returns a traceback instance from an IFailedFuture
  70. 'failure_format_traceback', # a string, the formatted traceback
  71. 'make_batched_timer', # create BatchedTimer/IBatchedTimer instances
  72. 'make_logger', # creates an object implementing ILogger
  73. 'start_logging', # initializes logging (may grab stdin at this point)
  74. 'set_global_log_level', # Set the global log level
  75. 'get_global_log_level', # Get the global log level
  76. 'add_log_categories',
  77. 'IFailedFuture', # describes API for arg to errback()s
  78. 'ILogger', # API for logging
  79. 'sleep', # little helper for inline sleeping
  80. 'time_ns', # helper: current time (UTC) in ns
  81. 'perf_counter_ns', # helper: current performance counter in ns
  82. )
  83. _explicit_framework = None
  84. def use_twisted():
  85. global _explicit_framework
  86. if _explicit_framework is not None and _explicit_framework != 'twisted':
  87. raise RuntimeError("Explicitly using '{}' already".format(_explicit_framework))
  88. _explicit_framework = 'twisted'
  89. from txaio import tx
  90. _use_framework(tx)
  91. import txaio
  92. txaio.using_twisted = True
  93. txaio.using_asyncio = False
  94. def use_asyncio():
  95. global _explicit_framework
  96. if _explicit_framework is not None and _explicit_framework != 'asyncio':
  97. raise RuntimeError("Explicitly using '{}' already".format(_explicit_framework))
  98. _explicit_framework = 'asyncio'
  99. from txaio import aio
  100. _use_framework(aio)
  101. import txaio
  102. txaio.using_twisted = False
  103. txaio.using_asyncio = True
  104. def _use_framework(module):
  105. """
  106. Internal helper, to set this modules methods to a specified
  107. framework helper-methods.
  108. """
  109. import txaio
  110. for method_name in __all__:
  111. if method_name in ['use_twisted', 'use_asyncio']:
  112. continue
  113. setattr(txaio, method_name, getattr(module, method_name))
  114. # use the "un-framework", which is neither asyncio nor twisted and
  115. # just throws an exception -- this forces you to call .use_twisted()
  116. # or .use_asyncio() to use the library.
  117. from txaio import _unframework # noqa
  118. _use_framework(_unframework)