blob: 719dc6dc86e2e80fd6b23f0f58e946661d6a24d0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
"""Dynamic plugin loader for strategy modules."""
import importlib.util
import sys
from pathlib import Path
from strategies.base import BaseStrategy
def load_strategies(strategies_dir: Path) -> list[BaseStrategy]:
"""Scan strategies_dir for *.py files and load all BaseStrategy subclasses."""
loaded: list[BaseStrategy] = []
for path in sorted(strategies_dir.glob("*.py")):
# Skip dunder files and base
if path.name.startswith("__") or path.name == "base.py":
continue
module_name = f"_strategy_plugin_{path.stem}"
spec = importlib.util.spec_from_file_location(module_name, path)
if spec is None or spec.loader is None:
continue
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
for attr_name in dir(module):
obj = getattr(module, attr_name)
if (
isinstance(obj, type)
and issubclass(obj, BaseStrategy)
and obj is not BaseStrategy
):
loaded.append(obj())
return loaded
|