Skip to content

Commit 9101858

Browse files
authored
Merge pull request #68 from labthings/error-log-fix
Error log fix
2 parents 4f87c33 + ffe7952 commit 9101858

File tree

3 files changed

+43
-19
lines changed

3 files changed

+43
-19
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)

labthings/server/wsgi/gevent.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@
1212

1313
class Server:
1414
def __init__(
15-
self, app, host="0.0.0.0", port=7485, log=None, debug=False, zeroconf=True
15+
self,
16+
app,
17+
host="0.0.0.0",
18+
port=7485,
19+
log=None,
20+
error_log=None,
21+
debug=False,
22+
zeroconf=True,
1623
):
1724
self.app = app
1825
# Find LabThing attached to app
@@ -22,6 +29,7 @@ def __init__(
2229
self.host = host
2330
self.port = port
2431
self.log = log
32+
self.error_log = error_log
2533
self.debug = debug
2634
self.zeroconf = zeroconf
2735

@@ -79,6 +87,8 @@ def start(self):
7987
# Handle logging
8088
if not self.log:
8189
self.log = logging.getLogger()
90+
if not self.error_log:
91+
self.error_log = logging.getLogger()
8292

8393
# Handle debug mode
8494
if self.debug:
@@ -114,7 +124,7 @@ def start(self):
114124
self.stop() # pragma: no cover
115125

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

148+
if error_log is not None:
149+
self.error_log = error_log
150+
138151
if debug is not None:
139152
self.debug = debug
140153

0 commit comments

Comments
 (0)