Refactor Singleton class
This commit is contained in:
		
							parent
							
								
									d83f725578
								
							
						
					
					
						commit
						d8beb0183c
					
				
					 3 changed files with 11 additions and 24 deletions
				
			
		| 
						 | 
					@ -1520,3 +1520,11 @@ def save_roster_position(window):
 | 
				
			||||||
    log.debug('Save roster position: %s %s', x_pos, y_pos)
 | 
					    log.debug('Save roster position: %s %s', x_pos, y_pos)
 | 
				
			||||||
    app.config.set('roster_x-position', x_pos)
 | 
					    app.config.set('roster_x-position', x_pos)
 | 
				
			||||||
    app.config.set('roster_y-position', y_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]
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,7 +21,7 @@ Helper code related to plug-ins management system.
 | 
				
			||||||
:license: GPL
 | 
					:license: GPL
 | 
				
			||||||
'''
 | 
					'''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__all__ = ['log', 'log_calls', 'Singleton']
 | 
					__all__ = ['log', 'log_calls']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from typing import List
 | 
					from typing import List
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -115,26 +115,6 @@ class log_calls:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return wrapper
 | 
					        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:
 | 
					def get_builder(file_name: str, widgets: List[str] = None) -> Builder:
 | 
				
			||||||
    return Builder(file_name,
 | 
					    return Builder(file_name,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,9 +38,10 @@ from gajim.common import configpaths
 | 
				
			||||||
from gajim.common import modules
 | 
					from gajim.common import modules
 | 
				
			||||||
from gajim.common.i18n import _
 | 
					from gajim.common.i18n import _
 | 
				
			||||||
from gajim.common.exceptions import PluginsystemError
 | 
					from gajim.common.exceptions import PluginsystemError
 | 
				
			||||||
 | 
					from gajim.common.helpers import Singleton
 | 
				
			||||||
from gajim.plugins import plugins_i18n
 | 
					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.helpers import GajimPluginActivateException
 | 
				
			||||||
from gajim.plugins.gajimplugin import GajimPlugin, GajimPluginException
 | 
					from gajim.plugins.gajimplugin import GajimPlugin, GajimPluginException
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -73,8 +74,6 @@ class PluginManager(metaclass=Singleton):
 | 
				
			||||||
               'destructors' (classes that register GUI extension points)
 | 
					               'destructors' (classes that register GUI extension points)
 | 
				
			||||||
    '''
 | 
					    '''
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    __metaclass__ = Singleton
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    #@log_calls('PluginManager')
 | 
					    #@log_calls('PluginManager')
 | 
				
			||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
        self.plugins = []
 | 
					        self.plugins = []
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		
		Reference in a new issue