__init__.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program 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
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. '''Qubes extensions.
  22. Extensions provide additional features (like application menus) found only on
  23. some systems. They may be OS- or architecture-dependent or custom-developed for
  24. particular customer.
  25. '''
  26. import pkg_resources
  27. import qubes.events
  28. class Extension(object):
  29. '''Base class for all extensions
  30. ''' # pylint: disable=too-few-public-methods
  31. def __new__(cls):
  32. if '_instance' not in cls.__dict__:
  33. cls._instance = super(Extension, cls).__new__(cls)
  34. for name in cls.__dict__:
  35. attr = getattr(cls._instance, name)
  36. if not qubes.events.ishandler(attr):
  37. continue
  38. if attr.ha_vm is not None:
  39. for event in attr.ha_events:
  40. attr.ha_vm.__handlers__[event].add(attr)
  41. else:
  42. # global hook
  43. for event in attr.ha_events:
  44. # pylint: disable=no-member
  45. qubes.Qubes.__handlers__[event].add(attr)
  46. return cls._instance
  47. def get_extensions():
  48. return set(ext.load()()
  49. for ext in pkg_resources.iter_entry_points('qubes.ext'))
  50. def handler(*events, **kwargs):
  51. '''Event handler decorator factory.
  52. To hook an event, decorate a method in your plugin class with this
  53. decorator. You may hook both per-vm-class and global events.
  54. .. note::
  55. This decorator is intended only for extensions! For regular use in the
  56. core, see :py:func:`qubes.events.handler`.
  57. :param str event: event type
  58. :param type vm: VM to hook (leave as None to hook all VMs)
  59. :param bool system: when :py:obj:`True`, hook is system-wide (not attached \
  60. to any VM)
  61. '''
  62. def decorator(func):
  63. func.ha_events = events
  64. if kwargs.get('system', False):
  65. func.ha_vm = None
  66. elif 'vm' in kwargs:
  67. func.ha_vm = kwargs['vm']
  68. else:
  69. func.ha_vm = qubes.vm.BaseVM
  70. return func
  71. return decorator