Command-line validation of op_system specifications.
Usage::
python -m op_system.validate CONFIG.yml [CONFIG.yml ...] [--json]
Each file may be a bare op_system spec or a flepimop2 configuration whose
system entries use module: op_system. The exit code is 0 when every
spec passes and 1 otherwise. Reading YAML requires PyYAML.
main(argv=None)
Validate every op_system spec in the given files.
Returns:
| Type |
Description |
int
|
0 when every spec passes, otherwise 1.
|
Source code in src/op_system/validate.py
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 | def main(argv: Sequence[str] | None = None) -> int:
"""Validate every op_system spec in the given files.
Returns:
``0`` when every spec passes, otherwise ``1``.
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("paths", nargs="+", type=Path)
parser.add_argument("--json", action="store_true")
parser.add_argument(
"--detect-gauges",
action="store_true",
help="Detect supported shared positive scaling actions",
)
parser.add_argument(
"--gauge-fix",
action="append",
type=_anchor_argument,
default=[],
metavar="NAME=VALUE",
help="Pin an independent scalar scaling anchor; repeat for each generator",
)
args = parser.parse_args(argv)
anchors: dict[str, float] = {}
for name, value in args.gauge_fix:
if name in anchors:
parser.error("--gauge-fix needs unique scalar anchors")
anchors[name] = value
results = [
(
label,
validate_spec(
spec,
detect_gauges=args.detect_gauges,
gauge_fix=anchors if args.gauge_fix else None,
),
)
for path in args.paths
for label, spec in specs_from_file(path)
]
if args.json:
print(json.dumps({label: asdict(r) for label, r in results}, indent=2)) # ruff: ignore[print]
else:
for label, report in results:
print("\n".join(_describe(label, report))) # ruff: ignore[print]
return 0 if all(report.ok for _, report in results) else 1
|
specs_from_file(path)
Return (label, spec) pairs from a spec or flepimop2 config file.
Raises:
| Type |
Description |
RuntimeError
|
If PyYAML is not installed.
|
Source code in src/op_system/validate.py
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 | def specs_from_file(path: Path) -> list[tuple[str, dict[str, Any]]]:
"""Return ``(label, spec)`` pairs from a spec or flepimop2 config file.
Raises:
RuntimeError: If PyYAML is not installed.
"""
try:
import yaml # type: ignore[import-untyped] # ruff: ignore[import-outside-top-level]
except ImportError as exc: # pragma: no cover - environment dependent
msg = "op_system.validate needs PyYAML to read configuration files"
raise RuntimeError(msg) from exc
loaded = yaml.safe_load(path.read_text(encoding="utf-8")) or {}
systems = loaded.get("system") if isinstance(loaded, dict) else None
if isinstance(systems, list):
return [
(f"{path}[system {index}]", dict(entry["spec"]))
for index, entry in enumerate(systems)
if isinstance(entry, dict) and entry.get("module") == "op_system"
]
return [(str(path), dict(loaded))]
|