Skip to content

Commit 3cf1559

Browse files
authored
Merge pull request #110 from labthings/simpler-spec
Schemas without decorators
2 parents b62879e + cc676b7 commit 3cf1559

36 files changed

+623
-1410
lines changed

codecov.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ coverage:
33
project:
44
default:
55
# basic
6-
target: 95%
7-
threshold: 1%
6+
target: auto
7+
threshold: 5%
88
base: auto
99
patch:
1010
default:
1111
# basic
12-
target: auto
13-
threshold: 1%
12+
target: 80%
13+
threshold: 5%
1414
base: auto

examples/builder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
# Create LabThings Flask app
1717
app, labthing = create_app(
1818
__name__,
19-
prefix="/api",
2019
title="My Lab Device API",
2120
description="Test LabThing-based API",
2221
version="0.1.0",

examples/nested_thing.py

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

66
from labthings.server.quick import create_app
77
from labthings.server.decorators import ThingProperty, PropertySchema
8-
from labthings.server.view import View
8+
from labthings.server.view import PropertyView
99
from labthings.server.find import find_component
1010
from labthings.server.schema import Schema
1111
from labthings.server import fields
@@ -64,19 +64,15 @@ class MyComponentSchema(Schema):
6464
"""
6565

6666

67-
# Register this view as a Thing Property
68-
@ThingProperty
69-
# Define the data we're going to output (get), and what to expect in (post)
70-
@PropertySchema(
71-
fields.Integer(
67+
class DenoiseProperty(PropertyView):
68+
69+
schema = fields.Integer(
7270
required=True,
7371
example=200,
7472
minimum=100,
7573
maximum=500,
7674
description="Value of magic_denoise",
7775
)
78-
)
79-
class DenoiseProperty(View):
8076

8177
# Main function to handle GET requests (read)
8278
def get(self):
@@ -104,10 +100,11 @@ def post(self, new_property_value):
104100
"""
105101

106102

107-
@ThingProperty
108-
@PropertySchema(MyComponentSchema())
109-
class MyComponentProperty(View):
103+
class MyComponentProperty(PropertyView):
110104
# Main function to handle GET requests
105+
106+
schema = MyComponentSchema()
107+
111108
def get(self):
112109
"""Show the current data value"""
113110

examples/properties_dictionary.py

Lines changed: 0 additions & 99 deletions
This file was deleted.

examples/simple_extensions.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@
33
import logging
44

55
from labthings.server.quick import create_app
6-
from labthings.server.decorators import (
7-
ThingProperty,
8-
PropertySchema,
9-
ThingAction,
10-
use_args,
11-
marshal_task,
12-
doc,
13-
)
14-
from labthings.server.view import View
6+
from labthings.server.view import ActionView, PropertyView
157
from labthings.server.find import find_component
168
from labthings.server import fields
179
from labthings.core.utilities import path_relative_to
@@ -28,26 +20,16 @@
2820
"""
2921

3022

31-
@ThingAction
32-
class ExtensionMeasurementAction(View):
33-
# Expect JSON parameters in the request body.
34-
# Pass to post function as dictionary argument.
35-
@use_args(
36-
{
37-
"averages": fields.Integer(
38-
missing=10,
39-
example=10,
40-
description="Number of data sets to average over",
41-
)
42-
}
43-
)
44-
# Shorthand to say we're always going to return a Task object
45-
@marshal_task
46-
@doc(title="Averaged Measurement")
47-
# Main function to handle POST requests
48-
def post(self, args):
49-
"""Start an averaged measurement"""
23+
class ExtensionMeasurementAction(ActionView):
24+
"""Start an averaged measurement"""
25+
26+
args = {
27+
"averages": fields.Integer(
28+
missing=10, example=10, description="Number of data sets to average over",
29+
)
30+
}
5031

32+
def post(self, args):
5133
# Find our attached component
5234
my_component = find_component("org.labthings.example.mycomponent")
5335

@@ -115,17 +97,15 @@ def data(self):
11597
"""
11698

11799

118-
@ThingProperty # Register this view as a Thing Property
119-
@PropertySchema( # Define the data we're going to output (get), and what to expect in (post)
120-
fields.Integer(
100+
class DenoiseProperty(PropertyView):
101+
102+
schema = fields.Integer(
121103
required=True,
122104
example=200,
123105
minimum=100,
124106
maximum=500,
125107
description="Value of magic_denoise",
126108
)
127-
)
128-
class DenoiseProperty(View):
129109

130110
# Main function to handle GET requests (read)
131111
def get(self):
@@ -153,9 +133,10 @@ def post(self, new_property_value):
153133
"""
154134

155135

156-
@ThingProperty
157-
@PropertySchema(fields.List(fields.Float()))
158-
class QuickDataProperty(View):
136+
class QuickDataProperty(PropertyView):
137+
138+
schema = fields.List(fields.Float())
139+
159140
# Main function to handle GET requests
160141
def get(self):
161142
"""Show the current data value"""

examples/simple_thing.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import atexit
1414

1515
from labthings.server.quick import create_app
16-
from labthings.server.decorators import PropertySchema, use_args, marshal_with, Doc
1716
from labthings.server import semantics
1817
from labthings.server.view import ActionView, PropertyView
1918
from labthings.server.find import find_component
@@ -73,23 +72,17 @@ def average_data(self, n: int):
7372
"""
7473

7574

76-
# Define the data we're going to output (get), and what to expect in (post)
75+
# Wrap in a semantic annotation to autmatically set schema and args
7776
@semantics.moz.LevelProperty(100, 500, example=200)
78-
@Doc(description="Value of magic_denoise",)
7977
class DenoiseProperty(PropertyView):
78+
"""Value of magic_denoise"""
8079

81-
# Main function to handle GET requests (read)
8280
def get(self):
83-
"""Show the current magic_denoise value"""
84-
8581
# When a GET request is made, we'll find our attached component
8682
my_component = find_component("org.labthings.example.mycomponent")
8783
return my_component.magic_denoise
8884

89-
# Main function to handle POST requests (write)
90-
def post(self, new_property_value):
91-
"""Change the current magic_denoise value"""
92-
85+
def put(self, new_property_value):
9386
# Find our attached component
9487
my_component = find_component("org.labthings.example.mycomponent")
9588

@@ -104,12 +97,13 @@ def post(self, new_property_value):
10497
"""
10598

10699

107-
@PropertySchema(fields.List(fields.Float()))
108100
class QuickDataProperty(PropertyView):
109-
# Main function to handle GET requests
110-
def get(self):
111-
"""Show the current data value"""
101+
"""Show the current data value"""
102+
103+
# Marshal the response as a list of floats
104+
schema = fields.List(fields.Float())
112105

106+
def get(self):
113107
# Find our attached component
114108
my_component = find_component("org.labthings.example.mycomponent")
115109
return my_component.data
@@ -123,17 +117,14 @@ def get(self):
123117
class MeasurementAction(ActionView):
124118
# Expect JSON parameters in the request body.
125119
# Pass to post function as dictionary argument.
126-
@use_args(
127-
{
128-
"averages": fields.Integer(
129-
missing=20,
130-
example=20,
131-
description="Number of data sets to average over",
132-
)
133-
}
134-
)
135-
# Output schema
136-
@marshal_with(fields.List(fields.Number))
120+
args = {
121+
"averages": fields.Integer(
122+
missing=20, example=20, description="Number of data sets to average over",
123+
)
124+
}
125+
# Marshal the response as a list of numbers
126+
schema = fields.List(fields.Number)
127+
137128
# Main function to handle POST requests
138129
def post(self, args):
139130
"""Start an averaged measurement"""

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "labthings"
3-
version = "0.6.7"
3+
version = "0.7.0-beta.0"
44
description = "Python implementation of LabThings, based on the Flask microframework"
55
readme = "README.md"
66
repository = "https://github.com/labthings/python-labthings/"

src/labthings/core/lock.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __call__(self, timeout=sentinel, blocking=True):
3838
yield result
3939
if result:
4040
self.release()
41-
41+
4242
def locked(self):
4343
return self._lock.locked()
4444

@@ -96,7 +96,7 @@ def __call__(self, timeout=sentinel, blocking=True):
9696
yield result
9797
if result:
9898
self.release()
99-
99+
100100
def acquire(self, blocking=True, timeout=sentinel):
101101
if timeout is sentinel:
102102
timeout = self.timeout

0 commit comments

Comments
 (0)