Fixed plugin route attaching

This commit is contained in:
Joel Collins 2019-12-13 14:49:28 +00:00
parent 477abb6970
commit 779c112ecc
3 changed files with 34 additions and 37 deletions

View file

@ -16,16 +16,11 @@ from flask_cors import CORS
from openflexure_microscope.api.exceptions import JSONExceptionHandler from openflexure_microscope.api.exceptions import JSONExceptionHandler
from openflexure_microscope.api.utilities import list_routes from openflexure_microscope.api.utilities import list_routes
from openflexure_microscope.config import ( from openflexure_microscope.config import settings_file_path, JSONEncoder
settings_file_path,
JSONEncoder,
USER_PLUGINS_PATH,
)
from openflexure_microscope.api.v1 import blueprints from openflexure_microscope.api.v1 import blueprints
from openflexure_microscope.api import v2 from openflexure_microscope.api import v2
from openflexure_microscope.common.labthings import LabThing 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 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")) 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") @app.route("/routes")
def routes(): def routes():
""" """

View file

@ -2,11 +2,13 @@
Top-level representation of attached and enabled plugins 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.api.views import MicroscopeViewPlugin
from openflexure_microscope.utilities import get_docstring, description_from_view from openflexure_microscope.utilities import get_docstring, description_from_view
from openflexure_microscope.api.utilities import blueprint_for_module 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 flask import Blueprint, jsonify, url_for
from openflexure_microscope.api.views import MicroscopeView from openflexure_microscope.api.views import MicroscopeView
@ -14,8 +16,11 @@ import copy
import logging import logging
import warnings 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 Generate a dictionary representation of all plugins, including Flask route URLs
@ -27,7 +32,7 @@ def plugins_representation(plugin_loader_object: PluginLoader):
""" """
plugins = {} plugins = {}
for plugin in plugin_loader_object.active: for plugin in plugin_list:
logging.debug(f"Representing plugin {plugin._name}") logging.debug(f"Representing plugin {plugin._name}")
d = { d = {
"python_name": plugin._name_python_safe, "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. 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): def construct_blueprint(microscope_obj):
blueprint = blueprint_for_module(__name__) 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 # Create a base route to return plugin API forms, if any exist
blueprint.add_url_rule( blueprint.add_url_rule(
"/", view_func=PluginFormAPI.as_view("plugins", microscope=microscope_obj) "/", view_func=PluginFormAPI.as_view("plugins", microscope=microscope_obj)

View file

@ -16,13 +16,15 @@ class BasePlugin:
Handles binding route views and forms. Handles binding route views and forms.
""" """
def __init__(self): def __init__(self, name: str, description=""):
self._views = ( self._views = (
{} {}
) # Key: Full, Python-safe ID. Val: Original rule, and view class ) # Key: Full, Python-safe ID. Val: Original rule, and view class
self._rules = {} # Key: Original rule. Val: View class self._rules = {} # Key: Original rule. Val: View class
self._gui = None self._gui = None
self.name = name
@property @property
def views(self): def views(self):
return self._views return self._views
@ -84,22 +86,17 @@ class BasePlugin:
@property @property
def _name(self): def _name(self):
return self.__class__.__name__ return self.name
@property @property
def _name_python_safe(self): 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 @property
def _name_uri_safe(self): def _name_uri_safe(self):
return camel_to_spine(self._name) return camel_to_spine(self._name_python_safe)
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__
def find_plugins(plugin_path, module_name="plugins"): 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) spec.loader.exec_module(mod)
return mod if hasattr(mod, "__plugins__"):
return mod.__plugins__
else:
return None