Get action descriptions from docstrings

This commit is contained in:
jtc42 2019-11-19 22:46:50 +00:00
parent c2d27e8f56
commit 347bd2207a
6 changed files with 48 additions and 9 deletions

View file

@ -1,45 +1,41 @@
from flask import Blueprint, url_for, jsonify from flask import Blueprint, url_for, jsonify
from sys import platform from sys import platform
from openflexure_microscope.utilities import get_docstring
from openflexure_microscope.api.views import MicroscopeView from openflexure_microscope.api.views import MicroscopeView
from . import camera, stage, system from . import camera, stage, system
_actions = { _actions = {
"capture": { "capture": {
"rule": "/camera/capture/", "rule": "/camera/capture/",
"view_class": camera.CaptureAPI, "view_class": camera.CaptureAPI,
"description": "Take a single still capture",
"conditions": True, "conditions": True,
}, },
"preview_start": { "preview_start": {
"rule": "/camera/preview/start", "rule": "/camera/preview/start",
"view_class": camera.GPUPreviewStartAPI, "view_class": camera.GPUPreviewStartAPI,
"description": "Start the on-board GPU preview",
"conditions": True, "conditions": True,
}, },
"preview_stop": { "preview_stop": {
"rule": "/camera/preview/stop", "rule": "/camera/preview/stop",
"view_class": camera.GPUPreviewStopAPI, "view_class": camera.GPUPreviewStopAPI,
"description": "Stop the on-board GPU preview",
"conditions": True, "conditions": True,
}, },
"move": { "move": {
"rule": "/stage/move/", "rule": "/stage/move/",
"view_class": stage.MoveStageAPI, "view_class": stage.MoveStageAPI,
"description": "Move the microscope stage",
"conditions": True, "conditions": True,
}, },
"shutdown": { "shutdown": {
"rule": "/system/shutdown/", "rule": "/system/shutdown/",
"view_class": system.ShutdownAPI, "view_class": system.ShutdownAPI,
"description": "Shutdown the device",
"conditions": (platform == "linux"), "conditions": (platform == "linux"),
}, },
"reboot": { "reboot": {
"rule": "/system/reboot/", "rule": "/system/reboot/",
"view_class": system.RebootAPI, "view_class": system.RebootAPI,
"description": "Reboot the device",
"conditions": (platform == "linux"), "conditions": (platform == "linux"),
}, },
} }
@ -57,7 +53,7 @@ def actions_representation():
for name, action in enabled_actions().items(): for name, action in enabled_actions().items():
d = { d = {
"links": {"self": url_for(f".{name}")}, "links": {"self": url_for(f".{name}")},
"description": action["description"], "description": get_docstring(action["view_class"]),
"rule": action["rule"], "rule": action["rule"],
"view_class": str(action["view_class"]), "view_class": str(action["view_class"]),
} }

View file

@ -7,6 +7,12 @@ from flask import jsonify, request, abort, url_for, redirect, send_file
class CaptureAPI(MicroscopeView): class CaptureAPI(MicroscopeView):
"""
Create a new image capture.
Allowed methods:
POST
"""
def post(self): def post(self):
""" """
Create a new image capture. Create a new image capture.
@ -101,6 +107,12 @@ class CaptureAPI(MicroscopeView):
class GPUPreviewStartAPI(MicroscopeView): class GPUPreviewStartAPI(MicroscopeView):
"""
Start the onboard GPU preview.
Allowed methods:
POST
"""
def post(self, operation): def post(self, operation):
""" """
Start the onboard GPU preview. Start the onboard GPU preview.
@ -143,6 +155,12 @@ class GPUPreviewStartAPI(MicroscopeView):
class GPUPreviewStopAPI(MicroscopeView): class GPUPreviewStopAPI(MicroscopeView):
"""
Stop the onboard GPU preview.
Allowed methods:
POST
"""
def post(self, operation): def post(self, operation):
""" """
Stop the onboard GPU preview. Stop the onboard GPU preview.

View file

@ -8,6 +8,12 @@ import logging
class MoveStageAPI(MicroscopeView): class MoveStageAPI(MicroscopeView):
"""
Handle stage movements.
Allowed methods:
POST
"""
def post(self): def post(self):
""" """
Set x, y and z positions of the stage. Set x, y and z positions of the stage.

View file

@ -4,6 +4,12 @@ import subprocess
class ShutdownAPI(MicroscopeView): class ShutdownAPI(MicroscopeView):
"""
Attempt to shutdown the device
Allowed methods:
POST
"""
def post(self): def post(self):
""" """
Attempt to shutdown the device Attempt to shutdown the device
@ -17,6 +23,12 @@ class ShutdownAPI(MicroscopeView):
class RebootAPI(MicroscopeView): class RebootAPI(MicroscopeView):
"""
Attempt to reboot the device
Allowed methods:
POST
"""
def post(self): def post(self):
""" """
Attempt to shutdown the device Attempt to shutdown the device

View file

@ -1,5 +1,6 @@
from openflexure_microscope.plugins import PluginLoader, MicroscopePlugin from openflexure_microscope.plugins import PluginLoader, MicroscopePlugin
from openflexure_microscope.api.views import MicroscopeViewPlugin from openflexure_microscope.api.views import MicroscopeViewPlugin
from openflexure_microscope.utilities import get_docstring
from flask import Blueprint, jsonify, url_for from flask import Blueprint, jsonify, url_for
from openflexure_microscope.api.views import MicroscopeView from openflexure_microscope.api.views import MicroscopeView
@ -28,7 +29,7 @@ def plugins_representation(plugin_loader_object: PluginLoader):
"plugin": str(plugin), "plugin": str(plugin),
"views": {}, "views": {},
"form": plugin.form, "form": plugin.form,
"description": plugin.__doc__.strip() if plugin.__doc__ else "" "description": get_docstring(plugin)
} }
for view_id, view_data in plugin.views.items(): for view_id, view_data in plugin.views.items():
@ -36,7 +37,7 @@ def plugins_representation(plugin_loader_object: PluginLoader):
uri = url_for(f"v2_plugins_blueprint.{view_id}") uri = url_for(f"v2_plugins_blueprint.{view_id}")
# Make links dictionary if it doesn't yet exist # Make links dictionary if it doesn't yet exist
view_d = { view_d = {
"description": view_data["view"].__doc__.strip() if view_data["view"].__doc__ else "", "description": get_docstring(view_data["view"]),
"links": {"self": uri} "links": {"self": uri}
} }

View file

@ -5,6 +5,12 @@ from collections import abc
from functools import reduce from functools import reduce
from contextlib import contextmanager from contextlib import contextmanager
def get_docstring(obj):
ds = obj.__doc__
if ds:
return ds.strip()
else:
return ""
def camel_to_snake(name): def camel_to_snake(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)