qubes/ext: convert extensions to singletons

From now the extensions are instantiated once. They no longer have .app
attribute, but can access it from event handlers via vm.app.
This commit is contained in:
Wojtek Porczyk 2016-04-11 14:52:01 +02:00 committed by Marek Marczykowski-Górecki
parent eaf5efd814
commit 540942de47
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 23 additions and 18 deletions

View File

@ -1186,9 +1186,7 @@ class Qubes(PropertyHolder):
#: logger instance for logging global messages #: logger instance for logging global messages
self.log = logging.getLogger('app') self.log = logging.getLogger('app')
# pylint: disable=no-member self._extensions = qubes.ext.get_extensions()
self.extensions = set(ext.load()(self)
for ext in pkg_resources.iter_entry_points('qubes.ext'))
#: collection of all VMs managed by this Qubes instance #: collection of all VMs managed by this Qubes instance
self.domains = VMCollection(self) self.domains = VMCollection(self)

View File

@ -29,30 +29,37 @@ some systems. They may be OS- or architecture-dependent or custom-developed for
particular customer. particular customer.
''' '''
import pkg_resources
import qubes.events import qubes.events
class Extension(object): class Extension(object):
'''Base class for all extensions '''Base class for all extensions
''' # pylint: disable=too-few-public-methods
:param qubes.Qubes app: application object def __new__(cls):
''' # pylint: disable=too-few-public-methods if '_instance' not in cls.__dict__:
cls._instance = super(Extension, cls).__new__(cls)
def __init__(self, app): for name in cls.__dict__:
self.app = app attr = getattr(cls._instance, name)
if not qubes.events.ishandler(attr):
continue
for name in dir(self): if attr.ha_vm is not None:
attr = getattr(self, name) for event in attr.ha_events:
if not qubes.events.ishandler(attr): attr.ha_vm.add_handler(event, attr)
continue else:
# global hook
for event in attr.ha_events:
qubes.Qubes.add_handler(event, attr)
if attr.ha_vm is not None: return cls._instance
for event in attr.ha_events:
attr.ha_vm.add_handler(event, attr)
else: def get_extensions():
# global hook return set(ext.load()()
for event in attr.ha_events: for ext in pkg_resources.iter_entry_points('qubes.ext'))
self.app.add_handler(event, attr)
def handler(*events, **kwargs): def handler(*events, **kwargs):