__init__.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 extensions.
  21. Extensions provide additional features (like application menus) found only on
  22. some systems. They may be OS- or architecture-dependent or custom-developed for
  23. particular customer.
  24. '''
  25. import pkg_resources
  26. import qubes.events
  27. class Extension(object):
  28. '''Base class for all extensions
  29. ''' # pylint: disable=too-few-public-methods
  30. def __new__(cls):
  31. if '_instance' not in cls.__dict__:
  32. cls._instance = super(Extension, cls).__new__(cls)
  33. for name in cls.__dict__:
  34. attr = getattr(cls._instance, name)
  35. if not qubes.events.ishandler(attr):
  36. continue
  37. if attr.ha_vm is not None:
  38. for event in attr.ha_events:
  39. attr.ha_vm.__handlers__[event].add(attr)
  40. else:
  41. # global hook
  42. for event in attr.ha_events:
  43. # pylint: disable=no-member
  44. qubes.Qubes.__handlers__[event].add(attr)
  45. return cls._instance
  46. def get_extensions():
  47. return set(ext.load()()
  48. for ext in pkg_resources.iter_entry_points('qubes.ext'))
  49. def handler(*events, **kwargs):
  50. '''Event handler decorator factory.
  51. To hook an event, decorate a method in your plugin class with this
  52. decorator. You may hook both per-vm-class and global events.
  53. .. note::
  54. This decorator is intended only for extensions! For regular use in the
  55. core, see :py:func:`qubes.events.handler`.
  56. :param str event: event type
  57. :param type vm: VM to hook (leave as None to hook all VMs)
  58. :param bool system: when :py:obj:`True`, hook is system-wide (not attached \
  59. to any VM)
  60. '''
  61. def decorator(func):
  62. func.ha_events = events
  63. if kwargs.get('system', False):
  64. func.ha_vm = None
  65. elif 'vm' in kwargs:
  66. func.ha_vm = kwargs['vm']
  67. else:
  68. func.ha_vm = qubes.vm.BaseVM
  69. return func
  70. return decorator