Skip to content

Commit eb6615f

Browse files
authored
Merge pull request #102 from labthings/semantic-classes
Added decorator classes for semantic annotations
2 parents 02e8773 + ef9be7a commit eb6615f

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

examples/builder.py

Lines changed: 2 additions & 1 deletion
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
from labthings.server import fields
1112

1213
from components.pdf_component import PdfComponent
@@ -34,7 +35,7 @@
3435
"magic_denoise", # Objects attribute name
3536
"/denoise", # URL to bind the property to
3637
description="A magic denoise property",
37-
schema=fields.Int(example=200), # Property should be integer formatted
38+
semtype=semantics.moz.LevelProperty(100, 500, example=200),
3839
)
3940

4041
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
@@ -6,6 +6,7 @@
66
Idempotent,
77
Semtype,
88
)
9+
from labthings.server.semantics.base import Semantic
910
from . import View, ActionView, PropertyView
1011
from .. import fields
1112
from ..spec.utilities import compile_view_spec
@@ -78,8 +79,16 @@ def _update(self, args):
7879
generated_class
7980
)
8081

82+
# Apply semantic type last, to ensure this is always used
8183
if semtype:
82-
generated_class = Semtype(semtype)(generated_class)
84+
if isinstance(semtype, str):
85+
generated_class = Semtype(semtype)(generated_class)
86+
elif isinstance(semtype, Semantic):
87+
generated_class = semtype(generated_class)
88+
else:
89+
logging.error(
90+
"Unsupported type for semtype. Must be a string or Semantic object"
91+
)
8392

8493
# Compile the generated views spec
8594
# Useful if its being attached to something other than a LabThing instance
@@ -121,15 +130,23 @@ def _post_with_args(self, args):
121130
generated_class
122131
)
123132

124-
if semtype:
125-
generated_class = Semtype(semtype)(generated_class)
126-
127133
if safe:
128134
generated_class = Safe(generated_class)
129135

130136
if idempotent:
131137
generated_class = Idempotent(generated_class)
132138

139+
# Apply semantic type last, to ensure this is always used
140+
if semtype:
141+
if isinstance(semtype, str):
142+
generated_class = Semtype(semtype)(generated_class)
143+
elif isinstance(semtype, Semantic):
144+
generated_class = semtype(generated_class)
145+
else:
146+
logging.error(
147+
"Unsupported type for semtype. Must be a string or Semantic object"
148+
)
149+
133150
# Compile the generated views spec
134151
# Useful if its being attached to something other than a LabThing instance
135152
compile_view_spec(generated_class)

0 commit comments

Comments
 (0)