qubes-events.rst 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. :py:mod:`qubes.events` -- Qubes events
  2. ======================================
  3. Some objects in qubes (most notably domains) emit events. You may hook them and
  4. execute your code when particular event is fired. Events in qubes are added
  5. class-wide -- it is not possible to add event handler to one instance only, you
  6. have to add handler for whole class.
  7. Firing events
  8. -------------
  9. Events are fired by calling :py:meth:`qubes.events.Emitter.fire_event`. The
  10. first argument is event name (a string). You can fire any event you wish, the
  11. names are not checked in any way, however each class' documentation tells what
  12. standard events will be fired on it. The rest of arguments are dependent on the
  13. particular event in question -- they are passed as-is to handlers.
  14. Event handlers are fired in reverse method resolution order, that is, first for
  15. parent class and then for it's child. For each class, first are called handlers
  16. defined in it's source, then handlers from extensions and last the callers added
  17. manually.
  18. There is second method, :py:meth:`qubes.events.Emitter.fire_event_pre`, which
  19. fires events in reverse order. It is suitable for events fired before some
  20. action is performed. You may at your own responsibility raise exceptions from
  21. such events to try to prevent such action.
  22. Events handlers may yield values. Those values are aggregated and returned
  23. to the caller as a list of those values. See below for details.
  24. Handling events
  25. ---------------
  26. There are several ways to handle events. In all cases you supply a callable
  27. (most likely function or method) that will be called when someone fires the
  28. event. The first argument passed to the callable will be the object instance on
  29. which the event was fired and the second one is the event name. The rest are
  30. passed from :py:meth:`qubes.events.Emitter.fire_event` as described previously.
  31. One callable can handle more than one event.
  32. The easiest way to hook an event is to invoke
  33. :py:meth:`qubes.events.Emitter.add_handler` classmethod.
  34. .. code-block:: python
  35. import qubes.events
  36. class MyClass(qubes.events.Emitter):
  37. pass
  38. def event_handler(subject, event):
  39. if event == 'event1':
  40. print('Got event 1')
  41. elif event == 'event2':
  42. print('Got event 2')
  43. MyClass.add_handler('event1', event_handler)
  44. MyClass.add_handler('event2', event_handler)
  45. o = MyClass()
  46. o.fire_event('event1')
  47. If you wish to define handler in the class definition, the best way is to use
  48. :py:func:`qubes.events.handler` decorator.
  49. .. code-block:: python
  50. import qubes.events
  51. class MyClass(qubes.events.Emitter):
  52. @qubes.events.handler('event1', 'event2')
  53. def event_handler(self, event):
  54. if event == 'event1':
  55. print('Got event 1')
  56. elif event == 'event2':
  57. print('Got event 2')
  58. o = MyClass()
  59. o.fire_event('event1')
  60. .. TODO: extensions
  61. Handling events with variable signature
  62. ---------------------------------------
  63. Some events are specified with variable signature (i.e. they may have different
  64. number of arguments on each call to handlers). You can write handlers just like
  65. every other python function with variable signature.
  66. .. code-block:: python
  67. import qubes
  68. def on_property_change(subject, event, name, newvalue, oldvalue=None):
  69. if oldvalue is None:
  70. print('Property {} initialised to {!r}'.format(name, newvalue))
  71. else:
  72. print('Property {} changed {!r} -> {!r}'.format(name, oldvalue, newvalue))
  73. qubes.Qubes.add_handler('property-set:default_netvm')
  74. If you expect :py:obj:`None` to be a reasonable value of the property, you have
  75. a problem. One way to solve it is to invent your very own, magic
  76. :py:class:`object` instance.
  77. .. code-block:: python
  78. import qubes
  79. MAGIC_NO_VALUE = object()
  80. def on_property_change(subject, event, name, newvalue, oldvalue=MAGIC_NO_VALUE):
  81. if oldvalue is MAGIC_NO_VALUE:
  82. print('Property {} initialised to {!r}'.format(name, newvalue))
  83. else:
  84. print('Property {} changed {!r} -> {!r}'.format(name, oldvalue, newvalue))
  85. qubes.Qubes.add_handler('property-set:default_netvm')
  86. There is no possible way of collision other than intentionally passing this very
  87. object (not even passing similar featureless ``object()``), because ``is``
  88. python syntax checks object's :py:meth:`id`\ entity, which will be different for
  89. each :py:class:`object` instance.
  90. Returning values from events
  91. ----------------------------
  92. Some events may be called to collect values from the handlers. For example the
  93. event ``is-fully-usable`` allows plugins to report a domain as not fully usable.
  94. Such handlers, instead of returning :py:obj:`None` (which is the default when
  95. the function does not include ``return`` statement), should return an iterable
  96. or itself be a generator. Those values are aggregated from all handlers and
  97. returned to the caller as list. The order of this list is undefined.
  98. .. code-block:: python
  99. import qubes.events
  100. class MyClass(qubes.events.Emitter):
  101. @qubes.events.handler('event1')
  102. def event1_handler1(self, event):
  103. # do not return anything, equivalent to "return" and "return None"
  104. pass
  105. @qubes.events.handler('event1')
  106. def event1_handler2(self, event):
  107. yield 'aqq'
  108. yield 'zxc'
  109. @qubes.events.handler('event1')
  110. def event1_handler3(self, event):
  111. return ('123', '456')
  112. o = MyClass()
  113. # returns ['aqq', 'zxc', '123', '456'], possibly not in order
  114. effect = o.fire_event('event1')
  115. Module contents
  116. ---------------
  117. .. automodule:: qubes.events
  118. :members:
  119. :show-inheritance:
  120. .. vim: ts=3 sw=3 et