diff --git a/src/utils/endpoint_registry.py b/src/utils/endpoint_registry.py index 15ffa9e..86e4295 100644 --- a/src/utils/endpoint_registry.py +++ b/src/utils/endpoint_registry.py @@ -3,11 +3,11 @@ # Lib imports # Application imports +from .singleton import Singleton - -class EndpointRegistry(): +class EndpointRegistry(Singleton): def __init__(self): self._endpoints = {} diff --git a/src/utils/event_system.py b/src/utils/event_system.py index 88f7299..9d876cf 100644 --- a/src/utils/event_system.py +++ b/src/utils/event_system.py @@ -4,11 +4,11 @@ from collections import defaultdict # Lib imports # Application imports +from .singleton import Singleton - -class EventSystem: +class EventSystem(Singleton): """ Create event system. """ def __init__(self): diff --git a/src/utils/keybindings.py b/src/utils/keybindings.py index cb47685..50e7b71 100644 --- a/src/utils/keybindings.py +++ b/src/utils/keybindings.py @@ -7,7 +7,7 @@ gi.require_version('Gdk', '3.0') from gi.repository import Gdk # Application imports - +from .singleton import Singleton @@ -19,7 +19,7 @@ class KeymapError(Exception): """ Custom exception for errors in keybinding configurations """ MODIFIER = re.compile('<([^<]+)>') -class Keybindings: +class Keybindings(Singleton): """ Class to handle loading and lookup of Terminator keybindings """ modifiers = { diff --git a/src/utils/logger.py b/src/utils/logger.py index 6ca2add..10e93c4 100644 --- a/src/utils/logger.py +++ b/src/utils/logger.py @@ -5,11 +5,11 @@ import logging # Lib imports # Application imports +from .singleton import Singleton - -class Logger: +class Logger(Singleton): """ Create a new logging object and return it. :note: diff --git a/src/utils/settings/settings.py b/src/utils/settings/settings.py index 823b3dd..58408e3 100644 --- a/src/utils/settings/settings.py +++ b/src/utils/settings/settings.py @@ -6,6 +6,7 @@ import inspect # Lib imports # Application imports +from ..singleton import Singleton from .start_check_mixin import StartCheckMixin @@ -14,7 +15,7 @@ class MissingConfigError(Exception): -class Settings(StartCheckMixin): +class Settings(StartCheckMixin, Singleton): def __init__(self): self._SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__)) self._USER_HOME = os.path.expanduser('~') diff --git a/src/utils/singleton.py b/src/utils/singleton.py new file mode 100644 index 0000000..ee85368 --- /dev/null +++ b/src/utils/singleton.py @@ -0,0 +1,23 @@ +# Python imports + +# Lib imports + +# Application imports + + +class SingletonError(Exception): + pass + + + +class Singleton: + ccount = 0 + + def __new__(cls, *args, **kwargs): + obj = super(Singleton, cls).__new__(cls) + cls.ccount += 1 + + if cls.ccount == 2: + raise SingletonError(f"Exceeded {cls.__name__} instantiation limit...") + + return obj