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.

interfaces.py 5.8KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. import abc
  27. #: all the log-levels that txaio recognizes
  28. log_levels = [
  29. 'none',
  30. 'critical',
  31. 'error',
  32. 'warn',
  33. 'info',
  34. 'debug',
  35. 'trace',
  36. ]
  37. class IBatchedTimer(abc.ABC):
  38. """
  39. Objects created by :met:`txaio.make_batched_timer` implement this
  40. interface.
  41. These APIs allow you to put call_later()'s into "buckets",
  42. reducing the number of actual underlying delayed calls that the
  43. event-loop (asyncio or Twisted) needs to deal with. Obviously, you
  44. lose some amount of precision in when the timers fire in exchange
  45. for less memory use, and fewer objects on the queues for the
  46. underlying event-loop/reactor.
  47. As a concrete example, in Autobahn we're using this to batch
  48. together timers for the "auto ping" feature. In this case, it is
  49. not vital when precisely the timers fire, but as the
  50. connection-count increases the number of outstanding timers
  51. becomes quite large.
  52. It is intended to be used like so:
  53. class Something(object):
  54. timers = txaio.make_batched_timer()
  55. def a_method(self):
  56. self.timers.call_later() # drop-in API from txaio.call_later
  57. """
  58. def call_later(self, delay, func, *args, **kw):
  59. """
  60. This speaks the same API as :meth:`txaio.call_later` and also
  61. returns an object that has a ``.cancel`` method.
  62. You cannot rely on any other methods/attributes of the
  63. returned object. The timeout will actually fire at an
  64. aribtrary time "close" to the delay specified, depening upon
  65. the arguments this IBatchedTimer was created with.
  66. """
  67. class ILogger(abc.ABC):
  68. """
  69. This defines the methods you can call on the object returned from
  70. :meth:`txaio.make_logger` -- although the actual object may have
  71. additional methods, you should *only* call the methods listed
  72. here.
  73. All the log methods have the same signature, they just differ in
  74. what "log level" they represent to the handlers/emitters. The
  75. ``message`` argument is a format string using `PEP3101
  76. <https://www.python.org/dev/peps/pep-3101/>`_-style references to
  77. things from the ``kwargs``. Note that there are also the following
  78. keys added to the ``kwargs``: ``log_time`` and ``log_level``.
  79. For example::
  80. class MyThing(object):
  81. log = txaio.make_logger()
  82. def something_interesting(self, things=dict(one=1, two=2)):
  83. try:
  84. self.log.debug("Called with {things[one]}", things=things)
  85. result = self._method_call()
  86. self.log.info("Got '{result}'.", result=result)
  87. except Exception:
  88. fail = txaio.create_failure()
  89. self.log.critical(txaio.failure_format_traceback(fail))
  90. The philsophy behind txaio's interface is fairly similar to
  91. Twisted's logging APIs after version 15. See `Twisted's
  92. documentation
  93. <http://twistedmatrix.com/documents/current/core/howto/logger.html>`_
  94. for details.
  95. """
  96. # stdlib notes:
  97. # levels:
  98. # CRITICAL 50
  99. # ERROR 40
  100. # WARNING 30
  101. # INFO 20
  102. # DEBUG 10
  103. # NOTSET 0
  104. # NOTES
  105. # things in Twisted's event:
  106. # - log_level
  107. # - log_failure (sometimes?)
  108. # - log_format (can be None)
  109. # - log_source (sometimes? no, always, but sometimes None)
  110. # - log_namespace
  111. #
  112. # .warn not warning!
  113. def critical(self, message, **kwargs):
  114. "log a critical-level message"
  115. def error(self, message, **kwargs):
  116. "log a error-level message"
  117. def warn(self, message, **kwargs):
  118. "log a error-level message"
  119. def info(self, message, **kwargs):
  120. "log an info-level message"
  121. def debug(self, message, **kwargs):
  122. "log an debug-level message"
  123. def trace(self, message, **kwargs):
  124. "log a trace-level message"
  125. class IFailedFuture(abc.ABC):
  126. """
  127. This defines the interface for a common object encapsulating a
  128. failure from either an asyncio task/coroutine or a Twisted
  129. Deferred.
  130. An instance implementing this interface is given to any
  131. ``errback`` callables you provide via :meth:`txaio.add_callbacks`
  132. In your errback you can extract information from an IFailedFuture
  133. with :meth:`txaio.failure_message` and
  134. :meth:`txaio.failure_traceback` or use ``.value`` to get the
  135. Exception instance.
  136. Depending on other details or methods will probably cause
  137. incompatibilities between asyncio and Twisted.
  138. """
  139. @abc.abstractproperty
  140. def value(self):
  141. """
  142. An actual Exception instance. Same as the second item returned from
  143. ``sys.exc_info()``
  144. """