Skip to content

Commit

Permalink
Add extension property to views and added LABTHINGS_EXTENSIONS class …
Browse files Browse the repository at this point in the history
…list loader
  • Loading branch information
Joel Collins committed Nov 30, 2020
1 parent 3b6e9af commit 15c364a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/labthings/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 13 additions & 2 deletions src/labthings/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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):
""" """
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 15c364a

Please sign in to comment.