diff --git a/fishy/helper/depless.py b/fishy/helper/depless.py new file mode 100644 index 0000000..d29a054 --- /dev/null +++ b/fishy/helper/depless.py @@ -0,0 +1,20 @@ +""" +no imports from fishy itself here, or anything which depends on fishy +""" + + +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 diff --git a/fishy/osservices/os_services.py b/fishy/osservices/os_services.py index 03f5a9a..e6387c0 100644 --- a/fishy/osservices/os_services.py +++ b/fishy/osservices/os_services.py @@ -6,6 +6,7 @@ from abc import ABC, abstractmethod from typing import Tuple, Optional import platform +from fishy.helper.depless import singleton_proxy class IOSServices(ABC): @@ -59,26 +60,6 @@ class IOSServices(ABC): """ -# 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 - - @singleton_proxy("_instance") class os_services: _instance: Optional[IOSServices] = None