From c3d5241e29f6a0f6cbaad5a7dd2c20fa7a8cbb90 Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Sun, 1 Feb 2026 03:56:27 +0000 Subject: [PATCH] Initial upload: ErrorFix CLI with rule engine and pattern matching --- errorfix/plugins/loader.py | 72 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 errorfix/plugins/loader.py diff --git a/errorfix/plugins/loader.py b/errorfix/plugins/loader.py new file mode 100644 index 0000000..afce274 --- /dev/null +++ b/errorfix/plugins/loader.py @@ -0,0 +1,72 @@ +import sys +from typing import List, Optional, Dict, Any + +from .plugin import Plugin, PluginRegistry + + +class PluginLoader: + def __init__(self, registry: Optional[PluginRegistry] = None): + self.registry = registry or PluginRegistry() + self._entry_point_name = 'errorfix.plugins' + + def load_entry_points(self) -> List[Plugin]: + plugins = [] + try: + import importlib.metadata + eps = importlib.metadata.entry_points() + if hasattr(eps, 'select'): + entry_points = eps.select(group=self._entry_point_name) + else: + entry_points = eps.get(self._entry_point_name, []) + + for ep in entry_points: + try: + plugin_class = ep.load() + plugin = plugin_class() + self.registry.register(plugin) + plugins.append(plugin) + except Exception as e: + print(f"Warning: Failed to load plugin {ep.name}: {e}") + except Exception as e: + print(f"Warning: Could not load entry points: {e}") + return plugins + + def load_plugin_module(self, module_name: str) -> Plugin: + module = __import__(module_name, fromlist=['']) + if not hasattr(module, 'register'): + raise ValueError(f"Module {module_name} does not have a 'register' function") + + plugin = module.register() + if not isinstance(plugin, Plugin): + raise ValueError(f"register() must return a Plugin instance") + + self.registry.register(plugin) + return plugin + + def load_from_path(self, path: str) -> List[Plugin]: + import importlib.util + + plugins = [] + import os + if os.path.isdir(path): + for filename in os.listdir(path): + if filename.endswith('.py') and not filename.startswith('_'): + module_name = filename[:-3] + file_path = os.path.join(path, filename) + spec = importlib.util.spec_from_file_location(module_name, file_path) + if spec and spec.loader: + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + if hasattr(module, 'register'): + plugin = module.register() + if isinstance(plugin, Plugin): + self.registry.register(plugin) + plugins.append(plugin) + return plugins + + def discover_and_load(self) -> List[Plugin]: + plugins = self.load_entry_points() + return plugins + + def get_registry(self) -> PluginRegistry: + return self.registry