Skip to content

Commit d4ecd46

Browse files
committed
Added semantic annotation support to view builder
1 parent 797388d commit d4ecd46

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

examples/builder.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88

99
from labthings.server.quick import create_app
10+
from labthings.server import semantics
1011

1112
from components.pdf_component import PdfComponent
1213

@@ -33,6 +34,7 @@
3334
"magic_denoise", # Objects attribute name
3435
"/denoise", # URL to bind the property to
3536
description="A magic denoise property",
37+
semtype=semantics.moz.LevelProperty(100, 500, example=200),
3638
)
3739

3840
labthing.build_property(
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from .. import decorators
2+
from .. import fields
3+
4+
5+
class Semantic:
6+
pass
7+
8+
9+
# BASIC PROPERTIES
10+
class Property(Semantic):
11+
def __init__(self, schema):
12+
self.schema = schema
13+
14+
def __call__(self, viewcls):
15+
# Use the class name as the semantic type
16+
viewcls = decorators.Semtype(self.__class__.__name__)(viewcls)
17+
viewcls = decorators.PropertySchema(self.schema)(viewcls)
18+
return viewcls

src/labthings/server/semantics/moz.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
from .. import decorators
21
from .. import fields
2+
from .base import Property
33

44
# BASIC PROPERTIES
5-
class Property:
6-
def __init__(self, schema):
7-
self.schema = schema
8-
9-
def __call__(self, viewcls):
10-
# Use the class name as the semantic type
11-
viewcls = decorators.Semtype(self.__class__.__name__)(viewcls)
12-
viewcls = decorators.PropertySchema(self.schema)(viewcls)
13-
return viewcls
14-
15-
165
class BooleanProperty(Property):
176
"""https://iot.mozilla.org/schemas/#BooleanProperty"""
187

src/labthings/server/view/builder.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
Idempotent,
1212
Semtype,
1313
)
14+
from labthings.server.semantics.base import Semantic
1415
from . import View, ActionView, PropertyView
1516
from ..spec.utilities import compile_view_spec
1617

@@ -86,8 +87,16 @@ def _update(self, args):
8687
generated_class
8788
)
8889

90+
# Apply semantic type last, to ensure this is always used
8991
if semtype:
90-
generated_class = Semtype(semtype)(generated_class)
92+
if isinstance(semtype, str):
93+
generated_class = Semtype(semtype)(generated_class)
94+
elif isinstance(semtype, Semantic):
95+
generated_class = semtype(generated_class)
96+
else:
97+
logging.error(
98+
"Unsupported type for semtype. Must be a string or Semantic object"
99+
)
91100

92101
# Compile the generated views spec
93102
# Useful if its being attached to something other than a LabThing instance
@@ -128,15 +137,23 @@ def _post(self, args):
128137
generated_class
129138
)
130139

131-
if semtype:
132-
generated_class = Semtype(semtype)(generated_class)
133-
134140
if safe:
135141
generated_class = Safe(generated_class)
136142

137143
if idempotent:
138144
generated_class = Idempotent(generated_class)
139145

146+
# Apply semantic type last, to ensure this is always used
147+
if semtype:
148+
if isinstance(semtype, str):
149+
generated_class = Semtype(semtype)(generated_class)
150+
elif isinstance(semtype, Semantic):
151+
generated_class = semtype(generated_class)
152+
else:
153+
logging.error(
154+
"Unsupported type for semtype. Must be a string or Semantic object"
155+
)
156+
140157
# Compile the generated views spec
141158
# Useful if its being attached to something other than a LabThing instance
142159
compile_view_spec(generated_class)

0 commit comments

Comments
 (0)