From 540942de475706143f1c86362e96c9fa6eb461bd Mon Sep 17 00:00:00 2001 From: Wojtek Porczyk Date: Mon, 11 Apr 2016 14:52:01 +0200 Subject: [PATCH] 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. --- qubes/__init__.py | 4 +--- qubes/ext/__init__.py | 37 ++++++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/qubes/__init__.py b/qubes/__init__.py index 433749dd..a5e5c7eb 100644 --- a/qubes/__init__.py +++ b/qubes/__init__.py @@ -1186,9 +1186,7 @@ class Qubes(PropertyHolder): #: logger instance for logging global messages self.log = logging.getLogger('app') - # pylint: disable=no-member - self.extensions = set(ext.load()(self) - for ext in pkg_resources.iter_entry_points('qubes.ext')) + self._extensions = qubes.ext.get_extensions() #: collection of all VMs managed by this Qubes instance self.domains = VMCollection(self) diff --git a/qubes/ext/__init__.py b/qubes/ext/__init__.py index 789b9566..8d4c7804 100644 --- a/qubes/ext/__init__.py +++ b/qubes/ext/__init__.py @@ -29,30 +29,37 @@ some systems. They may be OS- or architecture-dependent or custom-developed for particular customer. ''' +import pkg_resources import qubes.events class Extension(object): '''Base class for all extensions + ''' # pylint: disable=too-few-public-methods - :param qubes.Qubes app: application object - ''' # pylint: disable=too-few-public-methods + def __new__(cls): + if '_instance' not in cls.__dict__: + cls._instance = super(Extension, cls).__new__(cls) - def __init__(self, app): - self.app = app + for name in cls.__dict__: + attr = getattr(cls._instance, name) + if not qubes.events.ishandler(attr): + continue - for name in dir(self): - attr = getattr(self, name) - if not qubes.events.ishandler(attr): - continue + if attr.ha_vm is not None: + for event in attr.ha_events: + attr.ha_vm.add_handler(event, attr) + else: + # global hook + for event in attr.ha_events: + qubes.Qubes.add_handler(event, attr) - if attr.ha_vm is not None: - for event in attr.ha_events: - attr.ha_vm.add_handler(event, attr) - else: - # global hook - for event in attr.ha_events: - self.app.add_handler(event, attr) + return cls._instance + + +def get_extensions(): + return set(ext.load()() + for ext in pkg_resources.iter_entry_points('qubes.ext')) def handler(*events, **kwargs):