From 15c364a9f01104c1f732dbdf6e4e7457e7438e80 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Mon, 30 Nov 2020 15:29:21 +0000 Subject: [PATCH] Add extension property to views and added LABTHINGS_EXTENSIONS class list loader --- src/labthings/extensions.py | 10 +++++++--- src/labthings/views/__init__.py | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/labthings/extensions.py b/src/labthings/extensions.py index 034ed161..df7b092d 100644 --- a/src/labthings/extensions.py +++ b/src/labthings/extensions.py @@ -109,6 +109,9 @@ def add_view(self, view_class, *urls, endpoint=None, **kwargs): for url in cleaned_urls: self._rules[url] = self._views[endpoint] + # Store this extension name as the View owner + view_class._parent_extension_name = self.name + def on_register(self, function, args=None, kwargs=None): """ @@ -262,11 +265,12 @@ def find_extensions_in_file(extension_path: str, module_name="extensions") -> li ) return [] else: + # TODO: Add documentation links to warnings if hasattr(mod, "LABTHINGS_EXTENSIONS"): return [ - ext_obj - for ext_obj in getattr(mod, "LABTHINGS_EXTENSIONS") - if isinstance(ext_obj, BaseExtension) + ext_class() + for ext_class in getattr(mod, "LABTHINGS_EXTENSIONS") + if issubclass(ext_class, BaseExtension) ] elif hasattr(mod, "__extensions__"): logging.warning( diff --git a/src/labthings/views/__init__.py b/src/labthings/views/__init__.py index 9449dcba..8db36281 100644 --- a/src/labthings/views/__init__.py +++ b/src/labthings/views/__init__.py @@ -8,7 +8,7 @@ from ..actions.pool import Pool from ..deque import Deque -from ..find import current_labthing +from ..find import current_labthing, find_extension from ..marshalling import marshal_with, use_args from ..representations import DEFAULT_REPRESENTATIONS from ..schema import ActionSchema, EventSchema, Schema, build_action_schema @@ -40,6 +40,11 @@ class View(MethodView): _cls_tags: Set[str] = set() # Class tags that shouldn't be removed _opmap: Dict[str, str] = {} # Mapping of Thing Description ops to class methods + # Name of parent extension, if one exists. + # This is only used for extension development where Views are added to the extension. + # We store the name instead of the object itself to prevent circular references. + _parent_extension_name: Optional[str] = None + def __init__(self, *args, **kwargs): MethodView.__init__(self, *args, **kwargs) @@ -50,6 +55,12 @@ def __init__(self, *args, **kwargs): else DEFAULT_REPRESENTATIONS ) + @property + def extension(self): + if self._parent_extension_name: + return find_extension(self._parent_extension_name) + return None + @classmethod def get_tags(cls): """ """ @@ -257,7 +268,7 @@ class EventView(View): # Internal _opmap = { - "subscribeevent": "get", + "subscribeevent": "get" } # Mapping of Thing Description ops to class methods _cls_tags = {"events"} _deque = Deque() # Action queue