From 45e7bcb167abedd568c28981a1eaa08f83cec4c3 Mon Sep 17 00:00:00 2001 From: Joel Collins Date: Wed, 27 Nov 2019 14:34:45 +0000 Subject: [PATCH] Fixed plugin view ID clashes --- .../api/v2/blueprints/plugins.py | 6 ++++-- openflexure_microscope/plugins/loader.py | 19 +++++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/openflexure_microscope/api/v2/blueprints/plugins.py b/openflexure_microscope/api/v2/blueprints/plugins.py index 644a33aa..a5028b56 100644 --- a/openflexure_microscope/api/v2/blueprints/plugins.py +++ b/openflexure_microscope/api/v2/blueprints/plugins.py @@ -39,7 +39,7 @@ def plugins_representation(plugin_loader_object: PluginLoader): for view_id, view_data in plugin.views.items(): logging.debug(f"Representing view {view_id}") - uri = url_for(f"v2_plugins_blueprint.{view_id}") + uri = url_for(f"v2_plugins_blueprint.{plugin._name_python_safe}_{view_id}") # Make links dictionary if it doesn't yet exist view_d = {"links": {"self": uri}} @@ -86,7 +86,9 @@ def construct_blueprint(microscope_obj): blueprint.add_url_rule( plugin_view["rule"], view_func=plugin_view["view"].as_view( - plugin_view_id, microscope=microscope_obj, plugin=plugin + f"{plugin._name_python_safe}_{plugin_view_id}", + microscope=microscope_obj, + plugin=plugin, ), ) diff --git a/openflexure_microscope/plugins/loader.py b/openflexure_microscope/plugins/loader.py index 503bb2d0..dc7f9f1d 100644 --- a/openflexure_microscope/plugins/loader.py +++ b/openflexure_microscope/plugins/loader.py @@ -1,6 +1,7 @@ import importlib import copy import os +import collections import inspect import logging @@ -258,9 +259,11 @@ class BasePlugin: # Expand the rule to include plugin name full_rule = "/{}/{}".format(self._name_uri_safe, cleaned_rule) - # Create a Python-safe route ID view_id = cleaned_rule.replace("/", "_") + # Create a Python-safe route ID + logging.debug(view_id) + # Store route information in a dictionary d = {"rule": full_rule, "view": view_class} @@ -271,10 +274,22 @@ class BasePlugin: @property def gui(self): + print(self._gui) + # Handle missing/no GUI if not self._gui: return None + # Handle GUI as a dictionary-returning callable function + elif isinstance(self._gui, collections.Callable): + api_gui = self._gui() + # Handle GUI as a static dictionary + elif isinstance(self._gui, dict): + api_gui = copy.deepcopy(self._gui) + # Handle borked GUI + else: + raise ValueError( + "GUI must be a dictionary, or a dictionary-returning function with no arguments." + ) - api_gui = copy.deepcopy(self._gui) api_gui["id"] = self._name if "forms" in api_gui and isinstance(api_gui["forms"], list):