File size: 2,782 Bytes
e58615a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from __future__ import annotations

import argparse
from pathlib import Path

from racing_reports.artifacts import DEFAULT_REPORT_ARTIFACTS
from racing_reports.datastore import DataStore
from racing_reports.match_index import MatchIndex
from racing_reports.runner import ReportRunner, request_from_yaml


def _sync(args) -> None:
    store = DataStore()
    path = store.sync_preprocessed(args.league, args.season, force=args.force)
    print(path)
    artifacts = list(args.artifact or [])
    if args.with_artifacts:
        artifacts.extend(DEFAULT_REPORT_ARTIFACTS)
    for artifact in dict.fromkeys(artifacts):
        print(store.sync_artifact(artifact, force=args.force))


def _list_matches(args) -> None:
    idx = MatchIndex(DataStore())
    df = idx.build(args.league, args.season)
    if args.query:
        q = args.query.lower()
        mask = (
            df["home_team"].str.lower().str.contains(q, na=False)
            | df["away_team"].str.lower().str.contains(q, na=False)
            | df["match_id"].str.lower().str.contains(q, na=False)
            | df["date"].str.lower().str.contains(q, na=False)
        )
        df = df[mask]
    print(df.to_string(index=False))


def _run(args) -> None:
    request = request_from_yaml(Path(args.config))
    result = ReportRunner().run(request)
    print(f"Output dir: {result.output_dir}")
    for name, path in result.html_paths.items():
        print(f"{name}: {path}")
    print(f"manifest: {result.manifest_path}")


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(prog="racing-reports")
    sub = parser.add_subparsers(required=True)

    sync = sub.add_parser("sync", help="Descarga preprocessed/artifacts desde Azure al cache local.")
    sync.add_argument("--league", required=True)
    sync.add_argument("--season", required=True)
    sync.add_argument("--force", action="store_true")
    sync.add_argument("--with-artifacts", action="store_true", help="Descarga artifacts estándar para reportes predictivos.")
    sync.add_argument("--artifact", action="append", help="Artifact relativo a AZURE_REPORT_ARTIFACTS_ROOT.")
    sync.set_defaults(func=_sync)

    lm = sub.add_parser("list-matches", help="Lista partidos disponibles en el preprocessed cacheado.")
    lm.add_argument("--league", required=True)
    lm.add_argument("--season", required=True)
    lm.add_argument("--query")
    lm.set_defaults(func=_list_matches)

    run = sub.add_parser("run", help="Genera reportes desde un YAML.")
    run.add_argument("--config", required=True)
    run.set_defaults(func=_run)
    return parser


def main(argv: list[str] | None = None) -> None:
    parser = build_parser()
    args = parser.parse_args(argv)
    args.func(args)


if __name__ == "__main__":
    main()