Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions examples/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,25 @@
labthing.add_component(my_component, "org.labthings.example.mycomponent")

# Add routes for the API views we created
labthing.add_view(
property_of(my_component, "magic_denoise", description="A magic denoise property"),
"/denoise",

labthing.build_property(
my_component, "magic_denoise", "/denoise", description="A magic denoise property",
)
labthing.add_view(
property_of(
my_component,
"magic_dictionary",
description="A big dictionary of little properties",
),

labthing.build_property(
my_component,
"magic_dictionary",
"/dictionary",
description="A big dictionary of little properties",
)
labthing.add_view(
action_from(
my_component.average_data,
description="Take an averaged measurement",
task=True, # Is the action a long-running task?
safe=True, # Is the state of the Thing unchanged by calling the action?
idempotent=True, # Can the action be called repeatedly with the same result?
),

labthing.build_action(
my_component.average_data,
"/average",
description="Take an averaged measurement",
task=True, # Is the action a long-running task?
safe=True, # Is the state of the Thing unchanged by calling the action?
idempotent=True, # Can the action be called repeatedly with the same result?
)


Expand Down
13 changes: 13 additions & 0 deletions labthings/server/labthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@
from .decorators import tag
from .sockets import Sockets

from .view.builder import property_of, action_from

from .default_views.extensions import ExtensionList
from .default_views.tasks import TaskList, TaskView
from .default_views.docs import docs_blueprint, SwaggerUIView
from .default_views.root import RootView
from .default_views.sockets import socket_handler

from typing import Callable

import weakref
import logging

Expand Down Expand Up @@ -333,3 +337,12 @@ def add_root_link(self, view, rel, kwargs=None, params=None):
if params is None:
params = {}
self.thing_description.add_link(view, rel, kwargs=kwargs, params=params)

# Convenience methods
def build_property(
self, property_object: object, property_name: str, *urls, **kwargs
):
self.add_view(property_of(property_object, property_name, **kwargs), *urls)

def build_action(self, function: Callable, *urls, **kwargs):
self.add_view(action_from(function, **kwargs), *urls)
17 changes: 15 additions & 2 deletions labthings/server/wsgi/gevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@

class Server:
def __init__(
self, app, host="0.0.0.0", port=7485, log=None, debug=False, zeroconf=True
self,
app,
host="0.0.0.0",
port=7485,
log=None,
error_log=None,
debug=False,
zeroconf=True,
):
self.app = app
# Find LabThing attached to app
Expand All @@ -22,6 +29,7 @@ def __init__(
self.host = host
self.port = port
self.log = log
self.error_log = error_log
self.debug = debug
self.zeroconf = zeroconf

Expand Down Expand Up @@ -79,6 +87,8 @@ def start(self):
# Handle logging
if not self.log:
self.log = logging.getLogger()
if not self.error_log:
self.error_log = logging.getLogger()

# Handle debug mode
if self.debug:
Expand Down Expand Up @@ -114,7 +124,7 @@ def start(self):
self.stop() # pragma: no cover

def run(
self, host=None, port=None, log=None, debug=None, zeroconf=None,
self, host=None, port=None, log=None, error_log=None, debug=None, zeroconf=None,
):
"""Starts the server allowing for runtime parameters. Designed to immitate
the old Flask app.run style of starting an app
Expand All @@ -135,6 +145,9 @@ def run(
if log is not None:
self.log = log

if error_log is not None:
self.error_log = error_log

if debug is not None:
self.debug = debug

Expand Down