Added plugin conflict check

This commit is contained in:
Pünkösd Marcell 2021-04-09 00:15:32 +02:00
parent 49a5c07f98
commit fb2c243255

View File

@ -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}")