odcs-check 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
odcs_check/__init__.py ADDED
@@ -0,0 +1 @@
1
+ __version__ = "0.1.0"
odcs_check/__main__.py ADDED
@@ -0,0 +1,3 @@
1
+ from .cli import main
2
+
3
+ raise SystemExit(main())
odcs_check/cli.py ADDED
@@ -0,0 +1,57 @@
1
+ """odcs-check test CONTRACT: exit 0 pass, 1 data broke the contract, 2 could not run the check."""
2
+ import argparse
3
+ import sys
4
+
5
+ from . import __version__
6
+ from .contract import ContractError, load
7
+ from .run import DataError, run
8
+
9
+
10
+ def _parser() -> argparse.ArgumentParser:
11
+ ap = argparse.ArgumentParser(prog="odcs-check", description="Check a Parquet or CSV file against an ODCS v3 contract.")
12
+ ap.add_argument("--version", action="version", version=__version__)
13
+ sub = ap.add_subparsers(dest="cmd", required=True)
14
+ t = sub.add_parser("test", help="run the contract's checks against its data file")
15
+ t.add_argument("contract")
16
+ t.add_argument("--server", help="server name, required when the contract has more than one")
17
+ t.add_argument("--path", help="data file to check instead of the server's path")
18
+ return ap
19
+
20
+
21
+ def _line(*parts: str) -> str:
22
+ return " ".join(" ".join(p.split()) for p in parts).rstrip()
23
+
24
+
25
+ def _test(args) -> int:
26
+ try:
27
+ c = load(args.contract, args.server)
28
+ except ContractError as e:
29
+ for p in e.problems:
30
+ print(_line("contract error", p))
31
+ print(f"odcs-check: {len(e.problems)} contract problem(s) — contract invalid")
32
+ return 2
33
+ try:
34
+ results = run(c, args.path)
35
+ except DataError as e:
36
+ print(_line("data error", str(e)))
37
+ print("odcs-check: data not readable — contract not checked")
38
+ return 2
39
+ for r in results:
40
+ print(_line(r.status, r.check, r.column or "-", r.detail))
41
+ n = lambda s: sum(r.status == s for r in results) # noqa: E731
42
+ skipped = f", {n('not checked')} not checked" if n("not checked") else ""
43
+ ok = n("failed") == 0
44
+ print(f"odcs-check: {len(results)} checks, {n('passed')} passed, {n('failed')} failed, "
45
+ f"{n('warning')} warnings{skipped} — contract {'valid' if ok else 'invalid'}")
46
+ return 0 if ok else 1
47
+
48
+
49
+ def main(argv: list[str] | None = None) -> int:
50
+ if hasattr(sys.stdout, "reconfigure"):
51
+ sys.stdout.reconfigure(encoding="utf-8")
52
+ args = _parser().parse_args(argv) # bad usage: argparse exits 2 itself
53
+ try:
54
+ return _test(args)
55
+ except Exception as e: # noqa: BLE001 - exit 1 means "data broke the contract", never a bug
56
+ print(f"odcs-check: internal error: {type(e).__name__}: {e}", file=sys.stderr)
57
+ return 2