Skip to content

Commit 15c364a

Browse files
author
Joel Collins
committed
Add extension property to views and added LABTHINGS_EXTENSIONS class list loader
1 parent 3b6e9af commit 15c364a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/labthings/extensions.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ def add_view(self, view_class, *urls, endpoint=None, **kwargs):
109109
for url in cleaned_urls:
110110
self._rules[url] = self._views[endpoint]
111111

112+
# Store this extension name as the View owner
113+
view_class._parent_extension_name = self.name
114+
112115
def on_register(self, function, args=None, kwargs=None):
113116
"""
114117
@@ -262,11 +265,12 @@ def find_extensions_in_file(extension_path: str, module_name="extensions") -> li
262265
)
263266
return []
264267
else:
268+
# TODO: Add documentation links to warnings
265269
if hasattr(mod, "LABTHINGS_EXTENSIONS"):
266270
return [
267-
ext_obj
268-
for ext_obj in getattr(mod, "LABTHINGS_EXTENSIONS")
269-
if isinstance(ext_obj, BaseExtension)
271+
ext_class()
272+
for ext_class in getattr(mod, "LABTHINGS_EXTENSIONS")
273+
if issubclass(ext_class, BaseExtension)
270274
]
271275
elif hasattr(mod, "__extensions__"):
272276
logging.warning(

src/labthings/views/__init__.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ..actions.pool import Pool
1010
from ..deque import Deque
11-
from ..find import current_labthing
11+
from ..find import current_labthing, find_extension
1212
from ..marshalling import marshal_with, use_args
1313
from ..representations import DEFAULT_REPRESENTATIONS
1414
from ..schema import ActionSchema, EventSchema, Schema, build_action_schema
@@ -40,6 +40,11 @@ class View(MethodView):
4040
_cls_tags: Set[str] = set() # Class tags that shouldn't be removed
4141
_opmap: Dict[str, str] = {} # Mapping of Thing Description ops to class methods
4242

43+
# Name of parent extension, if one exists.
44+
# This is only used for extension development where Views are added to the extension.
45+
# We store the name instead of the object itself to prevent circular references.
46+
_parent_extension_name: Optional[str] = None
47+
4348
def __init__(self, *args, **kwargs):
4449
MethodView.__init__(self, *args, **kwargs)
4550

@@ -50,6 +55,12 @@ def __init__(self, *args, **kwargs):
5055
else DEFAULT_REPRESENTATIONS
5156
)
5257

58+
@property
59+
def extension(self):
60+
if self._parent_extension_name:
61+
return find_extension(self._parent_extension_name)
62+
return None
63+
5364
@classmethod
5465
def get_tags(cls):
5566
""" """
@@ -257,7 +268,7 @@ class EventView(View):
257268

258269
# Internal
259270
_opmap = {
260-
"subscribeevent": "get",
271+
"subscribeevent": "get"
261272
} # Mapping of Thing Description ops to class methods
262273
_cls_tags = {"events"}
263274
_deque = Deque() # Action queue

0 commit comments

Comments
 (0)