Add artifact_2/src/artifact_2/cli.py
This commit is contained in:
parent
f1bef8136a
commit
1a11211508
1 changed files with 68 additions and 0 deletions
68
artifact_2/src/artifact_2/cli.py
Normal file
68
artifact_2/src/artifact_2/cli.py
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
import argparse
|
||||
import json
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from typing import Any, List
|
||||
|
||||
from artifact_2.core import LogEntry, check_timestamp_consistency
|
||||
|
||||
|
||||
def _load_log_entries(file_path: Path) -> List[LogEntry]:
|
||||
if not file_path.exists():
|
||||
raise FileNotFoundError(f"Input file not found: {file_path}")
|
||||
with file_path.open("r", encoding="utf-8") as f:
|
||||
try:
|
||||
data = json.load(f)
|
||||
except json.JSONDecodeError as e:
|
||||
raise ValueError(f"Invalid JSON format: {e}") from e
|
||||
|
||||
if not isinstance(data, list):
|
||||
raise ValueError("Input JSON must be a list of log entries.")
|
||||
|
||||
entries: List[LogEntry] = []
|
||||
for idx, item in enumerate(data):
|
||||
if not isinstance(item, dict):
|
||||
raise ValueError(f"Log entry at index {idx} must be an object.")
|
||||
try:
|
||||
entry = LogEntry(
|
||||
epoch_ms=int(item["epoch_ms"]),
|
||||
monotonic_ns=int(item["monotonic_ns"]),
|
||||
tz_offset_minutes=int(item["tz_offset_minutes"]),
|
||||
run_id=str(item["run_id"]),
|
||||
step_id=str(item["step_id"]),
|
||||
)
|
||||
except KeyError as e:
|
||||
raise ValueError(f"Missing required field in log entry: {e}") from e
|
||||
entries.append(entry)
|
||||
|
||||
return entries
|
||||
|
||||
|
||||
def _parse_args(argv: List[str]) -> argparse.Namespace:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Check timestamp consistency in log entries."
|
||||
)
|
||||
parser.add_argument(
|
||||
"--input",
|
||||
required=True,
|
||||
help="Path to the JSON file containing log entries.",
|
||||
)
|
||||
return parser.parse_args(argv)
|
||||
|
||||
|
||||
def main(argv: List[str] | None = None) -> None:
|
||||
argv = argv if argv is not None else sys.argv[1:]
|
||||
args = _parse_args(argv)
|
||||
input_path = Path(args.input)
|
||||
|
||||
try:
|
||||
log_entries = _load_log_entries(input_path)
|
||||
result = check_timestamp_consistency(log_entries)
|
||||
print("OK" if result else "Inconsistent")
|
||||
except Exception as e:
|
||||
print(f"Error: {e}", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Reference in a new issue