Philipp Hörist pushed to branch master at gajim / gajim
Commits: d8beb018 by Philipp Hörist at 2019-04-13T23:58:45Z Refactor Singleton class
- - - - -
3 changed files:
- gajim/common/helpers.py - gajim/plugins/helpers.py - gajim/plugins/pluginmanager.py
Changes:
===================================== gajim/common/helpers.py ===================================== @@ -1520,3 +1520,11 @@ def save_roster_position(window): log.debug('Save roster position: %s %s', x_pos, y_pos) app.config.set('roster_x-position', x_pos) app.config.set('roster_y-position', y_pos) + + +class Singleton(type): + _instances = {} + def __call__(cls, *args, **kwargs): + if cls not in cls._instances: + cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) + return cls._instances[cls]
===================================== gajim/plugins/helpers.py ===================================== @@ -21,7 +21,7 @@ Helper code related to plug-ins management system. :license: GPL '''
-__all__ = ['log', 'log_calls', 'Singleton'] +__all__ = ['log', 'log_calls']
from typing import List
@@ -115,26 +115,6 @@ class log_calls:
return wrapper
-class Singleton(type): - ''' - Singleton metaclass. - ''' - def __init__(cls, name, bases, dic): - super(Singleton, cls).__init__(name, bases, dic) - cls.instance = None - - def __call__(cls, *args, **kwargs): - if cls.instance is None: - cls.instance = super(Singleton, cls).__call__(*args, **kwargs) - #log.debug('%(classname)s - new instance created'%{ - #'classname' : cls.__name__}) - else: - pass - #log.debug('%(classname)s - returning already existing instance'%{ - #'classname' : cls.__name__}) - - return cls.instance -
def get_builder(file_name: str, widgets: List[str] = None) -> Builder: return Builder(file_name,
===================================== gajim/plugins/pluginmanager.py ===================================== @@ -38,9 +38,10 @@ from gajim.common import configpaths from gajim.common import modules from gajim.common.i18n import _ from gajim.common.exceptions import PluginsystemError +from gajim.common.helpers import Singleton from gajim.plugins import plugins_i18n
-from gajim.plugins.helpers import log, log_calls, Singleton +from gajim.plugins.helpers import log, log_calls from gajim.plugins.helpers import GajimPluginActivateException from gajim.plugins.gajimplugin import GajimPlugin, GajimPluginException
@@ -73,8 +74,6 @@ class PluginManager(metaclass=Singleton): 'destructors' (classes that register GUI extension points) '''
- __metaclass__ = Singleton - #@log_calls('PluginManager') def __init__(self): self.plugins = []
View it on GitLab: https://dev.gajim.org/gajim/gajim/commit/d8beb0183c51ea3286412b80ca28a587903...