2015-01-19 18:03:23 +01:00
|
|
|
#
|
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
|
|
|
#
|
|
|
|
# Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# This library is distributed in the hope that it will be useful,
|
2015-01-19 18:03:23 +01:00
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-10-12 00:11:50 +02:00
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2017-10-12 00:11:50 +02:00
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, see <https://www.gnu.org/licenses/>.
|
2015-01-19 18:03:23 +01:00
|
|
|
#
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Qubes events.
|
|
|
|
|
|
|
|
Events are fired when something happens, like VM start or stop, property change
|
|
|
|
etc.
|
|
|
|
'''
|
2017-06-23 19:03:57 +02:00
|
|
|
import asyncio
|
2014-11-13 14:38:41 +01:00
|
|
|
import collections
|
2018-01-06 00:40:19 +01:00
|
|
|
import fnmatch
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2017-05-12 13:53:30 +02:00
|
|
|
import itertools
|
|
|
|
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
def handler(*events):
|
2014-12-09 14:14:24 +01:00
|
|
|
'''Event handler decorator factory.
|
2014-11-13 18:10:27 +01:00
|
|
|
|
|
|
|
To hook an event, decorate a method in your plugin class with this
|
|
|
|
decorator.
|
|
|
|
|
2018-01-06 00:40:19 +01:00
|
|
|
Some event handlers may be defined as coroutine. In such a case
|
|
|
|
:py:func:`asyncio.coroutine` decorator should be used after this one,
|
|
|
|
i.e. you should decorate a coroutine.
|
2017-06-23 19:03:57 +02:00
|
|
|
See appropriate event documentation for details.
|
2014-12-17 13:36:58 +01:00
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
.. note::
|
|
|
|
For hooking events from extensions, see :py:func:`qubes.ext.handler`.
|
|
|
|
|
2018-01-06 00:40:19 +01:00
|
|
|
:param str events: events names, can contain basic wildcards (`*`, `?`)
|
2014-11-13 18:10:27 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
def decorator(func):
|
2015-10-05 23:46:25 +02:00
|
|
|
# pylint: disable=missing-docstring
|
2015-01-20 14:41:19 +01:00
|
|
|
func.ha_events = events
|
2016-08-09 02:58:14 +02:00
|
|
|
# mark class own handler (i.e. not from extension)
|
|
|
|
func.ha_bound = True
|
2015-01-20 14:41:19 +01:00
|
|
|
return func
|
2014-11-13 14:38:41 +01:00
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
def ishandler(obj):
|
2014-11-13 18:10:27 +01:00
|
|
|
'''Test if a method is hooked to an event.
|
|
|
|
|
|
|
|
:param object o: suspected hook
|
|
|
|
:return: :py:obj:`True` when function is a hook, :py:obj:`False` otherwise
|
|
|
|
:rtype: bool
|
|
|
|
'''
|
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
return callable(obj) \
|
|
|
|
and hasattr(obj, 'ha_events')
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2014-11-13 18:10:27 +01:00
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
class EmitterMeta(type):
|
|
|
|
'''Metaclass for :py:class:`Emitter`'''
|
|
|
|
def __init__(cls, name, bases, dict_):
|
2015-01-20 16:32:25 +01:00
|
|
|
super(EmitterMeta, cls).__init__(name, bases, dict_)
|
2014-12-09 14:14:24 +01:00
|
|
|
cls.__handlers__ = collections.defaultdict(set)
|
|
|
|
|
|
|
|
try:
|
2015-01-21 12:50:00 +01:00
|
|
|
propnames = set(prop.__name__ for prop in cls.property_list())
|
2014-12-09 14:14:24 +01:00
|
|
|
except AttributeError:
|
|
|
|
propnames = set()
|
2014-11-13 18:10:27 +01:00
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
for attr in dict_:
|
2014-12-09 14:14:24 +01:00
|
|
|
if attr in propnames:
|
|
|
|
# we have to be careful, not to getattr() on properties which
|
|
|
|
# may be unset
|
|
|
|
continue
|
2014-11-13 18:10:27 +01:00
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
attr = dict_[attr]
|
2014-12-09 14:14:24 +01:00
|
|
|
if not ishandler(attr):
|
|
|
|
continue
|
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
for event in attr.ha_events:
|
2017-05-12 13:53:30 +02:00
|
|
|
cls.__handlers__[event].add(attr)
|
2015-01-12 14:57:24 +01:00
|
|
|
|
|
|
|
|
2018-07-15 23:08:23 +02:00
|
|
|
class Emitter(metaclass=EmitterMeta):
|
2015-01-19 18:03:23 +01:00
|
|
|
'''Subject that can emit events.
|
2015-06-24 15:02:49 +02:00
|
|
|
|
|
|
|
By default all events are disabled not to interfere with loading from XML.
|
|
|
|
To enable event dispatch, set :py:attr:`events_enabled` to :py:obj:`True`.
|
2015-01-12 14:57:24 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Emitter, self).__init__(*args, **kwargs)
|
2015-09-17 12:08:03 +02:00
|
|
|
if not hasattr(self, 'events_enabled'):
|
|
|
|
self.events_enabled = False
|
2017-05-12 13:53:30 +02:00
|
|
|
self.__handlers__ = collections.defaultdict(set)
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2017-08-28 14:24:48 +02:00
|
|
|
def close(self):
|
|
|
|
self.events_enabled = False
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2017-05-12 13:53:30 +02:00
|
|
|
def add_handler(self, event, func):
|
2015-01-19 18:03:23 +01:00
|
|
|
'''Add event handler to subject's class.
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2017-05-10 15:52:59 +02:00
|
|
|
This is class method, it is invalid to call it on object instance.
|
|
|
|
|
2014-12-09 14:14:24 +01:00
|
|
|
:param str event: event identificator
|
|
|
|
:param collections.Callable handler: handler callable
|
|
|
|
'''
|
|
|
|
|
2015-01-19 19:02:28 +01:00
|
|
|
# pylint: disable=no-member
|
2017-05-12 13:53:30 +02:00
|
|
|
self.__handlers__[event].add(func)
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2017-05-12 13:53:30 +02:00
|
|
|
def remove_handler(self, event, func):
|
2017-04-10 00:50:18 +02:00
|
|
|
'''Remove event handler from subject's class.
|
|
|
|
|
2017-05-10 15:52:59 +02:00
|
|
|
This is class method, it is invalid to call it on object instance.
|
|
|
|
|
|
|
|
This method must be called on the same class as
|
|
|
|
:py:meth:`add_handler` was called to register the handler.
|
|
|
|
|
2017-04-10 00:50:18 +02:00
|
|
|
:param str event: event identificator
|
|
|
|
:param collections.Callable handler: handler callable
|
|
|
|
'''
|
|
|
|
|
|
|
|
# pylint: disable=no-member
|
2017-05-12 13:53:30 +02:00
|
|
|
self.__handlers__[event].remove(func)
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2017-06-23 17:29:09 +02:00
|
|
|
def _fire_event(self, event, kwargs, pre_event=False):
|
2014-12-29 13:07:20 +01:00
|
|
|
'''Fire event for classes in given order.
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2017-06-23 17:29:09 +02:00
|
|
|
Do not use this method. Use :py:meth:`fire_event`.
|
2014-12-09 14:14:24 +01:00
|
|
|
'''
|
2014-11-13 18:10:27 +01:00
|
|
|
|
2014-12-09 18:34:00 +01:00
|
|
|
if not self.events_enabled:
|
2017-06-23 19:03:57 +02:00
|
|
|
return [], []
|
2014-12-09 18:34:00 +01:00
|
|
|
|
2017-06-23 17:29:09 +02:00
|
|
|
order = itertools.chain((self,), self.__class__.__mro__)
|
|
|
|
if not pre_event:
|
|
|
|
order = reversed(list(order))
|
|
|
|
|
2016-04-02 21:36:07 +02:00
|
|
|
effects = []
|
2017-06-23 19:03:57 +02:00
|
|
|
async_effects = []
|
2017-05-12 13:53:30 +02:00
|
|
|
for i in order:
|
|
|
|
try:
|
|
|
|
handlers_dict = i.__handlers__
|
|
|
|
except AttributeError:
|
2014-12-17 13:36:58 +01:00
|
|
|
continue
|
2018-01-06 00:40:19 +01:00
|
|
|
handlers = [h_func for h_name, h_func_set in handlers_dict.items()
|
|
|
|
for h_func in h_func_set
|
|
|
|
if fnmatch.fnmatch(event, h_name)]
|
2016-09-19 22:49:24 +02:00
|
|
|
for func in sorted(handlers,
|
2015-01-19 17:06:30 +01:00
|
|
|
key=(lambda handler: hasattr(handler, 'ha_bound')),
|
|
|
|
reverse=True):
|
2017-02-21 14:09:06 +01:00
|
|
|
effect = func(self, event, **kwargs)
|
2017-06-23 19:03:57 +02:00
|
|
|
if asyncio.iscoroutinefunction(func):
|
|
|
|
async_effects.append(effect)
|
|
|
|
elif effect is not None:
|
2016-04-08 12:35:11 +02:00
|
|
|
effects.extend(effect)
|
2017-06-23 19:03:57 +02:00
|
|
|
return effects, async_effects
|
2014-12-29 13:07:20 +01:00
|
|
|
|
2017-06-23 17:29:09 +02:00
|
|
|
def fire_event(self, event, pre_event=False, **kwargs):
|
2014-12-29 13:07:20 +01:00
|
|
|
'''Call all handlers for an event.
|
|
|
|
|
2017-06-23 12:15:31 +02:00
|
|
|
Handlers are called for class and all parent classes, in **reversed**
|
2017-06-23 17:29:09 +02:00
|
|
|
or **true** (depending on *pre_event* parameter)
|
2014-12-29 13:07:20 +01:00
|
|
|
method resolution order. For each class first are called bound handlers
|
|
|
|
(specified in class definition), then handlers from extensions. Aside
|
|
|
|
from above, remaining order is undefined.
|
|
|
|
|
2017-06-23 19:03:57 +02:00
|
|
|
This method call only synchronous handlers. If any asynchronous
|
|
|
|
handler is registered for the event, :py:class:``RuntimeError`` is
|
|
|
|
raised.
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
:py:meth:`fire_event_async`
|
|
|
|
|
2017-06-23 12:15:31 +02:00
|
|
|
:param str event: event identifier
|
2017-06-23 17:29:09 +02:00
|
|
|
:param pre_event: is this -pre- event? reverse handlers calling order
|
2016-04-02 21:36:07 +02:00
|
|
|
:returns: list of effects
|
2014-12-29 13:07:20 +01:00
|
|
|
|
2017-02-21 14:09:06 +01:00
|
|
|
All *kwargs* are passed verbatim. They are different for different
|
|
|
|
events.
|
2014-12-29 13:07:20 +01:00
|
|
|
'''
|
|
|
|
|
2017-06-23 19:03:57 +02:00
|
|
|
sync_effects, async_effects = self._fire_event(event, kwargs,
|
|
|
|
pre_event=pre_event)
|
|
|
|
if async_effects:
|
|
|
|
raise RuntimeError(
|
|
|
|
'unexpected async-handler(s) {!r} for sync event {!s}'.format(
|
|
|
|
async_effects, event))
|
|
|
|
return sync_effects
|
|
|
|
|
|
|
|
|
|
|
|
@asyncio.coroutine
|
|
|
|
def fire_event_async(self, event, pre_event=False, **kwargs):
|
|
|
|
'''Call all handlers for an event, allowing async calls.
|
|
|
|
|
|
|
|
Handlers are called for class and all parent classes, in **reversed**
|
|
|
|
or **true** (depending on *pre_event* parameter)
|
|
|
|
method resolution order. For each class first are called bound handlers
|
|
|
|
(specified in class definition), then handlers from extensions. Aside
|
|
|
|
from above, remaining order is undefined.
|
|
|
|
|
|
|
|
This method call both synchronous and asynchronous handlers. Order of
|
|
|
|
asynchronous calls is, by definition, undefined.
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
:py:meth:`fire_event`
|
|
|
|
|
|
|
|
:param str event: event identifier
|
|
|
|
:param pre_event: is this -pre- event? reverse handlers calling order
|
|
|
|
:returns: list of effects
|
|
|
|
|
|
|
|
All *kwargs* are passed verbatim. They are different for different
|
|
|
|
events.
|
|
|
|
'''
|
|
|
|
|
|
|
|
sync_effects, async_effects = self._fire_event(event,
|
|
|
|
kwargs, pre_event=pre_event)
|
|
|
|
effects = sync_effects
|
|
|
|
if async_effects:
|
|
|
|
async_tasks, _ = yield from asyncio.wait(async_effects)
|
|
|
|
for task in async_tasks:
|
|
|
|
effect = task.result()
|
|
|
|
if effect is not None:
|
|
|
|
effects.extend(effect)
|
|
|
|
return effects
|