__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. #
  23. '''Qubes extensions.
  24. Extensions provide additional features (like application menus) found only on
  25. some systems. They may be OS- or architecture-dependent or custom-developed for
  26. particular customer.
  27. '''
  28. import inspect
  29. import qubes.events
  30. import qubes.plugins
  31. class ExtensionPlugin(qubes.plugins.Plugin):
  32. '''Metaclass for :py:class:`Extension`'''
  33. def __init__(cls, name, bases, dict_):
  34. super(ExtensionPlugin, cls).__init__(name, bases, dict_)
  35. cls._instance = None
  36. def __call__(cls, *args, **kwargs):
  37. if cls._instance is None:
  38. cls._instance = super(ExtensionPlugin, cls).__call__(
  39. *args, **kwargs)
  40. return cls._instance
  41. class Extension(object):
  42. '''Base class for all extensions
  43. :param qubes.Qubes app: application object
  44. ''' # pylint: disable=too-few-public-methods
  45. __metaclass__ = ExtensionPlugin
  46. def __init__(self, app):
  47. self.app = app
  48. for name in dir(self):
  49. attr = getattr(self, name)
  50. if not qubes.events.ishandler(attr):
  51. continue
  52. if attr.ha_vm is not None:
  53. attr.ha_vm.add_hook(attr.ha_event, attr)
  54. else:
  55. # global hook
  56. self.app.add_hook(attr.ha_event, attr)
  57. def handler(*events, **kwargs):
  58. '''Event handler decorator factory.
  59. To hook an event, decorate a method in your plugin class with this
  60. decorator. You may hook both per-vm-class and global events.
  61. .. note::
  62. This decorator is intended only for extensions! For regular use in the
  63. core, see :py:func:`qubes.events.handler`.
  64. :param str event: event type
  65. :param type vm: VM to hook (leave as None to hook all VMs)
  66. :param bool system: when :py:obj:`True`, hook is system-wide (not attached \
  67. to any VM)
  68. '''
  69. def decorator(func):
  70. func.ha_events = events
  71. if kwargs.get('system', False):
  72. func.ha_vm = None
  73. elif 'vm' in kwargs:
  74. func.ha_vm = kwargs['vm']
  75. else:
  76. func.ha_vm = qubes.vm.BaseVM
  77. return func
  78. return decorator
  79. __all__ = ['Extension', 'ExtensionPlugin', 'handler'] \
  80. + qubes.plugins.load(__file__)