|
25 | 25 | """ |
26 | 26 |
|
27 | 27 | import functools |
| 28 | +import importlib |
28 | 29 | import sys |
29 | 30 | from collections.abc import Callable, Mapping |
30 | 31 |
|
@@ -58,6 +59,77 @@ class PhysicsCfg(PresetCfg): |
58 | 59 | pass |
59 | 60 |
|
60 | 61 |
|
| 62 | +class _LazyPreset: |
| 63 | + """Lazy preset alternative backed by an import path.""" |
| 64 | + |
| 65 | + __slots__ = ("error_hint", "import_path", "kwargs") |
| 66 | + |
| 67 | + def __init__(self, import_path: str, error_hint: str | None = None, kwargs: dict[str, object] | None = None): |
| 68 | + self.import_path = import_path |
| 69 | + self.error_hint = error_hint |
| 70 | + self.kwargs = kwargs or {} |
| 71 | + |
| 72 | + def __eq__(self, other: object) -> bool: |
| 73 | + return ( |
| 74 | + isinstance(other, _LazyPreset) |
| 75 | + and self.import_path == other.import_path |
| 76 | + and self.error_hint == other.error_hint |
| 77 | + and self.kwargs == other.kwargs |
| 78 | + ) |
| 79 | + |
| 80 | + def load(self, preset_name: str) -> object: |
| 81 | + module_name, _, attr_name = self.import_path.partition(":") |
| 82 | + try: |
| 83 | + module = importlib.import_module(module_name) |
| 84 | + cfg_cls = getattr(module, attr_name) |
| 85 | + except ModuleNotFoundError as exc: |
| 86 | + raise ModuleNotFoundError(self._format_error(preset_name, exc)) from exc |
| 87 | + except (AttributeError, ImportError) as exc: |
| 88 | + raise ImportError(self._format_error(preset_name, exc)) from exc |
| 89 | + return cfg_cls(**self.kwargs) |
| 90 | + |
| 91 | + def _format_error(self, preset_name: str, exc: Exception) -> str: |
| 92 | + message = f"Preset '{preset_name}' requires config '{self.import_path}', but it could not be imported." |
| 93 | + if self.error_hint: |
| 94 | + message += f" {self.error_hint}" |
| 95 | + message += f" Original error: {exc}" |
| 96 | + return message |
| 97 | + |
| 98 | + |
| 99 | +def lazy_preset( |
| 100 | + import_path: str, |
| 101 | + *, |
| 102 | + error_hint: str | None = None, |
| 103 | + **kwargs: object, |
| 104 | +) -> _LazyPreset: |
| 105 | + """Create a preset alternative that imports its config only when selected. |
| 106 | +
|
| 107 | + The target class is imported and instantiated only when the preset is selected. |
| 108 | + This lets task configs advertise presets without importing their backend |
| 109 | + packages during normal config loading. |
| 110 | +
|
| 111 | + Args: |
| 112 | + import_path: Import path in ``"module:ClassName"`` form. |
| 113 | + error_hint: Optional guidance appended to import errors. |
| 114 | + **kwargs: Keyword arguments forwarded to the imported config class. |
| 115 | +
|
| 116 | + Returns: |
| 117 | + A lazy preset alternative that can be used as a :class:`PresetCfg` field. |
| 118 | +
|
| 119 | + Raises: |
| 120 | + ValueError: If :attr:`import_path` is not in ``"module:ClassName"`` form. |
| 121 | + """ |
| 122 | + module_name, sep, attr_name = import_path.partition(":") |
| 123 | + if not sep or not module_name or not attr_name: |
| 124 | + raise ValueError(f"Lazy preset import path must use 'module:ClassName' form, got: {import_path!r}.") |
| 125 | + return _LazyPreset(import_path, error_hint, kwargs) |
| 126 | + |
| 127 | + |
| 128 | +def _materialize_lazy_preset(value, preset_name: str) -> object: |
| 129 | + """Instantiate a lazy preset if needed.""" |
| 130 | + return value.load(preset_name) if isinstance(value, _LazyPreset) else value |
| 131 | + |
| 132 | + |
61 | 133 | def preset(**options) -> PresetCfg: |
62 | 134 | """Create a :class:`PresetCfg` instance from keyword arguments. |
63 | 135 |
|
@@ -179,9 +251,9 @@ def _pick_alternative(preset_obj: PresetCfg, selected: set[str], path: str = "") |
179 | 251 | fields = _preset_fields(preset_obj) |
180 | 252 | for name in selected: |
181 | 253 | if name in fields: |
182 | | - return fields[name] |
| 254 | + return _materialize_lazy_preset(fields[name], name) |
183 | 255 | if "default" in fields: |
184 | | - return fields["default"] |
| 256 | + return _materialize_lazy_preset(fields["default"], "default") |
185 | 257 | raise ValueError( |
186 | 258 | f"PresetCfg {type(preset_obj).__name__} at '{path}' has no 'default' field " |
187 | 259 | f"and none of the selected presets {selected} match its fields {set(fields.keys())}." |
@@ -522,7 +594,7 @@ def _path_reachable(sec: str, path: str) -> bool: |
522 | 594 | for full_path in sorted(resolved, key=lambda fp: fp.count(".")): |
523 | 595 | sec, path, name = resolved[full_path] |
524 | 596 | if cfgs[sec] is not None and _path_reachable(sec, path): |
525 | | - node = presets[sec][path][name] |
| 597 | + node = _materialize_lazy_preset(presets[sec][path][name], name) |
526 | 598 | node_dict = ( |
527 | 599 | node.to_dict() if hasattr(node, "to_dict") else dict(node) if isinstance(node, Mapping) else node |
528 | 600 | ) |
|
0 commit comments