Skip to content

Validate

validate

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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")
    args = parser.parse_args(argv)
    results = [
        (label, validate_spec(spec))
        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))]