semantics
This commit is contained in:
@@ -5,22 +5,20 @@ from typing import Dict, List, Optional
|
||||
from .plugin_base import PluginBase, PluginConfig
|
||||
from .logger import belief_scope
|
||||
|
||||
# [DEF:PluginLoader:Class]
|
||||
# @COMPLEXITY: 3
|
||||
# @SEMANTICS: plugin, loader, dynamic, import
|
||||
# @PURPOSE: Scans a specified directory for Python modules, dynamically loads them, and registers any classes that are valid implementations of the PluginBase interface.
|
||||
# @LAYER: Core
|
||||
# @RELATION: Depends on PluginBase. It is used by the main application to discover and manage available plugins.
|
||||
# #region PluginLoader [C:3] [TYPE Class] [SEMANTICS plugin, loader, dynamic, import]
|
||||
# @BRIEF Scans a specified directory for Python modules, dynamically loads them, and registers any classes that are valid implementations of the PluginBase interface.
|
||||
# @LAYER Core
|
||||
# @RELATION Depends on PluginBase. It is used by the main application to discover and manage available plugins.
|
||||
class PluginLoader:
|
||||
"""
|
||||
Scans a directory for Python modules, loads them, and identifies classes
|
||||
that inherit from PluginBase.
|
||||
"""
|
||||
|
||||
# [DEF:__init__:Function]
|
||||
# @PURPOSE: Initializes the PluginLoader with a directory to scan.
|
||||
# @PRE: plugin_dir is a valid directory path.
|
||||
# @POST: Plugins are loaded and registered.
|
||||
# #region __init__ [TYPE Function]
|
||||
# @BRIEF Initializes the PluginLoader with a directory to scan.
|
||||
# @PRE plugin_dir is a valid directory path.
|
||||
# @POST Plugins are loaded and registered.
|
||||
# @PARAM: plugin_dir (str) - The directory containing plugin modules.
|
||||
def __init__(self, plugin_dir: str):
|
||||
with belief_scope("__init__"):
|
||||
@@ -28,12 +26,12 @@ class PluginLoader:
|
||||
self._plugins: Dict[str, PluginBase] = {}
|
||||
self._plugin_configs: Dict[str, PluginConfig] = {}
|
||||
self._load_plugins()
|
||||
# [/DEF:__init__:Function]
|
||||
# #endregion __init__
|
||||
|
||||
# [DEF:_load_plugins:Function]
|
||||
# @PURPOSE: Scans the plugin directory and loads all valid plugins.
|
||||
# @PRE: plugin_dir exists or can be created.
|
||||
# @POST: _load_module is called for each .py file.
|
||||
# #region _load_plugins [TYPE Function]
|
||||
# @BRIEF Scans the plugin directory and loads all valid plugins.
|
||||
# @PRE plugin_dir exists or can be created.
|
||||
# @POST _load_module is called for each .py file.
|
||||
def _load_plugins(self):
|
||||
with belief_scope("_load_plugins"):
|
||||
"""
|
||||
@@ -63,12 +61,12 @@ class PluginLoader:
|
||||
if filename.endswith(".py") and filename != "__init__.py":
|
||||
module_name = filename[:-3]
|
||||
self._load_module(module_name, file_path)
|
||||
# [/DEF:_load_plugins:Function]
|
||||
# #endregion _load_plugins
|
||||
|
||||
# [DEF:_load_module:Function]
|
||||
# @PURPOSE: Loads a single Python module and discovers PluginBase implementations.
|
||||
# @PRE: module_name and file_path are valid.
|
||||
# @POST: Plugin classes are instantiated and registered.
|
||||
# #region _load_module [TYPE Function]
|
||||
# @BRIEF Loads a single Python module and discovers PluginBase implementations.
|
||||
# @PRE module_name and file_path are valid.
|
||||
# @POST Plugin classes are instantiated and registered.
|
||||
# @PARAM: module_name (str) - The name of the module.
|
||||
# @PARAM: file_path (str) - The path to the module file.
|
||||
def _load_module(self, module_name: str, file_path: str):
|
||||
@@ -104,12 +102,12 @@ class PluginLoader:
|
||||
self._register_plugin(plugin_instance)
|
||||
except Exception as e:
|
||||
print(f"Error instantiating plugin {attribute_name} in {module_name}: {e}") # Replace with proper logging
|
||||
# [/DEF:_load_module:Function]
|
||||
# #endregion _load_module
|
||||
|
||||
# [DEF:_register_plugin:Function]
|
||||
# @PURPOSE: Registers a PluginBase instance and its configuration.
|
||||
# @PRE: plugin_instance is a valid implementation of PluginBase.
|
||||
# @POST: Plugin is added to _plugins and _plugin_configs.
|
||||
# #region _register_plugin [TYPE Function]
|
||||
# @BRIEF Registers a PluginBase instance and its configuration.
|
||||
# @PRE plugin_instance is a valid implementation of PluginBase.
|
||||
# @POST Plugin is added to _plugins and _plugin_configs.
|
||||
# @PARAM: plugin_instance (PluginBase) - The plugin instance to register.
|
||||
def _register_plugin(self, plugin_instance: PluginBase):
|
||||
with belief_scope("_register_plugin"):
|
||||
@@ -145,13 +143,13 @@ class PluginLoader:
|
||||
except Exception as e:
|
||||
from ..core.logger import logger
|
||||
logger.error(f"Error validating plugin '{plugin_instance.name}' (ID: {plugin_id}): {e}")
|
||||
# [/DEF:_register_plugin:Function]
|
||||
# #endregion _register_plugin
|
||||
|
||||
|
||||
# [DEF:get_plugin:Function]
|
||||
# @PURPOSE: Retrieves a loaded plugin instance by its ID.
|
||||
# @PRE: plugin_id is a string.
|
||||
# @POST: Returns plugin instance or None.
|
||||
# #region get_plugin [TYPE Function]
|
||||
# @BRIEF Retrieves a loaded plugin instance by its ID.
|
||||
# @PRE plugin_id is a string.
|
||||
# @POST Returns plugin instance or None.
|
||||
# @PARAM: plugin_id (str) - The unique identifier of the plugin.
|
||||
# @RETURN: Optional[PluginBase] - The plugin instance if found, otherwise None.
|
||||
def get_plugin(self, plugin_id: str) -> Optional[PluginBase]:
|
||||
@@ -160,25 +158,25 @@ class PluginLoader:
|
||||
Returns a loaded plugin instance by its ID.
|
||||
"""
|
||||
return self._plugins.get(plugin_id)
|
||||
# [/DEF:get_plugin:Function]
|
||||
# #endregion get_plugin
|
||||
|
||||
# [DEF:get_all_plugin_configs:Function]
|
||||
# @PURPOSE: Returns a list of all registered plugin configurations.
|
||||
# @PRE: None.
|
||||
# @POST: Returns list of all PluginConfig objects.
|
||||
# @RETURN: List[PluginConfig] - A list of plugin configurations.
|
||||
# #region get_all_plugin_configs [TYPE Function]
|
||||
# @BRIEF Returns a list of all registered plugin configurations.
|
||||
# @PRE None.
|
||||
# @POST Returns list of all PluginConfig objects.
|
||||
# @RETURN List[PluginConfig] - A list of plugin configurations.
|
||||
def get_all_plugin_configs(self) -> List[PluginConfig]:
|
||||
with belief_scope("get_all_plugin_configs"):
|
||||
"""
|
||||
Returns a list of all loaded plugin configurations.
|
||||
"""
|
||||
return list(self._plugin_configs.values())
|
||||
# [/DEF:get_all_plugin_configs:Function]
|
||||
# #endregion get_all_plugin_configs
|
||||
|
||||
# [DEF:has_plugin:Function]
|
||||
# @PURPOSE: Checks if a plugin with the given ID is registered.
|
||||
# @PRE: plugin_id is a string.
|
||||
# @POST: Returns True if plugin exists.
|
||||
# #region has_plugin [TYPE Function]
|
||||
# @BRIEF Checks if a plugin with the given ID is registered.
|
||||
# @PRE plugin_id is a string.
|
||||
# @POST Returns True if plugin exists.
|
||||
# @PARAM: plugin_id (str) - The unique identifier of the plugin.
|
||||
# @RETURN: bool - True if the plugin is registered, False otherwise.
|
||||
def has_plugin(self, plugin_id: str) -> bool:
|
||||
@@ -187,6 +185,6 @@ class PluginLoader:
|
||||
Checks if a plugin with the given ID is loaded.
|
||||
"""
|
||||
return plugin_id in self._plugins
|
||||
# [/DEF:has_plugin:Function]
|
||||
# #endregion has_plugin
|
||||
|
||||
# [/DEF:PluginLoader:Class]
|
||||
# #endregion PluginLoader
|
||||
|
||||
Reference in New Issue
Block a user