2023-03-27 19:53:47 -05:00
|
|
|
# Python imports
|
2026-01-12 23:34:34 -06:00
|
|
|
from typing import Type, TypeVar, Any
|
2023-03-27 19:53:47 -05:00
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
2023-03-27 21:25:54 -05:00
|
|
|
|
2023-03-27 19:53:47 -05:00
|
|
|
class SingletonError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-12 23:34:34 -06:00
|
|
|
T = TypeVar('T', bound='Singleton')
|
|
|
|
|
|
2023-03-27 19:53:47 -05:00
|
|
|
class Singleton:
|
2025-10-21 22:20:06 -05:00
|
|
|
_instance = None
|
2023-03-27 19:53:47 -05:00
|
|
|
|
2026-01-12 23:34:34 -06:00
|
|
|
def __new__(cls: Type[T], *args: Any, **kwargs: Any) -> T:
|
2025-12-28 19:53:05 -06:00
|
|
|
if cls._instance is not None:
|
|
|
|
|
logger.debug(f"'{cls.__name__}' is a Singleton. Returning instance...")
|
|
|
|
|
return cls._instance
|
2023-03-27 19:53:47 -05:00
|
|
|
|
2025-10-21 22:20:06 -05:00
|
|
|
cls._instance = super(Singleton, cls).__new__(cls)
|
|
|
|
|
return cls._instance
|
2025-12-28 19:53:05 -06:00
|
|
|
|
2026-01-12 23:34:34 -06:00
|
|
|
def __init__(self) -> None:
|
2025-12-28 19:53:05 -06:00
|
|
|
if self._instance is not None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
super(Singleton, self).__init__()
|