__init__.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 qubes.events
  29. class Extension(object):
  30. '''Base class for all extensions
  31. :param qubes.Qubes app: application object
  32. ''' # pylint: disable=too-few-public-methods
  33. def __init__(self, app):
  34. self.app = app
  35. for name in dir(self):
  36. attr = getattr(self, name)
  37. if not qubes.events.ishandler(attr):
  38. continue
  39. if attr.ha_vm is not None:
  40. for event in attr.ha_events:
  41. attr.ha_vm.add_handler(event, attr)
  42. else:
  43. # global hook
  44. for event in attr.ha_events:
  45. self.app.add_handler(event, attr)
  46. def handler(*events, **kwargs):
  47. '''Event handler decorator factory.
  48. To hook an event, decorate a method in your plugin class with this
  49. decorator. You may hook both per-vm-class and global events.
  50. .. note::
  51. This decorator is intended only for extensions! For regular use in the
  52. core, see :py:func:`qubes.events.handler`.
  53. :param str event: event type
  54. :param type vm: VM to hook (leave as None to hook all VMs)
  55. :param bool system: when :py:obj:`True`, hook is system-wide (not attached \
  56. to any VM)
  57. '''
  58. def decorator(func):
  59. func.ha_events = events
  60. if kwargs.get('system', False):
  61. func.ha_vm = None
  62. elif 'vm' in kwargs:
  63. func.ha_vm = kwargs['vm']
  64. else:
  65. func.ha_vm = qubes.vm.BaseVM
  66. return func
  67. return decorator