Skip to content

Commit 439d999

Browse files
author
Joel Collins
committed
Converted view class tags to set()
1 parent 4240fbf commit 439d999

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

labthings/server/decorators.py

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

99
from marshmallow import Schema as _Schema
1010

11-
from .spec.utilities import update_spec
11+
from .spec.utilities import update_spec, tag_spec
1212
from .schema import TaskSchema, Schema, FieldSchema
1313
from .fields import Field
1414
from .view import View
@@ -100,8 +100,7 @@ def ThingAction(viewcls: View):
100100
View: View class with Action spec tags
101101
"""
102102
# Update Views API spec
103-
update_spec(viewcls, {"tags": ["actions"]})
104-
update_spec(viewcls, {"_groups": ["actions"]})
103+
tag_spec(viewcls, "actions")
105104
return viewcls
106105

107106

@@ -173,8 +172,7 @@ def wrapped(*args, **kwargs):
173172
viewcls.put = property_notify(viewcls.put)
174173

175174
# Update Views API spec
176-
update_spec(viewcls, {"tags": ["properties"]})
177-
update_spec(viewcls, {"_groups": ["properties"]})
175+
tag_spec(viewcls, "properties")
178176
return viewcls
179177

180178

@@ -281,16 +279,11 @@ def __call__(self, f):
281279

282280
class Tag:
283281
def __init__(self, tags):
284-
if isinstance(tags, str):
285-
self.tags = [tags]
286-
elif isinstance(tags, list) and all([isinstance(e, str) for e in tags]):
287-
self.tags = tags
288-
else:
289-
raise TypeError("Tags must be a string or list of strings")
282+
self.tags = tags
290283

291284
def __call__(self, f):
292285
# Pass params to call function attribute for external access
293-
update_spec(f, {"tags": self.tags})
286+
tag_spec(f, self.tags)
294287
return f
295288

296289

labthings/server/labthing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,11 +303,11 @@ def _register_view(self, app, view, *urls, endpoint=None, **kwargs):
303303

304304
# Handle resource groups listed in API spec
305305
view_spec = get_spec(view)
306-
view_groups = view_spec.get("_groups", [])
307-
if "actions" in view_groups:
306+
view_tags = view_spec.get("tags", set())
307+
if "actions" in view_tags:
308308
self.thing_description.action(flask_rules, view)
309309
self._action_views[view.endpoint] = view
310-
if "properties" in view_groups:
310+
if "properties" in view_tags:
311311
self.thing_description.property(flask_rules, view)
312312
self._property_views[view.endpoint] = view
313313

labthings/server/spec/apispec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ def build_spec(view, inherit_from=None):
168168
)
169169

170170
# Build tags
171-
tags = getattr(view, "__apispec__").get("tags", [])
172-
tags.extend(inherited_spec.get("tags", []))
171+
tags = getattr(view, "__apispec__").get("tags", set())
172+
tags = tags.union(inherited_spec.get("tags", set()))
173173

174174
return update_spec(
175175
view, {"description": description, "summary": summary, "tags": tags}

labthings/server/spec/utilities.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ def update_spec(obj, spec: dict):
2121
return obj.__apispec__ or {}
2222

2323

24+
def tag_spec(obj, tags, add_group: bool = True):
25+
obj.__apispec__ = obj.__dict__.get("__apispec__", {})
26+
27+
if "tags" not in obj.__apispec__:
28+
obj.__apispec__["tags"] = set()
29+
30+
if isinstance(tags, set) or isinstance(tags, list):
31+
obj.__apispec__["tags"] = obj.__apispec__["tags"].union(tags)
32+
else:
33+
obj.__apispec__["tags"].add(tags)
34+
35+
2436
def get_spec(obj):
2537
"""
2638
Get the __apispec__ dictionary, created by LabThings decorators,

0 commit comments

Comments
 (0)