|
14 | 14 | # limitations under the License. |
15 | 15 |
|
16 | 16 | import importlib |
| 17 | +import inspect |
17 | 18 | import os |
18 | 19 | import pkgutil |
19 | 20 | import sys |
20 | 21 |
|
21 | 22 |
|
22 | | -def _discover_external_plugins(base_path: str) -> None: |
23 | | - """Import all top-level modules and packages under base_path (ENGINES/), |
24 | | - recursively importing subpackages to trigger engine registration side-effects. |
25 | | - """ |
26 | | - try: |
27 | | - if not os.path.isdir(base_path): |
28 | | - return |
29 | | - if base_path not in sys.path: |
30 | | - sys.path.insert(0, base_path) |
31 | | - # Import only top-level PACKAGES (directories with __init__.py). Skip bare modules. |
32 | | - for _finder, name, ispkg in pkgutil.iter_modules([base_path]): |
33 | | - if not ispkg: |
34 | | - # Enforce package-only engines |
35 | | - continue |
| 23 | +def _import_engine_modules(base_path: str, project_root: str | None = None) -> list[object]: |
| 24 | + """Import all engine packages and submodules found under ENGINES/ dynamically.""" |
| 25 | + imported: list[object] = [] |
| 26 | + if not os.path.isdir(base_path): |
| 27 | + return imported |
| 28 | + |
| 29 | + # Prefer project-root namespace imports (ENGINES.<name>) to avoid collisions. |
| 30 | + if project_root and project_root not in sys.path: |
| 31 | + sys.path.insert(0, project_root) |
| 32 | + if base_path not in sys.path: |
| 33 | + sys.path.insert(0, base_path) |
| 34 | + |
| 35 | + for _finder, name, ispkg in pkgutil.iter_modules([base_path]): |
| 36 | + if not ispkg: |
| 37 | + continue |
| 38 | + |
| 39 | + module_names: list[str] = [] |
| 40 | + if project_root: |
| 41 | + module_names.append(f"ENGINES.{name}") |
| 42 | + module_names.append(name) |
| 43 | + |
| 44 | + package = None |
| 45 | + for mod_name in module_names: |
36 | 46 | try: |
37 | | - # Import the package so its __init__ can self-register the engine |
38 | | - importlib.import_module(name) |
39 | | - # Import submodules to trigger additional registrations if needed |
40 | | - pkg_path = os.path.join(base_path, name) |
41 | | - for __f, subname, __ispkg in pkgutil.walk_packages( |
42 | | - [pkg_path], prefix=f"{name}." |
43 | | - ): |
44 | | - try: |
45 | | - importlib.import_module(subname) |
46 | | - except Exception: |
47 | | - # Ignore broken submodules to avoid crashing global discovery |
48 | | - pass |
| 47 | + package = importlib.import_module(mod_name) |
| 48 | + imported.append(package) |
| 49 | + break |
49 | 50 | except Exception: |
50 | | - # Ignore broken plugins; do not crash host discovery |
51 | | - pass |
| 51 | + continue |
| 52 | + if package is None: |
| 53 | + continue |
| 54 | + |
| 55 | + pkg_name = getattr(package, "__name__", "") |
| 56 | + pkg_path = os.path.join(base_path, name) |
| 57 | + try: |
| 58 | + for _f, sub_name, _is_pkg in pkgutil.walk_packages( |
| 59 | + [pkg_path], prefix=f"{pkg_name}." |
| 60 | + ): |
| 61 | + try: |
| 62 | + sub = importlib.import_module(sub_name) |
| 63 | + imported.append(sub) |
| 64 | + except Exception: |
| 65 | + continue |
| 66 | + except Exception: |
| 67 | + continue |
| 68 | + |
| 69 | + return imported |
| 70 | + |
| 71 | + |
| 72 | +def _register_discovered_engines(modules: list[object]) -> None: |
| 73 | + """Register all CompilerEngine subclasses discovered in imported modules.""" |
| 74 | + try: |
| 75 | + from EngineLoader import registry as _registry # local import to avoid cycles |
| 76 | + from EngineLoader.base import CompilerEngine |
52 | 77 | except Exception: |
53 | | - # Never let discovery break the host application |
54 | | - pass |
| 78 | + return |
| 79 | + |
| 80 | + for mod in modules: |
| 81 | + try: |
| 82 | + for _, cls in inspect.getmembers(mod, inspect.isclass): |
| 83 | + try: |
| 84 | + if not issubclass(cls, CompilerEngine) or cls is CompilerEngine: |
| 85 | + continue |
| 86 | + except Exception: |
| 87 | + continue |
| 88 | + try: |
| 89 | + eid = getattr(cls, "id", None) |
| 90 | + if not isinstance(eid, str) or not eid.strip(): |
| 91 | + continue |
| 92 | + _registry.register(cls) |
| 93 | + except Exception: |
| 94 | + continue |
| 95 | + except Exception: |
| 96 | + continue |
55 | 97 |
|
56 | 98 |
|
57 | 99 | def _auto_discover() -> None: |
58 | | - """Discover and register external engine plugins ONLY from the 'ENGINES' folder at project root.""" |
| 100 | + """Discover and register engine plugins dynamically from the ENGINES folder.""" |
59 | 101 | base_dir = os.path.dirname(__file__) |
60 | 102 | try: |
61 | 103 | project_root = os.path.abspath(os.path.join(base_dir, os.pardir, os.pardir)) |
62 | 104 | external_dir = os.path.join(project_root, "ENGINES") |
63 | | - _discover_external_plugins(external_dir) |
| 105 | + modules = _import_engine_modules(external_dir, project_root=project_root) |
| 106 | + _register_discovered_engines(modules) |
64 | 107 | except Exception: |
65 | 108 | pass |
0 commit comments