Skip to content

Commit

Permalink
Added taskification to action builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Apr 3, 2020
1 parent 85d7f25 commit 62729ea
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
4 changes: 3 additions & 1 deletion examples/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def cleanup():
"/dictionary",
)
labthing.add_view(
action_from(my_component.average_data, description="Take an averaged measurement"),
action_from(
my_component.average_data, description="Take an averaged measurement", task=True
),
"/average",
)

Expand Down
16 changes: 14 additions & 2 deletions labthings/server/view/builder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from labthings.core.tasks import taskify
from labthings.server.types import (
value_to_field,
data_dict_to_schema,
Expand All @@ -7,6 +8,7 @@
ThingProperty,
PropertySchema,
ThingAction,
marshal_task,
use_args,
Doc,
)
Expand Down Expand Up @@ -72,12 +74,19 @@ def _put(self, args):
return generated_class


def action_from(function, name: str = None, description=None):
def action_from(function, name: str = None, description=None, task=False):

# Create a class name
if not name:
name = f"Action_{function.__name__}"

# Create schema
action_schema = function_signature_to_schema(function)

# Handle taskification
if task:
function = taskify(function)

# Create inner functions
def _post(self, args):
return function(**args)
Expand All @@ -86,9 +95,12 @@ def _post(self, args):
generated_class = type(name, (View, object), {"post": _post})

# Add decorators for arguments etc
action_schema = function_signature_to_schema(function)

generated_class.post = use_args(action_schema)(generated_class.post)

if task:
generated_class.post = marshal_task(generated_class.post)

generated_class = ThingAction(generated_class)

if description:
Expand Down

0 comments on commit 62729ea

Please sign in to comment.