__init__.py 2.9 KB

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