From a5499475f6d95c20d96238bf32071839af7dc484 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Wed, 22 Feb 2023 00:56:22 +0530 Subject: [PATCH] generalized ClassInstance even more to be used as a decorator --- fishy/osservices/os_services.py | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/fishy/osservices/os_services.py b/fishy/osservices/os_services.py index 3260ce7..e4c52bb 100644 --- a/fishy/osservices/os_services.py +++ b/fishy/osservices/os_services.py @@ -1,4 +1,7 @@ +import inspect import logging +import re +import sys from abc import ABC, abstractmethod from typing import Tuple, Optional import platform @@ -55,12 +58,28 @@ class IOSServices(ABC): """ -class ClassInstance(type): - def __getattr__(cls, name): - return getattr(cls._instance, name) +# todo move this into helper and use for config and similar places +# but make sure other fishy stuff is not imported while importing helper +# to do that, move everything which uses fishy stuff into a different helper script +def singleton_proxy(instance_name): + def decorator(root_cls): + if not hasattr(root_cls, instance_name): + raise AttributeError(f"{instance_name} not found in {root_cls}") + + class SingletonProxy(type): + def __getattr__(cls, name): + return getattr(getattr(cls, instance_name), name) + + class NewClass(root_cls, metaclass=SingletonProxy): + ... + + return NewClass + + return decorator -class os_services(metaclass=ClassInstance): +@singleton_proxy("_instance") +class os_services: _instance: Optional[IOSServices] = None @staticmethod