Added plugin JSON schema
This commit is contained in:
parent
b4e2d5b172
commit
f0c1547d82
2 changed files with 31 additions and 33 deletions
|
|
@ -5,6 +5,7 @@ import copy
|
||||||
from importlib import util
|
from importlib import util
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
||||||
from openflexure_microscope.utilities import camel_to_snake, snake_to_spine
|
from openflexure_microscope.utilities import camel_to_snake, snake_to_spine
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -22,10 +23,13 @@ class BasePlugin:
|
||||||
self._rules = {} # Key: Original rule. Val: View class
|
self._rules = {} # Key: Original rule. Val: View class
|
||||||
self._gui = None
|
self._gui = None
|
||||||
|
|
||||||
|
self._cls = str(self) # String description of plugin instance
|
||||||
|
|
||||||
self.actions = []
|
self.actions = []
|
||||||
self.properties = []
|
self.properties = []
|
||||||
|
|
||||||
self.name = name
|
self.name = name
|
||||||
|
self.description = get_docstring(self)
|
||||||
|
|
||||||
self.methods = {}
|
self.methods = {}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,53 +5,47 @@ Top-level representation of attached and enabled plugins
|
||||||
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
from openflexure_microscope.common.labthings_core.utilities import get_docstring
|
||||||
from ..utilities import description_from_view
|
from ..utilities import description_from_view
|
||||||
|
|
||||||
from openflexure_microscope.common.flask_labthings.find import registered_plugins
|
|
||||||
from openflexure_microscope.common.flask_labthings.resource import Resource
|
from openflexure_microscope.common.flask_labthings.resource import Resource
|
||||||
|
from openflexure_microscope.common.flask_labthings.find import registered_plugins
|
||||||
|
from openflexure_microscope.common.flask_labthings.schema import Schema
|
||||||
|
from openflexure_microscope.common.flask_labthings.decorators import marshal_with
|
||||||
|
from openflexure_microscope.common.flask_labthings import fields
|
||||||
|
from marshmallow import pre_dump
|
||||||
|
|
||||||
from flask import jsonify, url_for
|
from flask import jsonify, url_for
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
|
||||||
def plugins_representation(plugin_dict):
|
class PluginSchema(Schema):
|
||||||
"""
|
name = fields.String(data_key="title")
|
||||||
Generate a dictionary representation of all plugins, including Flask route URLs
|
_name_python_safe = fields.String(data_key="pythonName")
|
||||||
|
_cls = fields.String(data_key="pythonObject")
|
||||||
|
gui = fields.Dict()
|
||||||
|
description = fields.String()
|
||||||
|
|
||||||
Args:
|
links = fields.Dict()
|
||||||
plugin_dict (dict): Dictionary of plugin objects
|
|
||||||
|
|
||||||
Returns:
|
# TODO: Automate this somewhat
|
||||||
dict: Dictionary representation of all plugins
|
@pre_dump
|
||||||
"""
|
def generate_links(self, data, **kwargs):
|
||||||
plugins = {}
|
d = {}
|
||||||
|
for view_id, view_data in data.views.items():
|
||||||
for plugin_name, plugin in plugin_dict.items():
|
view_cls = view_data["view"]
|
||||||
logging.debug(f"Representing plugin {plugin._name}")
|
view_kwargs = view_data["kwargs"]
|
||||||
d = {
|
|
||||||
"python_name": plugin._name_python_safe,
|
|
||||||
"plugin": str(plugin),
|
|
||||||
"links": {},
|
|
||||||
"gui": plugin.gui,
|
|
||||||
"description": get_docstring(plugin),
|
|
||||||
}
|
|
||||||
|
|
||||||
for view_id, view_data in plugin.views.items():
|
|
||||||
uri = (
|
|
||||||
url_for(f"pluginlistresource", _external=True)
|
|
||||||
+ "/"
|
|
||||||
+ view_data["rule"][1:]
|
|
||||||
)
|
|
||||||
# Make links dictionary if it doesn't yet exist
|
# Make links dictionary if it doesn't yet exist
|
||||||
view_d = {"href": uri, **description_from_view(view_data["view"])}
|
d[view_id] = {
|
||||||
|
"href": url_for(view_cls.endpoint, **view_kwargs, _external=True),
|
||||||
|
**description_from_view(view_cls),
|
||||||
|
}
|
||||||
|
|
||||||
d["links"][view_id] = view_d
|
data.links = d
|
||||||
|
|
||||||
plugins[plugin_name] = d
|
return data
|
||||||
|
|
||||||
return plugins
|
|
||||||
|
|
||||||
|
|
||||||
class PluginListResource(Resource):
|
class PluginListResource(Resource):
|
||||||
|
@marshal_with(PluginSchema(many=True))
|
||||||
def get(self):
|
def get(self):
|
||||||
"""
|
"""
|
||||||
Return the current plugin forms
|
Return the current plugin forms
|
||||||
|
|
@ -65,4 +59,4 @@ class PluginListResource(Resource):
|
||||||
A complete list of enabled plugins can be found in the microscope state.
|
A complete list of enabled plugins can be found in the microscope state.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return jsonify(plugins_representation(registered_plugins()))
|
return registered_plugins().values()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue