__init__.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. for event in attr.ha_events:
  54. attr.ha_vm.add_handler(event, attr)
  55. else:
  56. # global hook
  57. for event in attr.ha_events:
  58. self.app.add_handler(event, attr)
  59. def handler(*events, **kwargs):
  60. '''Event handler decorator factory.
  61. To hook an event, decorate a method in your plugin class with this
  62. decorator. You may hook both per-vm-class and global events.
  63. .. note::
  64. This decorator is intended only for extensions! For regular use in the
  65. core, see :py:func:`qubes.events.handler`.
  66. :param str event: event type
  67. :param type vm: VM to hook (leave as None to hook all VMs)
  68. :param bool system: when :py:obj:`True`, hook is system-wide (not attached \
  69. to any VM)
  70. '''
  71. def decorator(func):
  72. func.ha_events = events
  73. if kwargs.get('system', False):
  74. func.ha_vm = None
  75. elif 'vm' in kwargs:
  76. func.ha_vm = kwargs['vm']
  77. else:
  78. func.ha_vm = qubes.vm.BaseVM
  79. return func
  80. return decorator
  81. __all__ = ['Extension', 'ExtensionPlugin', 'handler'] \
  82. + qubes.plugins.load(__file__)