mirror of
https://github.com/fishyboteso/fishyboteso.git
synced 2024-08-30 18:32:13 +00:00
moved singleton_proxy to helper.depless
This commit is contained in:
parent
5fb58d9998
commit
788e78b9bb
20
fishy/helper/depless.py
Normal file
20
fishy/helper/depless.py
Normal file
@ -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
|
@ -6,6 +6,7 @@ from abc import ABC, abstractmethod
|
|||||||
from typing import Tuple, Optional
|
from typing import Tuple, Optional
|
||||||
import platform
|
import platform
|
||||||
|
|
||||||
|
from fishy.helper.depless import singleton_proxy
|
||||||
|
|
||||||
|
|
||||||
class IOSServices(ABC):
|
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")
|
@singleton_proxy("_instance")
|
||||||
class os_services:
|
class os_services:
|
||||||
_instance: Optional[IOSServices] = None
|
_instance: Optional[IOSServices] = None
|
||||||
|
Loading…
Reference in New Issue
Block a user