From 7d56d0f51bf560e826f35deb4ad7e4fbebce2faa Mon Sep 17 00:00:00 2001 From: Giuseppe Coco Date: Tue, 3 Dec 2024 19:38:57 +0100 Subject: [PATCH 1/4] feat: issue #980 --- core/cat/mad_hatter/plugin.py | 41 +++++++++++++++-------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/core/cat/mad_hatter/plugin.py b/core/cat/mad_hatter/plugin.py index b2f067334..81797dbe0 100644 --- a/core/cat/mad_hatter/plugin.py +++ b/core/cat/mad_hatter/plugin.py @@ -61,8 +61,8 @@ def __init__(self, plugin_path: str): self._forms: List[CatForm] = [] # list of plugin forms self._endpoints: List[CustomEndpoint] = [] # list of plugin endpoints - # list of @plugin decorated functions overriding default plugin behaviour - self._plugin_overrides = [] # TODO: make this a dictionary indexed by func name, for faster access + # list of @plugin decorated functions overriding default plugin behaviour + self._plugin_overrides = {} # plugin starts deactivated self._active = False @@ -101,20 +101,19 @@ def deactivate(self): self._tools = [] self._forms = [] self._deactivate_endpoints() - self._plugin_overrides = [] + self._plugin_overrides = {} self._active = False # get plugin settings JSON schema def settings_schema(self): # is "settings_schema" hook defined in the plugin? - for h in self._plugin_overrides: - if h.name == "settings_schema": - return h.function() - else: - # if the "settings_schema" is not defined but - # "settings_model" is it get the schema from the model - if h.name == "settings_model": - return h.function().model_json_schema() + if "settings_schema" in self._plugin_overrides: + return self._plugin_overrides["settings_schema"]() + else: + # if the "settings_schema" is not defined but + # "settings_model" is it get the schema from the model + if "settings_model" in self._plugin_overrides: + return self._plugin_overrides["settings_model"]().model_json_schema() # default schema (empty) return PluginSettingsModel.model_json_schema() @@ -122,9 +121,8 @@ def settings_schema(self): # get plugin settings Pydantic model def settings_model(self): # is "settings_model" hook defined in the plugin? - for h in self._plugin_overrides: - if h.name == "settings_model": - return h.function() + if "settings_model" in self._plugin_overrides: + return self._plugin_overrides["settings_model"]() # default schema (empty) return PluginSettingsModel @@ -132,9 +130,8 @@ def settings_model(self): # load plugin settings def load_settings(self): # is "settings_load" hook defined in the plugin? - for h in self._plugin_overrides: - if h.name == "load_settings": - return h.function() + if "load_settings" in self._plugin_overrides: + return self._plugin_overrides["load_settings"]() # by default, plugin settings are saved inside the plugin folder # in a JSON file called settings.json @@ -159,9 +156,8 @@ def load_settings(self): # save plugin settings def save_settings(self, settings: Dict): # is "settings_save" hook defined in the plugin? - for h in self._plugin_overrides: - if h.name == "save_settings": - return h.function(settings) + if "save_settings" in self._plugin_overrides: + return self._plugin_overrides["save_settings"](settings) # by default, plugin settings are saved inside the plugin folder # in a JSON file called settings.json @@ -331,9 +327,8 @@ def _load_decorated_functions(self): self._tools = list(map(self._clean_tool, tools)) self._forms = list(map(self._clean_form, forms)) self._endpoints = list(map(self._clean_endpoint, endpoints)) - self._plugin_overrides = list( - map(self._clean_plugin_override, plugin_overrides) - ) + self._plugin_overrides = {override.__name__: override for _, override in plugin_overrides} + def plugin_specific_error_message(self): name = self.manifest.get("name") From 8c7f2b59a4e43db593c4539a5cb29cc47baa2a1d Mon Sep 17 00:00:00 2001 From: Giuseppe Coco Date: Wed, 4 Dec 2024 09:58:10 +0100 Subject: [PATCH 2/4] fix: AttributeError: 'CatPluginDecorator' object has no attribute '__name__' --- core/cat/mad_hatter/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/cat/mad_hatter/plugin.py b/core/cat/mad_hatter/plugin.py index 81797dbe0..2c13b3775 100644 --- a/core/cat/mad_hatter/plugin.py +++ b/core/cat/mad_hatter/plugin.py @@ -327,7 +327,7 @@ def _load_decorated_functions(self): self._tools = list(map(self._clean_tool, tools)) self._forms = list(map(self._clean_form, forms)) self._endpoints = list(map(self._clean_endpoint, endpoints)) - self._plugin_overrides = {override.__name__: override for _, override in plugin_overrides} + self._plugin_overrides = {override.name: override for _, override in plugin_overrides} def plugin_specific_error_message(self): From a75fdc774085cfab2ee6b721c7fd1b6037a94be9 Mon Sep 17 00:00:00 2001 From: Giuseppe Coco Date: Wed, 4 Dec 2024 23:30:14 +0100 Subject: [PATCH 3/4] feat: issue #980 --- core/cat/mad_hatter/mad_hatter.py | 12 ++++++------ core/cat/mad_hatter/plugin.py | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/core/cat/mad_hatter/mad_hatter.py b/core/cat/mad_hatter/mad_hatter.py index 5983983a3..cfc131e70 100644 --- a/core/cat/mad_hatter/mad_hatter.py +++ b/core/cat/mad_hatter/mad_hatter.py @@ -200,9 +200,9 @@ def toggle_plugin(self, plugin_id): # Execute hook on plugin deactivation # Deactivation hook must happen before actual deactivation, # otherwise the hook will not be available in _plugin_overrides anymore - for hook in self.plugins[plugin_id]._plugin_overrides: - if hook.name == "deactivated": - hook.function(self.plugins[plugin_id]) + for name, function in self.plugins[plugin_id]._plugin_overrides.items(): + if name == "deactivated": + function(self.plugins[plugin_id]) # Deactivate the plugin self.plugins[plugin_id].deactivate() @@ -221,9 +221,9 @@ def toggle_plugin(self, plugin_id): # Execute hook on plugin activation # Activation hook must happen before actual activation, # otherwise the hook will still not be available in _plugin_overrides - for hook in self.plugins[plugin_id]._plugin_overrides: - if hook.name == "activated": - hook.function(self.plugins[plugin_id]) + for name, function in self.plugins[plugin_id]._plugin_overrides.items(): + if name == "activated": + function(self.plugins[plugin_id]) # Add the plugin in the list of active plugins self.active_plugins.append(plugin_id) diff --git a/core/cat/mad_hatter/plugin.py b/core/cat/mad_hatter/plugin.py index 2c13b3775..5ee53e187 100644 --- a/core/cat/mad_hatter/plugin.py +++ b/core/cat/mad_hatter/plugin.py @@ -108,12 +108,12 @@ def deactivate(self): def settings_schema(self): # is "settings_schema" hook defined in the plugin? if "settings_schema" in self._plugin_overrides: - return self._plugin_overrides["settings_schema"]() + return self._plugin_overrides["settings_schema"].function() else: # if the "settings_schema" is not defined but # "settings_model" is it get the schema from the model if "settings_model" in self._plugin_overrides: - return self._plugin_overrides["settings_model"]().model_json_schema() + return self._plugin_overrides["settings_model"].function().model_json_schema() # default schema (empty) return PluginSettingsModel.model_json_schema() @@ -122,7 +122,7 @@ def settings_schema(self): def settings_model(self): # is "settings_model" hook defined in the plugin? if "settings_model" in self._plugin_overrides: - return self._plugin_overrides["settings_model"]() + return self._plugin_overrides["settings_model"].function() # default schema (empty) return PluginSettingsModel @@ -131,7 +131,7 @@ def settings_model(self): def load_settings(self): # is "settings_load" hook defined in the plugin? if "load_settings" in self._plugin_overrides: - return self._plugin_overrides["load_settings"]() + return self._plugin_overrides["load_settings"].function() # by default, plugin settings are saved inside the plugin folder # in a JSON file called settings.json @@ -157,7 +157,7 @@ def load_settings(self): def save_settings(self, settings: Dict): # is "settings_save" hook defined in the plugin? if "save_settings" in self._plugin_overrides: - return self._plugin_overrides["save_settings"](settings) + return self._plugin_overrides["save_settings"].function(settings) # by default, plugin settings are saved inside the plugin folder # in a JSON file called settings.json @@ -327,7 +327,7 @@ def _load_decorated_functions(self): self._tools = list(map(self._clean_tool, tools)) self._forms = list(map(self._clean_form, forms)) self._endpoints = list(map(self._clean_endpoint, endpoints)) - self._plugin_overrides = {override.name: override for _, override in plugin_overrides} + self._plugin_overrides = {override.name: override for override in list(map(self._clean_plugin_override, plugin_overrides))} def plugin_specific_error_message(self): From a95a582e1be810f5fb32c8be77b3f9dbefba7880 Mon Sep 17 00:00:00 2001 From: Giuseppe Coco Date: Thu, 5 Dec 2024 10:48:42 +0100 Subject: [PATCH 4/4] fix: applied changes suggested by @matteocacciola --- core/cat/mad_hatter/mad_hatter.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/core/cat/mad_hatter/mad_hatter.py b/core/cat/mad_hatter/mad_hatter.py index cfc131e70..5519ed25a 100644 --- a/core/cat/mad_hatter/mad_hatter.py +++ b/core/cat/mad_hatter/mad_hatter.py @@ -200,9 +200,8 @@ def toggle_plugin(self, plugin_id): # Execute hook on plugin deactivation # Deactivation hook must happen before actual deactivation, # otherwise the hook will not be available in _plugin_overrides anymore - for name, function in self.plugins[plugin_id]._plugin_overrides.items(): - if name == "deactivated": - function(self.plugins[plugin_id]) + if "deactivated" in self.plugins[plugin_id]._plugin_overrides: + self.plugins[plugin_id]._plugin_overrides["deactivated"].function(self.plugins[plugin_id]) # Deactivate the plugin self.plugins[plugin_id].deactivate() @@ -221,9 +220,8 @@ def toggle_plugin(self, plugin_id): # Execute hook on plugin activation # Activation hook must happen before actual activation, # otherwise the hook will still not be available in _plugin_overrides - for name, function in self.plugins[plugin_id]._plugin_overrides.items(): - if name == "activated": - function(self.plugins[plugin_id]) + if "activated" in self.plugins[plugin_id]._plugin_overrides: + self.plugins[plugin_id]._plugin_overrides["activated"].function(self.plugins[plugin_id]) # Add the plugin in the list of active plugins self.active_plugins.append(plugin_id)