Added Singleton class to inherit as needed

This commit is contained in:
itdominator 2023-03-27 19:53:47 -05:00
parent 48d6c505a7
commit ef03030c88
6 changed files with 33 additions and 9 deletions

View File

@ -3,11 +3,11 @@
# Lib imports # Lib imports
# Application imports # Application imports
from .singleton import Singleton
class EndpointRegistry(Singleton):
class EndpointRegistry():
def __init__(self): def __init__(self):
self._endpoints = {} self._endpoints = {}

View File

@ -4,11 +4,11 @@ from collections import defaultdict
# Lib imports # Lib imports
# Application imports # Application imports
from .singleton import Singleton
class EventSystem(Singleton):
class EventSystem:
""" Create event system. """ """ Create event system. """
def __init__(self): def __init__(self):

View File

@ -7,7 +7,7 @@ gi.require_version('Gdk', '3.0')
from gi.repository import Gdk from gi.repository import Gdk
# Application imports # Application imports
from .singleton import Singleton
@ -19,7 +19,7 @@ class KeymapError(Exception):
""" Custom exception for errors in keybinding configurations """ """ Custom exception for errors in keybinding configurations """
MODIFIER = re.compile('<([^<]+)>') MODIFIER = re.compile('<([^<]+)>')
class Keybindings: class Keybindings(Singleton):
""" Class to handle loading and lookup of Terminator keybindings """ """ Class to handle loading and lookup of Terminator keybindings """
modifiers = { modifiers = {

View File

@ -5,11 +5,11 @@ import logging
# Lib imports # Lib imports
# Application imports # Application imports
from .singleton import Singleton
class Logger(Singleton):
class Logger:
""" """
Create a new logging object and return it. Create a new logging object and return it.
:note: :note:

View File

@ -6,6 +6,7 @@ import inspect
# Lib imports # Lib imports
# Application imports # Application imports
from ..singleton import Singleton
from .start_check_mixin import StartCheckMixin from .start_check_mixin import StartCheckMixin
@ -14,7 +15,7 @@ class MissingConfigError(Exception):
class Settings(StartCheckMixin): class Settings(StartCheckMixin, Singleton):
def __init__(self): def __init__(self):
self._SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__)) self._SCRIPT_PTH = os.path.dirname(os.path.realpath(__file__))
self._USER_HOME = os.path.expanduser('~') self._USER_HOME = os.path.expanduser('~')

23
src/utils/singleton.py Normal file
View File

@ -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