From fb2c243255bfc2d9e6bbcba1130b40c688c40b7c Mon Sep 17 00:00:00 2001 From: marcsello Date: Fri, 9 Apr 2021 00:15:32 +0200 Subject: [PATCH] Added plugin conflict check --- single_ursim_control/plugin_repository.py | 34 +++++++++++++++++++---- 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/single_ursim_control/plugin_repository.py b/single_ursim_control/plugin_repository.py index b317a0b..3caa236 100644 --- a/single_ursim_control/plugin_repository.py +++ b/single_ursim_control/plugin_repository.py @@ -3,11 +3,15 @@ from plugins import AbstractPlugin, AbstractCommandCompiler import logging +class ConflictingPlugins(BaseException): + pass + + class PluginRepository: def __init__(self): self._registered_plugins: Dict[str, type(AbstractPlugin)] = {} - self._loaded_plugins: List[AbstractPlugin] = [] + self._loaded_plugins: Dict[str, AbstractPlugin] = {} self._command_compilers: Dict[str, AbstractCommandCompiler] = {} self._logger = logging.getLogger("plugin_repository") @@ -16,20 +20,38 @@ class PluginRepository: self._logger.debug(f"Registered plugin: {plugin_class.plugin_name}") def load_plugin(self, plugin: str): + + if plugin in self._loaded_plugins.keys(): + self._logger.warning(f"Plugin {plugin} already loaded!") + return + + # create instance plugin_instance = self._registered_plugins[plugin]() # config is statically loaded - self._loaded_plugins.append(plugin_instance) + # store instance + self._loaded_plugins[plugin] = plugin_instance + # load compilers compilers = plugin_instance.load_compilers() + + if set(compilers.keys()).intersection(self._command_compilers.keys()): + raise ConflictingPlugins(f"Conflicting plugin is tried to be loaded: {plugin}") + + # Store commands self._command_compilers.update(compilers) + + # log stuff self._logger.info(f"Loaded plugin: {plugin}") self._logger.debug(f"Plugin {plugin} loaded the following commands: {', '.join(compilers.keys())}") def get_compiler(self, command: str) -> AbstractCommandCompiler: return self._command_compilers[command] + def get_plugin_instance(self, plugin: str) -> AbstractPlugin: + return self._loaded_plugins[plugin] + def close(self): - self._command_compilers = [] - for plugin in self._loaded_plugins: - plugin.close() - self._logger.info(f"Unloaded plugin: {plugin.plugin_name}") + self._command_compilers = {} + for plugin_name, plugin_instance in self._loaded_plugins: + plugin_instance.close() + self._logger.info(f"Unloaded plugin: {plugin_name}")