Skip to content

Commit fe61a29

Browse files
committed
Added LabThing-level property and action builders
1 parent 4f87c33 commit fe61a29

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

examples/builder.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,25 @@
2828
labthing.add_component(my_component, "org.labthings.example.mycomponent")
2929

3030
# Add routes for the API views we created
31-
labthing.add_view(
32-
property_of(my_component, "magic_denoise", description="A magic denoise property"),
33-
"/denoise",
31+
32+
labthing.build_property(
33+
my_component, "magic_denoise", "/denoise", description="A magic denoise property",
3434
)
35-
labthing.add_view(
36-
property_of(
37-
my_component,
38-
"magic_dictionary",
39-
description="A big dictionary of little properties",
40-
),
35+
36+
labthing.build_property(
37+
my_component,
38+
"magic_dictionary",
4139
"/dictionary",
40+
description="A big dictionary of little properties",
4241
)
43-
labthing.add_view(
44-
action_from(
45-
my_component.average_data,
46-
description="Take an averaged measurement",
47-
task=True, # Is the action a long-running task?
48-
safe=True, # Is the state of the Thing unchanged by calling the action?
49-
idempotent=True, # Can the action be called repeatedly with the same result?
50-
),
42+
43+
labthing.build_action(
44+
my_component.average_data,
5145
"/average",
46+
description="Take an averaged measurement",
47+
task=True, # Is the action a long-running task?
48+
safe=True, # Is the state of the Thing unchanged by calling the action?
49+
idempotent=True, # Can the action be called repeatedly with the same result?
5250
)
5351

5452

labthings/server/labthing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@
1919
from .decorators import tag
2020
from .sockets import Sockets
2121

22+
from .view.builder import property_of, action_from
23+
2224
from .default_views.extensions import ExtensionList
2325
from .default_views.tasks import TaskList, TaskView
2426
from .default_views.docs import docs_blueprint, SwaggerUIView
2527
from .default_views.root import RootView
2628
from .default_views.sockets import socket_handler
2729

30+
from typing import Callable
31+
2832
import weakref
2933
import logging
3034

@@ -333,3 +337,12 @@ def add_root_link(self, view, rel, kwargs=None, params=None):
333337
if params is None:
334338
params = {}
335339
self.thing_description.add_link(view, rel, kwargs=kwargs, params=params)
340+
341+
# Convenience methods
342+
def build_property(
343+
self, property_object: object, property_name: str, *urls, **kwargs
344+
):
345+
self.add_view(property_of(property_object, property_name, **kwargs), *urls)
346+
347+
def build_action(self, function: Callable, *urls, **kwargs):
348+
self.add_view(action_from(function, **kwargs), *urls)

0 commit comments

Comments
 (0)