import importlib.util import os import sys # Added this line from typing import Dict, Type, List, Optional from .plugin_base import PluginBase, PluginConfig from jsonschema import validate # [DEF:PluginLoader:Class] # @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. class PluginLoader: """ Scans a directory for Python modules, loads them, and identifies classes that inherit from PluginBase. """ def __init__(self, plugin_dir: str): self.plugin_dir = plugin_dir self._plugins: Dict[str, PluginBase] = {} self._plugin_configs: Dict[str, PluginConfig] = {} self._load_plugins() def _load_plugins(self): """ Scans the plugin directory, imports modules, and registers valid plugins. """ if not os.path.exists(self.plugin_dir): os.makedirs(self.plugin_dir) # Add the plugin directory's parent to sys.path to enable relative imports within plugins # This assumes plugin_dir is something like 'backend/src/plugins' # and we want 'backend/src' to be on the path for 'from ..core...' imports plugin_parent_dir = os.path.abspath(os.path.join(self.plugin_dir, os.pardir)) if plugin_parent_dir not in sys.path: sys.path.insert(0, plugin_parent_dir) for filename in os.listdir(self.plugin_dir): if filename.endswith(".py") and filename != "__init__.py": module_name = filename[:-3] file_path = os.path.join(self.plugin_dir, filename) self._load_module(module_name, file_path) def _load_module(self, module_name: str, file_path: str): """ Loads a single Python module and extracts PluginBase subclasses. """ # Try to determine the correct package prefix based on how the app is running if "backend.src" in __name__: package_prefix = "backend.src.plugins" else: package_prefix = "src.plugins" package_name = f"{package_prefix}.{module_name}" # print(f"DEBUG: Loading plugin {module_name} as {package_name}") spec = importlib.util.spec_from_file_location(package_name, file_path) if spec is None or spec.loader is None: print(f"Could not load module spec for {package_name}") # Replace with proper logging return module = importlib.util.module_from_spec(spec) try: spec.loader.exec_module(module) except Exception as e: print(f"Error loading plugin module {module_name}: {e}") # Replace with proper logging return for attribute_name in dir(module): attribute = getattr(module, attribute_name) if ( isinstance(attribute, type) and issubclass(attribute, PluginBase) and attribute is not PluginBase ): try: plugin_instance = attribute() 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 _register_plugin(self, plugin_instance: PluginBase): """ Registers a valid plugin instance. """ plugin_id = plugin_instance.id if plugin_id in self._plugins: print(f"Warning: Duplicate plugin ID '{plugin_id}' found. Skipping.") # Replace with proper logging return try: schema = plugin_instance.get_schema() # Basic validation to ensure it's a dictionary if not isinstance(schema, dict): raise TypeError("get_schema() must return a dictionary.") plugin_config = PluginConfig( id=plugin_instance.id, name=plugin_instance.name, description=plugin_instance.description, version=plugin_instance.version, schema=schema, ) # The following line is commented out because it requires a schema to be passed to validate against. # The schema provided by the plugin is the one being validated, not the data. # validate(instance={}, schema=schema) self._plugins[plugin_id] = plugin_instance self._plugin_configs[plugin_id] = plugin_config print(f"Plugin '{plugin_instance.name}' (ID: {plugin_id}) loaded successfully.") # Replace with proper logging except Exception as e: print(f"Error validating plugin '{plugin_instance.name}' (ID: {plugin_id}): {e}") # Replace with proper logging def get_plugin(self, plugin_id: str) -> Optional[PluginBase]: """ Returns a loaded plugin instance by its ID. """ return self._plugins.get(plugin_id) def get_all_plugin_configs(self) -> List[PluginConfig]: """ Returns a list of all loaded plugin configurations. """ return list(self._plugin_configs.values()) def has_plugin(self, plugin_id: str) -> bool: """ Checks if a plugin with the given ID is loaded. """ return plugin_id in self._plugins