2025-12-28 19:53:05 -06:00
|
|
|
# Python imports
|
2026-01-12 23:34:34 -06:00
|
|
|
from typing import Type, TypeVar, Any
|
2025-12-28 19:53:05 -06:00
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SingletonError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-01-12 23:34:34 -06:00
|
|
|
T = TypeVar('T', bound='SingletonRaised')
|
|
|
|
|
|
2025-12-28 19:53:05 -06:00
|
|
|
class SingletonRaised:
|
|
|
|
|
_instance = None
|
|
|
|
|
|
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:
|
|
|
|
|
raise SingletonError(f"'{cls.__name__}' is a Singleton. Cannot create a new instance...")
|
|
|
|
|
|
2026-01-12 23:34:34 -06:00
|
|
|
cls._instance = super(SingletonRaised, cls).__new__(cls)
|
2025-12-28 19:53:05 -06:00
|
|
|
return cls._instance
|
|
|
|
|
|
2026-01-12 23:34:34 -06:00
|
|
|
def __init__(self) -> None:
|
|
|
|
|
if self._instance is not None:
|
2025-12-28 19:53:05 -06:00
|
|
|
return
|