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:
|
2026-02-28 01:10:28 -06:00
|
|
|
__instance = None
|
2025-12-28 19:53:05 -06:00
|
|
|
|
2026-01-12 23:34:34 -06:00
|
|
|
def __new__(cls: Type[T], *args: Any, **kwargs: Any) -> T:
|
2026-02-28 01:10:28 -06:00
|
|
|
if cls.__instance is not None:
|
2025-12-28 19:53:05 -06:00
|
|
|
raise SingletonError(f"'{cls.__name__}' is a Singleton. Cannot create a new instance...")
|
|
|
|
|
|
2026-02-28 01:10:28 -06:00
|
|
|
cls.__instance = super(SingletonRaised, 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:
|
2026-02-28 01:10:28 -06:00
|
|
|
if self.__instance is not None:
|
2025-12-28 19:53:05 -06:00
|
|
|
return
|