Fixed plugin route attaching
This commit is contained in:
parent
477abb6970
commit
779c112ecc
3 changed files with 34 additions and 37 deletions
|
|
@ -16,16 +16,11 @@ from flask_cors import CORS
|
|||
from openflexure_microscope.api.exceptions import JSONExceptionHandler
|
||||
from openflexure_microscope.api.utilities import list_routes
|
||||
|
||||
from openflexure_microscope.config import (
|
||||
settings_file_path,
|
||||
JSONEncoder,
|
||||
USER_PLUGINS_PATH,
|
||||
)
|
||||
from openflexure_microscope.config import settings_file_path, JSONEncoder
|
||||
from openflexure_microscope.api.v1 import blueprints
|
||||
from openflexure_microscope.api import v2
|
||||
|
||||
from openflexure_microscope.common.labthings import LabThing
|
||||
from openflexure_microscope.common.labthings.plugins import find_plugins
|
||||
|
||||
from openflexure_microscope.api.microscope import default_microscope as api_microscope
|
||||
|
||||
|
|
@ -140,21 +135,6 @@ v2_actions_blueprint = v2.blueprints.actions.construct_blueprint(api_microscope)
|
|||
app.register_blueprint(v2_actions_blueprint, url_prefix=uri("/actions", "v2"))
|
||||
|
||||
|
||||
plugins = find_plugins(USER_PLUGINS_PATH)
|
||||
print(plugins.__plugins__)
|
||||
|
||||
for plugin_obj in plugins.__plugins__:
|
||||
for plugin_view_id, plugin_view in plugin_obj.views.items():
|
||||
# Add route to the plugins blueprint
|
||||
app.add_url_rule(
|
||||
"/new-plugins" + plugin_view["rule"],
|
||||
view_func=plugin_view["view"].as_view(
|
||||
f"{plugin_obj._name_python_safe}_{plugin_view_id}"
|
||||
),
|
||||
**plugin_view["kwargs"],
|
||||
)
|
||||
|
||||
|
||||
@app.route("/routes")
|
||||
def routes():
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -2,11 +2,13 @@
|
|||
Top-level representation of attached and enabled plugins
|
||||
"""
|
||||
|
||||
from openflexure_microscope.plugins import PluginLoader, MicroscopePlugin
|
||||
from openflexure_microscope.api.views import MicroscopeViewPlugin
|
||||
from openflexure_microscope.utilities import get_docstring, description_from_view
|
||||
from openflexure_microscope.api.utilities import blueprint_for_module
|
||||
|
||||
from openflexure_microscope.common.labthings.plugins import find_plugins
|
||||
from openflexure_microscope.config import USER_PLUGINS_PATH
|
||||
|
||||
from flask import Blueprint, jsonify, url_for
|
||||
from openflexure_microscope.api.views import MicroscopeView
|
||||
|
||||
|
|
@ -14,8 +16,11 @@ import copy
|
|||
import logging
|
||||
import warnings
|
||||
|
||||
_plugins = find_plugins(USER_PLUGINS_PATH)
|
||||
print(_plugins)
|
||||
|
||||
def plugins_representation(plugin_loader_object: PluginLoader):
|
||||
|
||||
def plugins_representation(plugin_list):
|
||||
"""
|
||||
Generate a dictionary representation of all plugins, including Flask route URLs
|
||||
|
||||
|
|
@ -27,7 +32,7 @@ def plugins_representation(plugin_loader_object: PluginLoader):
|
|||
"""
|
||||
plugins = {}
|
||||
|
||||
for plugin in plugin_loader_object.active:
|
||||
for plugin in plugin_list:
|
||||
logging.debug(f"Representing plugin {plugin._name}")
|
||||
d = {
|
||||
"python_name": plugin._name_python_safe,
|
||||
|
|
@ -67,13 +72,25 @@ class PluginFormAPI(MicroscopeView):
|
|||
A complete list of enabled plugins can be found in the microscope state.
|
||||
|
||||
"""
|
||||
return jsonify(plugins_representation(self.microscope.plugins))
|
||||
global _plugins
|
||||
return jsonify(plugins_representation(_plugins))
|
||||
|
||||
|
||||
def construct_blueprint(microscope_obj):
|
||||
|
||||
blueprint = blueprint_for_module(__name__)
|
||||
|
||||
for plugin_obj in _plugins:
|
||||
for plugin_view_id, plugin_view in plugin_obj.views.items():
|
||||
# Add route to the plugins blueprint
|
||||
blueprint.add_url_rule(
|
||||
plugin_view["rule"],
|
||||
view_func=plugin_view["view"].as_view(
|
||||
f"{plugin_obj._name_python_safe}_{plugin_view_id}"
|
||||
),
|
||||
**plugin_view["kwargs"],
|
||||
)
|
||||
|
||||
# Create a base route to return plugin API forms, if any exist
|
||||
blueprint.add_url_rule(
|
||||
"/", view_func=PluginFormAPI.as_view("plugins", microscope=microscope_obj)
|
||||
|
|
|
|||
|
|
@ -16,13 +16,15 @@ class BasePlugin:
|
|||
Handles binding route views and forms.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self, name: str, description=""):
|
||||
self._views = (
|
||||
{}
|
||||
) # Key: Full, Python-safe ID. Val: Original rule, and view class
|
||||
self._rules = {} # Key: Original rule. Val: View class
|
||||
self._gui = None
|
||||
|
||||
self.name = name
|
||||
|
||||
@property
|
||||
def views(self):
|
||||
return self._views
|
||||
|
|
@ -84,22 +86,17 @@ class BasePlugin:
|
|||
|
||||
@property
|
||||
def _name(self):
|
||||
return self.__class__.__name__
|
||||
return self.name
|
||||
|
||||
@property
|
||||
def _name_python_safe(self):
|
||||
return camel_to_snake(self._name)
|
||||
name = camel_to_snake(self._name) # Camel to snake
|
||||
name = name.replace(" ", "_") # Spaces to snake
|
||||
return name
|
||||
|
||||
@property
|
||||
def _name_uri_safe(self):
|
||||
return camel_to_spine(self._name)
|
||||
|
||||
def _full_name(self):
|
||||
module = self.__class__.__module__
|
||||
if module is None or module == str.__class__.__module__:
|
||||
return self.__class__.__name__ # Avoid reporting __builtin__
|
||||
else:
|
||||
return module + "." + self.__class__.__name__
|
||||
return camel_to_spine(self._name_python_safe)
|
||||
|
||||
|
||||
def find_plugins(plugin_path, module_name="plugins"):
|
||||
|
|
@ -116,4 +113,7 @@ def find_plugins(plugin_path, module_name="plugins"):
|
|||
|
||||
spec.loader.exec_module(mod)
|
||||
|
||||
return mod
|
||||
if hasattr(mod, "__plugins__"):
|
||||
return mod.__plugins__
|
||||
else:
|
||||
return None
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue