Plugin schema are now just plugin forms (requires eV 1.2)
This commit is contained in:
parent
0d7a5f71e0
commit
d9e07f913a
16 changed files with 106 additions and 98 deletions
|
|
@ -6,21 +6,21 @@ from openflexure_microscope.api.v1.views import MicroscopeView
|
|||
import logging
|
||||
import warnings
|
||||
|
||||
class PluginSchemaAPI(MicroscopeView):
|
||||
class PluginFormAPI(MicroscopeView):
|
||||
def get(self):
|
||||
"""
|
||||
Return the current plugin schemas
|
||||
Return the current plugin forms
|
||||
|
||||
.. :quickref: Plugin; Get schemas
|
||||
.. :quickref: Plugin; Get forms
|
||||
|
||||
Returns an array of present plugin schemas (describing plugin user interfaces.)
|
||||
Returns an array of present plugin forms (describing plugin user interfaces.)
|
||||
Please note, this is *not* a list of all enabled plugins, only those with associated
|
||||
user interface forms.
|
||||
|
||||
A complete list of enabled plugins can be found in the microscope state.
|
||||
|
||||
"""
|
||||
out = self.microscope.plugin.schemas
|
||||
out = self.microscope.plugin.forms
|
||||
return jsonify(out)
|
||||
|
||||
|
||||
|
|
@ -28,10 +28,10 @@ def construct_blueprint(microscope_obj):
|
|||
|
||||
blueprint = Blueprint('plugin_blueprint', __name__)
|
||||
|
||||
# Create a base route to return plugin API schemas, if any exist
|
||||
# Create a base route to return plugin API forms, if any exist
|
||||
blueprint.add_url_rule(
|
||||
'/',
|
||||
view_func=PluginSchemaAPI.as_view('plugin_api_schema', microscope=microscope_obj)
|
||||
view_func=PluginFormAPI.as_view('plugin_api_form', microscope=microscope_obj)
|
||||
)
|
||||
|
||||
all_routes = []
|
||||
|
|
@ -86,20 +86,21 @@ def construct_blueprint(microscope_obj):
|
|||
)
|
||||
)
|
||||
|
||||
# If plugin includes an API schema
|
||||
if hasattr(plugin_obj, 'api_schema') and isinstance(plugin_obj.api_schema, dict):
|
||||
schema = plugin_obj.api_schema
|
||||
# TODO: Validate schema? We need to make sure no single plugin can break all plugins.
|
||||
schema['id'] = plugin_name
|
||||
if 'forms' in schema and isinstance(schema['forms'], list):
|
||||
for form in schema['forms']:
|
||||
# If plugin includes an API form
|
||||
if hasattr(plugin_obj, 'api_form') and isinstance(plugin_obj.api_form, dict):
|
||||
api_form_info = plugin_obj.api_form
|
||||
# TODO: Validate form? We need to make sure no single plugin can break all plugins.
|
||||
api_form_info['id'] = plugin_name
|
||||
if 'forms' in api_form_info and isinstance(api_form_info['forms'], list):
|
||||
for form in api_form_info['forms']:
|
||||
if 'route' in form and form['route'] in expanded_routes.keys():
|
||||
form['route'] = expanded_routes[form['route']]
|
||||
else:
|
||||
logging.warn("No valid expandable route found for {}".format(form['route']))
|
||||
|
||||
# Store the complete schema in Microscope().plugin.schema
|
||||
microscope_obj.plugin.schemas.append(schema)
|
||||
# Store the complete form in Microscope().plugin.form
|
||||
microscope_obj.plugin.forms.append(api_form_info)
|
||||
print(microscope_obj.plugin.forms)
|
||||
|
||||
else:
|
||||
warnings.warn(
|
||||
|
|
|
|||
|
|
@ -107,7 +107,6 @@ class MockStreamer(BaseCamera):
|
|||
# If stream was paused to update config, unpause
|
||||
if paused_stream:
|
||||
logging.info("Resuming stream.")
|
||||
self.start_stream_recording()
|
||||
|
||||
else:
|
||||
raise Exception(
|
||||
|
|
|
|||
|
|
@ -94,26 +94,32 @@ class Microscope:
|
|||
# Maybe even attach dummy hardware at __init__, and replace with real hardware if it exists
|
||||
|
||||
logging.debug("Attaching camera...")
|
||||
self.camera = camera #: :py:class:`openflexure_microscope.camera.base.BaseCamera`: Picamera object
|
||||
self.camera = (
|
||||
camera
|
||||
) #: :py:class:`openflexure_microscope.camera.base.BaseCamera`: Picamera object
|
||||
if not self.camera:
|
||||
logging.info("No camera attached.")
|
||||
else:
|
||||
logging.info("Attached camera {}".format(camera))
|
||||
|
||||
if hasattr(self.camera, 'lock'): # If camera has a lock
|
||||
if hasattr(self.camera, "lock"): # If camera has a lock
|
||||
logging.info("Attaching {} to composite lock.".format(self.camera.lock))
|
||||
# Add the lock to the microscope composite lock
|
||||
self.lock.locks.append(self.camera.lock)
|
||||
|
||||
logging.debug("Attaching stage...")
|
||||
self.stage = stage #: :py:class:`openflexure_microscope.stage.base.BaseStage`: OpenFlexure stage object
|
||||
self.stage = (
|
||||
stage
|
||||
) #: :py:class:`openflexure_microscope.stage.base.BaseStage`: OpenFlexure stage object
|
||||
if not self.stage:
|
||||
logging.info("No stage attached.")
|
||||
else:
|
||||
logging.info("Attached stage {}".format(stage))
|
||||
|
||||
if hasattr(self.stage, 'lock'): # If stage object has a lock
|
||||
logging.info("Attaching lock {} to composite lock.".format(self.stage.lock))
|
||||
if hasattr(self.stage, "lock"): # If stage object has a lock
|
||||
logging.info(
|
||||
"Attaching lock {} to composite lock.".format(self.stage.lock)
|
||||
)
|
||||
# Add the lock to the microscope composite lock
|
||||
self.lock.locks.append(self.stage.lock)
|
||||
|
||||
|
|
@ -149,10 +155,10 @@ class Microscope:
|
|||
and :py:attr:`openflexure_microscope.camera.base.BaseCamera.state`
|
||||
"""
|
||||
state = {
|
||||
'camera': self.camera.state,
|
||||
'stage': self.stage.state,
|
||||
'plugin': self.plugin.state,
|
||||
'version': pkg_resources.get_distribution('openflexure_microscope').version
|
||||
"camera": self.camera.state,
|
||||
"stage": self.stage.state,
|
||||
"plugin": self.plugin.state,
|
||||
"version": pkg_resources.get_distribution("openflexure_microscope").version,
|
||||
}
|
||||
return state
|
||||
|
||||
|
|
@ -163,22 +169,22 @@ class Microscope:
|
|||
logging.debug("Microscope: Applying config: {}".format(config))
|
||||
|
||||
# If attached to a camera
|
||||
if ('camera_settings' in config) and self.camera:
|
||||
self.camera.apply_config(config['camera_settings'])
|
||||
if ("camera_settings" in config) and self.camera:
|
||||
self.camera.apply_config(config["camera_settings"])
|
||||
|
||||
# If attached to a stage
|
||||
if ('stage_settings' in config) and self.stage:
|
||||
self.stage.apply_config(config['stage_settings'])
|
||||
if ("stage_settings" in config) and self.stage:
|
||||
self.stage.apply_config(config["stage_settings"])
|
||||
|
||||
# Todo: tidy up with some loopy goodness
|
||||
if 'id' in config:
|
||||
self.id = config['id']
|
||||
if 'name' in config:
|
||||
self.name = config['name']
|
||||
if 'fov' in config:
|
||||
self.fov = config['fov']
|
||||
if 'plugins' in config:
|
||||
self.plugin_maps = config['plugins']
|
||||
if "id" in config:
|
||||
self.id = config["id"]
|
||||
if "name" in config:
|
||||
self.name = config["name"]
|
||||
if "fov" in config:
|
||||
self.fov = config["fov"]
|
||||
if "plugins" in config:
|
||||
self.plugin_maps = config["plugins"]
|
||||
|
||||
def read_config(self, json_safe=False):
|
||||
"""
|
||||
|
|
@ -192,21 +198,21 @@ class Microscope:
|
|||
"""
|
||||
|
||||
settings_current = {
|
||||
'id': self.id,
|
||||
'name': self.name,
|
||||
'fov': self.fov,
|
||||
'plugins': self.plugin_maps
|
||||
"id": self.id,
|
||||
"name": self.name,
|
||||
"fov": self.fov,
|
||||
"plugins": self.plugin_maps,
|
||||
}
|
||||
|
||||
# If attached to a camera
|
||||
if self.camera:
|
||||
settings_current_camera = self.camera.read_config()
|
||||
settings_current['camera_settings'] = settings_current_camera
|
||||
settings_current["camera_settings"] = settings_current_camera
|
||||
|
||||
# If attached to a stage
|
||||
if self.stage:
|
||||
settings_current_stage = self.stage.read_config()
|
||||
settings_current['stage_settings'] = settings_current_stage
|
||||
settings_current["stage_settings"] = settings_current_stage
|
||||
|
||||
settings_full = self.settings_file.merge(settings_current)
|
||||
|
||||
|
|
@ -222,7 +228,9 @@ class Microscope:
|
|||
# Read curent config
|
||||
current_config = self.read_config()
|
||||
# Merge in server version responsible for saving the config file
|
||||
current_config['server_version'] = pkg_resources.get_distribution('openflexure_microscope').version
|
||||
current_config["server_version"] = pkg_resources.get_distribution(
|
||||
"openflexure_microscope"
|
||||
).version
|
||||
# Save config to file
|
||||
self.settings_file.save(current_config, backup=True)
|
||||
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ class PluginMount(object):
|
|||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.plugins = [] # List of plugin objects
|
||||
self.schemas = [] # List of plugin schemas
|
||||
self.forms = [] # List of plugin forms
|
||||
logging.info("Creating plugin mount")
|
||||
|
||||
@property
|
||||
|
|
@ -182,7 +182,7 @@ class PluginMount(object):
|
|||
logging.info(ConColors.OKGREEN + "Plugin {} loaded as {}.".format(plugin_map, plugin_name) + ConColors.ENDC)
|
||||
|
||||
else:
|
||||
logging.error("Error loading plugin. Moving on.")
|
||||
logging.error(f"Error loading plugin {plugin_map}. Moving on.")
|
||||
|
||||
class MicroscopePlugin:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
"selfUpdate": true,
|
||||
"route": "/do",
|
||||
"submitLabel": "Do things",
|
||||
"schema": [
|
||||
"form": [
|
||||
[
|
||||
{
|
||||
"fieldType": "numberInput",
|
||||
|
|
@ -66,7 +66,7 @@
|
|||
"selfUpdate": true,
|
||||
"route": "/task",
|
||||
"submitLabel": "Start task",
|
||||
"schema": [
|
||||
"form": [
|
||||
{
|
||||
"fieldType": "numberInput",
|
||||
"placeholder": "",
|
||||
|
|
@ -7,16 +7,16 @@ from openflexure_microscope.devel import MicroscopePlugin
|
|||
from .api import DoAPI, TaskAPI
|
||||
|
||||
HERE = os.path.dirname(os.path.realpath(__file__))
|
||||
SCHEMA_PATH = os.path.join(HERE, "schema.json")
|
||||
FORM_PATH = os.path.join(HERE, "forms.json")
|
||||
|
||||
class ExamplePlugin(MicroscopePlugin):
|
||||
"""
|
||||
An example plugin using a comprehensive schema
|
||||
An example plugin using a comprehensive form
|
||||
"""
|
||||
global SCHEMA_PATH
|
||||
global FORM_PATH
|
||||
|
||||
with open(SCHEMA_PATH, 'r') as sc:
|
||||
api_schema = json.load(sc)
|
||||
with open(FORM_PATH, 'r') as sc:
|
||||
api_form = json.load(sc)
|
||||
|
||||
api_views = {
|
||||
'/do': DoAPI,
|
||||
|
|
@ -35,7 +35,7 @@ class ExamplePlugin(MicroscopePlugin):
|
|||
|
||||
def set_values(self, val_int, val_str, val_radio, val_check, val_select, val_disposable):
|
||||
"""
|
||||
Demonstrate a plugin with schema
|
||||
Demonstrate a plugin with form
|
||||
"""
|
||||
if val_int:
|
||||
self.val_int = int(val_int)
|
||||
Loading…
Add table
Add a link
Reference in a new issue