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
|
|
|
#
|
|
|
|
|
|
|
|
'''Qubes extensions.
|
2014-11-13 18:10:27 +01:00
|
|
|
|
|
|
|
Extensions provide additional features (like application menus) found only on
|
|
|
|
some systems. They may be OS- or architecture-dependent or custom-developed for
|
|
|
|
particular customer.
|
|
|
|
'''
|
|
|
|
|
2016-04-11 14:52:01 +02:00
|
|
|
import pkg_resources
|
2014-11-13 14:38:41 +01:00
|
|
|
import qubes.events
|
|
|
|
|
|
|
|
|
2018-07-15 23:08:23 +02:00
|
|
|
class Extension:
|
2014-12-09 14:14:24 +01:00
|
|
|
'''Base class for all extensions
|
2016-04-11 14:52:01 +02:00
|
|
|
''' # pylint: disable=too-few-public-methods
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2016-04-11 14:52:01 +02:00
|
|
|
def __new__(cls):
|
|
|
|
if '_instance' not in cls.__dict__:
|
|
|
|
cls._instance = super(Extension, cls).__new__(cls)
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2016-04-11 14:52:01 +02:00
|
|
|
for name in cls.__dict__:
|
|
|
|
attr = getattr(cls._instance, name)
|
|
|
|
if not qubes.events.ishandler(attr):
|
|
|
|
continue
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2016-04-11 14:52:01 +02:00
|
|
|
if attr.ha_vm is not None:
|
|
|
|
for event in attr.ha_events:
|
2017-05-12 13:53:30 +02:00
|
|
|
attr.ha_vm.__handlers__[event].add(attr)
|
2016-04-11 14:52:01 +02:00
|
|
|
else:
|
|
|
|
# global hook
|
|
|
|
for event in attr.ha_events:
|
2017-05-12 13:53:30 +02:00
|
|
|
# pylint: disable=no-member
|
|
|
|
qubes.Qubes.__handlers__[event].add(attr)
|
2014-11-13 14:38:41 +01:00
|
|
|
|
2016-04-11 14:52:01 +02:00
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
|
|
|
|
def get_extensions():
|
|
|
|
return set(ext.load()()
|
|
|
|
for ext in pkg_resources.iter_entry_points('qubes.ext'))
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
def handler(*events, **kwargs):
|
2014-12-09 14:14:24 +01:00
|
|
|
'''Event handler decorator factory.
|
|
|
|
|
|
|
|
To hook an event, decorate a method in your plugin class with this
|
|
|
|
decorator. You may hook both per-vm-class and global events.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
This decorator is intended only for extensions! For regular use in the
|
2015-01-19 18:03:23 +01:00
|
|
|
core, see :py:func:`qubes.events.handler`.
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
:param str event: event type
|
|
|
|
:param type vm: VM to hook (leave as None to hook all VMs)
|
2015-01-19 17:06:30 +01:00
|
|
|
:param bool system: when :py:obj:`True`, hook is system-wide (not attached \
|
|
|
|
to any VM)
|
2014-12-09 14:14:24 +01:00
|
|
|
'''
|
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
def decorator(func):
|
|
|
|
func.ha_events = events
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2015-01-12 14:57:24 +01:00
|
|
|
if kwargs.get('system', False):
|
2015-01-20 14:41:19 +01:00
|
|
|
func.ha_vm = None
|
2015-01-12 14:57:24 +01:00
|
|
|
elif 'vm' in kwargs:
|
2015-01-20 14:41:19 +01:00
|
|
|
func.ha_vm = kwargs['vm']
|
2014-12-09 14:14:24 +01:00
|
|
|
else:
|
2015-01-20 14:41:19 +01:00
|
|
|
func.ha_vm = qubes.vm.BaseVM
|
2014-12-09 14:14:24 +01:00
|
|
|
|
2015-01-20 14:41:19 +01:00
|
|
|
return func
|
2014-12-09 14:14:24 +01:00
|
|
|
|
|
|
|
return decorator
|