Skip to content

Commit 9478f82

Browse files
author
Joel Collins
committed
Added "action" key to action objects
1 parent 998b4ea commit 9478f82

File tree

9 files changed

+50
-47
lines changed

9 files changed

+50
-47
lines changed

src/labthings/actions/pool.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ def __init__(self, maxlen: int = 100):
1515
def add(self, thread: ActionThread):
1616
"""
1717
18-
:param thread: ActionThread:
18+
:param thread: ActionThread:
1919
2020
"""
2121
self.threads.append(thread)
2222

2323
def start(self, thread: ActionThread):
2424
"""
2525
26-
:param thread: ActionThread:
26+
:param thread: ActionThread:
2727
2828
"""
2929
self.add(thread)
3030
thread.start()
3131

32-
def spawn(self, function, *args, **kwargs):
32+
def spawn(self, action: str, function, *args, **kwargs):
3333
"""
3434
35-
:param function:
36-
:param *args:
37-
:param **kwargs:
35+
:param function:
36+
:param *args:
37+
:param **kwargs:
3838
3939
"""
40-
thread = ActionThread(target=function, args=args, kwargs=kwargs)
40+
thread = ActionThread(action, target=function, args=args, kwargs=kwargs)
4141
self.start(thread)
4242
return thread
4343

@@ -90,7 +90,7 @@ def get(self, task_id):
9090
def discard_id(self, task_id):
9191
"""
9292
93-
:param task_id:
93+
:param task_id:
9494
9595
"""
9696
marked_for_discard = set()
@@ -122,7 +122,7 @@ def join(self):
122122

123123
def current_action():
124124
"""Return the ActionThread instance in which the caller is currently running.
125-
125+
126126
If this function is called from outside an ActionThread, it will return None.
127127
128128
@@ -137,7 +137,7 @@ def current_action():
137137

138138
def update_action_progress(progress: int):
139139
"""Update the progress of the ActionThread in which the caller is currently running.
140-
140+
141141
If this function is called from outside an ActionThread, it will do nothing.
142142
143143
:param progress: int: Action progress, in percent (0-100)
@@ -151,7 +151,7 @@ def update_action_progress(progress: int):
151151

152152
def update_action_data(data: dict):
153153
"""Update the data of the ActionThread in which the caller is currently running.
154-
154+
155155
If this function is called from outside an ActionThread, it will do nothing.
156156
157157
:param data: dict: Action data dictionary

src/labthings/actions/thread.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ActionThread(threading.Thread):
2424

2525
def __init__(
2626
self,
27+
action,
2728
target=None,
2829
name=None,
2930
args=None,
@@ -46,6 +47,9 @@ def __init__(
4647
args = args or ()
4748
kwargs = kwargs or {}
4849

50+
# Action resource corresponding to this action object
51+
self.action = action
52+
4953
# A UUID for the ActionThread (not the same as the threading.Thread ident)
5054
self._ID = uuid.uuid4() # Task ID
5155

src/labthings/labthing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
)
2727
from .representations import DEFAULT_REPRESENTATIONS
2828
from .td import ThingDescription
29-
from .utilities import camel_to_snake, clean_url_string
29+
from .utilities import snake_to_camel, clean_url_string
3030
from .views import ActionView, PropertyView, EventView
3131

3232
# from apispec.ext.marshmallow import MarshmallowPlugin
@@ -376,7 +376,7 @@ def add_view(self, view, *urls, endpoint=None, **kwargs):
376376
labthing.add_view(Foo, '/foo', endpoint="foo")
377377
labthing.add_view(FooSpecial, '/special/foo', endpoint="foo")
378378
"""
379-
endpoint = endpoint or camel_to_snake(view.__name__)
379+
endpoint = endpoint or snake_to_camel(view.__name__)
380380

381381
logging.debug(f"{endpoint}: {type(view)} @ {urls}")
382382

@@ -410,7 +410,7 @@ def decorator(cls):
410410
return decorator
411411

412412
def _register_view(self, app, view, *urls, endpoint=None, **kwargs):
413-
endpoint = endpoint or camel_to_snake(view.__name__)
413+
endpoint = endpoint or view.__name__
414414
self.endpoints.add(endpoint)
415415
resource_class_args = kwargs.pop("resource_class_args", ())
416416
resource_class_kwargs = kwargs.pop("resource_class_kwargs", {})

src/labthings/schema.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def dump(self, value):
6464
"""
6565
return self.serialize(value)
6666

67+
6768
class LogRecordSchema(Schema):
6869
name = fields.String()
6970
message = fields.String()
@@ -85,6 +86,7 @@ def preprocess(self, data, **kwargs):
8586
class ActionSchema(Schema):
8687
""" """
8788

89+
action = fields.String()
8890
_ID = fields.String(data_key="id")
8991
_status = fields.String(
9092
data_key="status",
@@ -95,7 +97,7 @@ class ActionSchema(Schema):
9597
_request_time = fields.DateTime(data_key="timeRequested")
9698
_end_time = fields.DateTime(data_key="timeCompleted")
9799
log = fields.List(fields.Nested(LogRecordSchema()))
98-
100+
99101
input = fields.Raw()
100102
output = fields.Raw()
101103

src/labthings/td.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .find import current_labthing
44
from .json.schemas import rule_to_params, rule_to_path, schema_to_json
5-
from .utilities import ResourceURL, get_docstring, snake_to_camel
5+
from .utilities import ResourceURL, get_docstring
66
from .views import View
77
from .schema import build_action_schema
88

@@ -269,8 +269,7 @@ def property(self, rules: list, view: View):
269269
270270
"""
271271
endpoint = getattr(view, "endpoint", None) or getattr(rules[0], "endpoint")
272-
key = snake_to_camel(endpoint)
273-
self.properties[key] = self.view_to_thing_property(rules, view)
272+
self.properties[endpoint] = self.view_to_thing_property(rules, view)
274273

275274
def action(self, rules: list, view: View):
276275
"""Add a view representing an Action.
@@ -288,8 +287,7 @@ def action(self, rules: list, view: View):
288287
but it does not have a POST method."
289288
)
290289
endpoint = getattr(view, "endpoint", None) or getattr(rules[0], "endpoint")
291-
key = snake_to_camel(endpoint)
292-
self.actions[key] = self.view_to_thing_action(rules, view)
290+
self.actions[endpoint] = self.view_to_thing_action(rules, view)
293291

294292
def event(self, rules: list, view: View):
295293
"""Add a view representing an event queue.
@@ -299,5 +297,4 @@ def event(self, rules: list, view: View):
299297
300298
"""
301299
endpoint = getattr(view, "endpoint", None) or getattr(rules[0], "endpoint")
302-
key = snake_to_camel(endpoint)
303-
self.events[key] = self.view_to_thing_event(rules, view)
300+
self.events[endpoint] = self.view_to_thing_event(rules, view)

src/labthings/views/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def dispatch_request(self, *args, **kwargs):
174174
current_labthing().actions if current_labthing() else self._emergency_pool
175175
)
176176
# Make a task out of the views `post` method
177-
task = pool.spawn(meth, *args, **kwargs)
177+
task = pool.spawn(self.endpoint, meth, *args, **kwargs)
178178
# Optionally override the threads default_stop_timeout
179179
if self.default_stop_timeout is not None:
180180
task.default_stop_timeout = self.default_stop_timeout

tests/test_default_views.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_actions_list(thing_client):
2323
def task_func():
2424
pass
2525

26-
task_obj = current_labthing().actions.spawn(task_func)
26+
task_obj = current_labthing().actions.spawn("task_func", task_func)
2727

2828
with thing_client as c:
2929
response = c.get("/actions").json
@@ -35,7 +35,7 @@ def test_action_representation(thing_client):
3535
def task_func():
3636
pass
3737

38-
task_obj = current_labthing().actions.spawn(task_func)
38+
task_obj = current_labthing().actions.spawn("task_func", task_func)
3939
task_id = str(task_obj.id)
4040

4141
with thing_client as c:
@@ -53,7 +53,7 @@ def task_func():
5353
while not current_action().stopping:
5454
time.sleep(0)
5555

56-
task_obj = current_labthing().actions.spawn(task_func)
56+
task_obj = current_labthing().actions.spawn("task_func", task_func)
5757
task_id = str(task_obj.id)
5858

5959
# Wait for task to start
@@ -73,7 +73,7 @@ def task_func():
7373
while True:
7474
time.sleep(0)
7575

76-
task_obj = current_labthing().actions.spawn(task_func)
76+
task_obj = current_labthing().actions.spawn("task_func", task_func)
7777
task_id = str(task_obj.id)
7878

7979
# Wait for task to start

tests/test_tasks_pool.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def test_spawn_without_context(task_pool):
77
def task_func():
88
pass
99

10-
task_obj = task_pool.spawn(task_func)
10+
task_obj = task_pool.spawn("task_func", task_func)
1111
assert isinstance(task_obj, threading.Thread)
1212

1313

@@ -16,15 +16,15 @@ def task_func():
1616
pass
1717

1818
with app_ctx.test_request_context():
19-
task_obj = task_pool.spawn(task_func)
19+
task_obj = task_pool.spawn("task_func", task_func)
2020
assert isinstance(task_obj, threading.Thread)
2121

2222

2323
def test_update_task_data(task_pool):
2424
def task_func():
2525
actions.update_action_data({"key": "value"})
2626

27-
task_obj = task_pool.spawn(task_func)
27+
task_obj = task_pool.spawn("task_func", task_func)
2828
task_obj.join()
2929
assert task_obj.data == {"key": "value"}
3030

@@ -38,7 +38,7 @@ def test_update_task_progress(task_pool):
3838
def task_func():
3939
actions.update_action_progress(100)
4040

41-
task_obj = task_pool.spawn(task_func)
41+
task_obj = task_pool.spawn("task_func", task_func)
4242
task_obj.join()
4343
assert task_obj.progress == 100
4444

@@ -65,7 +65,7 @@ def test_discard_id(task_pool):
6565
def task_func():
6666
pass
6767

68-
task_obj = task_pool.spawn(task_func)
68+
task_obj = task_pool.spawn("task_func", task_func)
6969
assert str(task_obj.id) in task_pool.to_dict()
7070
task_obj.join()
7171

@@ -80,7 +80,7 @@ def task_func():
8080
pass
8181

8282
# Make sure at least 1 actions is around
83-
task_pool.spawn(task_func)
83+
task_pool.spawn("task_func", task_func)
8484

8585
# Wait for all actions to finish
8686
task_pool.join()

0 commit comments

Comments
 (0)