|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build the dedicated producer-owned political-event weekly artifact.""" |
| 3 | + |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import argparse |
| 7 | +import json |
| 8 | +import sys |
| 9 | +from datetime import date, datetime, timezone |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) |
| 13 | + |
| 14 | +from political_event_tracking_research.weekly_artifact import ( # noqa: E402 |
| 15 | + WeeklyArtifactError, |
| 16 | + build_weekly_artifact, |
| 17 | + completed_week_period, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def _date(value: str) -> date: |
| 22 | + try: |
| 23 | + return date.fromisoformat(value) |
| 24 | + except ValueError: |
| 25 | + raise argparse.ArgumentTypeError("invalid ISO date") from None |
| 26 | + |
| 27 | + |
| 28 | +def _generated_at(value: str) -> datetime: |
| 29 | + try: |
| 30 | + parsed = datetime.fromisoformat(value.replace("Z", "+00:00")) |
| 31 | + except ValueError: |
| 32 | + raise argparse.ArgumentTypeError("invalid UTC timestamp") from None |
| 33 | + if parsed.tzinfo != timezone.utc: |
| 34 | + raise argparse.ArgumentTypeError("generated_at must be UTC") |
| 35 | + return parsed |
| 36 | + |
| 37 | + |
| 38 | +def main() -> None: |
| 39 | + parser = argparse.ArgumentParser(description=__doc__) |
| 40 | + parser.add_argument("--period-start", type=_date) |
| 41 | + parser.add_argument("--as-of", type=_date) |
| 42 | + parser.add_argument("--scheduled-today", type=_date) |
| 43 | + parser.add_argument("--generated-at", required=True, type=_generated_at) |
| 44 | + parser.add_argument("--workflow-ref", required=True) |
| 45 | + parser.add_argument("--source-run-id", required=True) |
| 46 | + parser.add_argument("--producer-ref", required=True) |
| 47 | + parser.add_argument("--source-events", required=True, type=Path) |
| 48 | + parser.add_argument("--watchlist", required=True, type=Path) |
| 49 | + parser.add_argument("--feed-status", required=True, type=Path) |
| 50 | + parser.add_argument("--output-dir", required=True, type=Path) |
| 51 | + parser.add_argument("--run-mode", choices=("scheduled", "manual"), required=True) |
| 52 | + args = parser.parse_args() |
| 53 | + |
| 54 | + if args.scheduled_today is not None: |
| 55 | + if args.run_mode != "scheduled" or args.period_start is not None or args.as_of is not None: |
| 56 | + parser.error("scheduled mode requires only --scheduled-today") |
| 57 | + period_start, period_end = completed_week_period(args.scheduled_today) |
| 58 | + as_of = period_end - date.resolution |
| 59 | + elif args.run_mode == "manual" and args.period_start is not None and args.as_of is not None: |
| 60 | + period_start, as_of = args.period_start, args.as_of |
| 61 | + else: |
| 62 | + parser.error("manual mode requires --period-start and --as-of") |
| 63 | + |
| 64 | + try: |
| 65 | + feed_status = json.loads(args.feed_status.read_text(encoding="utf-8")) |
| 66 | + files = build_weekly_artifact( |
| 67 | + period_start=period_start, |
| 68 | + as_of=as_of, |
| 69 | + generated_at=args.generated_at, |
| 70 | + workflow_ref=args.workflow_ref, |
| 71 | + source_run_id=args.source_run_id, |
| 72 | + producer_ref=args.producer_ref, |
| 73 | + source_events=args.source_events.read_bytes(), |
| 74 | + watchlist=args.watchlist.read_bytes(), |
| 75 | + feed_status=feed_status, |
| 76 | + source_provenance="official_rss_source_pipeline_v1", |
| 77 | + run_mode=args.run_mode, |
| 78 | + ) |
| 79 | + args.output_dir.mkdir(parents=True, exist_ok=True) |
| 80 | + if any(args.output_dir.iterdir()): |
| 81 | + raise WeeklyArtifactError("artifact_output_not_empty") |
| 82 | + for name, content in files.items(): |
| 83 | + (args.output_dir / name).write_bytes(content) |
| 84 | + except WeeklyArtifactError as exc: |
| 85 | + raise SystemExit(exc.code) from None |
| 86 | + except (OSError, UnicodeError, json.JSONDecodeError, TypeError, ValueError): |
| 87 | + raise SystemExit("weekly_artifact_input_invalid") from None |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + main() |
0 commit comments