This commit is contained in:
35
single_ursim_control/plugin_repository.py
Normal file
35
single_ursim_control/plugin_repository.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from typing import Dict, List
|
||||
from plugins import AbstractPlugin, AbstractCommandCompiler
|
||||
import logging
|
||||
|
||||
|
||||
class PluginRepository:
|
||||
|
||||
def __init__(self):
|
||||
self._registered_plugins: Dict[str, type(AbstractPlugin)] = {}
|
||||
self._loaded_plugins: List[AbstractPlugin] = []
|
||||
self._command_compilers: Dict[str, AbstractCommandCompiler] = {}
|
||||
self._logger = logging.getLogger("plugin_repository")
|
||||
|
||||
def register_plugin(self, plugin_class: type(AbstractPlugin)):
|
||||
self._registered_plugins[plugin_class.plugin_name] = plugin_class
|
||||
self._logger.debug(f"Registered plugin: {plugin_class.plugin_name}")
|
||||
|
||||
def load_plugin(self, plugin: str):
|
||||
plugin_instance = self._registered_plugins[plugin]() # config is statically loaded
|
||||
|
||||
self._loaded_plugins.append(plugin_instance)
|
||||
|
||||
compilers = plugin_instance.load_compilers()
|
||||
self._command_compilers.update(compilers)
|
||||
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 close(self):
|
||||
self._command_compilers = []
|
||||
for plugin in self._loaded_plugins:
|
||||
plugin.close()
|
||||
self._logger.info(f"Unloaded plugin: {plugin.plugin_name}")
|
||||
Reference in New Issue
Block a user