events.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  19. #
  20. '''Qubes events.
  21. Events are fired when something happens, like VM start or stop, property change
  22. etc.
  23. '''
  24. import asyncio
  25. import collections
  26. import itertools
  27. def handler(*events):
  28. '''Event handler decorator factory.
  29. To hook an event, decorate a method in your plugin class with this
  30. decorator.
  31. Some event handlers may be defined as coroutine. In such a case, *async*
  32. should be set to :py:obj:``True``.
  33. See appropriate event documentation for details.
  34. .. note::
  35. For hooking events from extensions, see :py:func:`qubes.ext.handler`.
  36. :param str events: events
  37. '''
  38. def decorator(func):
  39. # pylint: disable=missing-docstring
  40. func.ha_events = events
  41. # mark class own handler (i.e. not from extension)
  42. func.ha_bound = True
  43. return func
  44. return decorator
  45. def ishandler(obj):
  46. '''Test if a method is hooked to an event.
  47. :param object o: suspected hook
  48. :return: :py:obj:`True` when function is a hook, :py:obj:`False` otherwise
  49. :rtype: bool
  50. '''
  51. return callable(obj) \
  52. and hasattr(obj, 'ha_events')
  53. class EmitterMeta(type):
  54. '''Metaclass for :py:class:`Emitter`'''
  55. def __init__(cls, name, bases, dict_):
  56. super(EmitterMeta, cls).__init__(name, bases, dict_)
  57. cls.__handlers__ = collections.defaultdict(set)
  58. try:
  59. propnames = set(prop.__name__ for prop in cls.property_list())
  60. except AttributeError:
  61. propnames = set()
  62. for attr in dict_:
  63. if attr in propnames:
  64. # we have to be careful, not to getattr() on properties which
  65. # may be unset
  66. continue
  67. attr = dict_[attr]
  68. if not ishandler(attr):
  69. continue
  70. for event in attr.ha_events:
  71. cls.__handlers__[event].add(attr)
  72. class Emitter(object, metaclass=EmitterMeta):
  73. '''Subject that can emit events.
  74. By default all events are disabled not to interfere with loading from XML.
  75. To enable event dispatch, set :py:attr:`events_enabled` to :py:obj:`True`.
  76. '''
  77. def __init__(self, *args, **kwargs):
  78. super(Emitter, self).__init__(*args, **kwargs)
  79. if not hasattr(self, 'events_enabled'):
  80. self.events_enabled = False
  81. self.__handlers__ = collections.defaultdict(set)
  82. def close(self):
  83. self.events_enabled = False
  84. def add_handler(self, event, func):
  85. '''Add event handler to subject's class.
  86. This is class method, it is invalid to call it on object instance.
  87. :param str event: event identificator
  88. :param collections.Callable handler: handler callable
  89. '''
  90. # pylint: disable=no-member
  91. self.__handlers__[event].add(func)
  92. def remove_handler(self, event, func):
  93. '''Remove event handler from subject's class.
  94. This is class method, it is invalid to call it on object instance.
  95. This method must be called on the same class as
  96. :py:meth:`add_handler` was called to register the handler.
  97. :param str event: event identificator
  98. :param collections.Callable handler: handler callable
  99. '''
  100. # pylint: disable=no-member
  101. self.__handlers__[event].remove(func)
  102. def _fire_event(self, event, kwargs, pre_event=False):
  103. '''Fire event for classes in given order.
  104. Do not use this method. Use :py:meth:`fire_event`.
  105. '''
  106. if not self.events_enabled:
  107. return [], []
  108. order = itertools.chain((self,), self.__class__.__mro__)
  109. if not pre_event:
  110. order = reversed(list(order))
  111. effects = []
  112. async_effects = []
  113. for i in order:
  114. try:
  115. handlers_dict = i.__handlers__
  116. except AttributeError:
  117. continue
  118. handlers = handlers_dict.get(event, set())
  119. if '*' in handlers_dict:
  120. handlers = handlers_dict['*'] | handlers
  121. for func in sorted(handlers,
  122. key=(lambda handler: hasattr(handler, 'ha_bound')),
  123. reverse=True):
  124. effect = func(self, event, **kwargs)
  125. if asyncio.iscoroutinefunction(func):
  126. async_effects.append(effect)
  127. elif effect is not None:
  128. effects.extend(effect)
  129. return effects, async_effects
  130. def fire_event(self, event, pre_event=False, **kwargs):
  131. '''Call all handlers for an event.
  132. Handlers are called for class and all parent classes, in **reversed**
  133. or **true** (depending on *pre_event* parameter)
  134. method resolution order. For each class first are called bound handlers
  135. (specified in class definition), then handlers from extensions. Aside
  136. from above, remaining order is undefined.
  137. This method call only synchronous handlers. If any asynchronous
  138. handler is registered for the event, :py:class:``RuntimeError`` is
  139. raised.
  140. .. seealso::
  141. :py:meth:`fire_event_async`
  142. :param str event: event identifier
  143. :param pre_event: is this -pre- event? reverse handlers calling order
  144. :returns: list of effects
  145. All *kwargs* are passed verbatim. They are different for different
  146. events.
  147. '''
  148. sync_effects, async_effects = self._fire_event(event, kwargs,
  149. pre_event=pre_event)
  150. if async_effects:
  151. raise RuntimeError(
  152. 'unexpected async-handler(s) {!r} for sync event {!s}'.format(
  153. async_effects, event))
  154. return sync_effects
  155. @asyncio.coroutine
  156. def fire_event_async(self, event, pre_event=False, **kwargs):
  157. '''Call all handlers for an event, allowing async calls.
  158. Handlers are called for class and all parent classes, in **reversed**
  159. or **true** (depending on *pre_event* parameter)
  160. method resolution order. For each class first are called bound handlers
  161. (specified in class definition), then handlers from extensions. Aside
  162. from above, remaining order is undefined.
  163. This method call both synchronous and asynchronous handlers. Order of
  164. asynchronous calls is, by definition, undefined.
  165. .. seealso::
  166. :py:meth:`fire_event`
  167. :param str event: event identifier
  168. :param pre_event: is this -pre- event? reverse handlers calling order
  169. :returns: list of effects
  170. All *kwargs* are passed verbatim. They are different for different
  171. events.
  172. '''
  173. sync_effects, async_effects = self._fire_event(event,
  174. kwargs, pre_event=pre_event)
  175. effects = sync_effects
  176. if async_effects:
  177. async_tasks, _ = yield from asyncio.wait(async_effects)
  178. for task in async_tasks:
  179. effect = task.result()
  180. if effect is not None:
  181. effects.extend(effect)
  182. return effects