aboutsummaryrefslogtreecommitdiff
path: root/devtools/export_framesed_classes.py
blob: e02752aed3c409d9a32df5c00bdbb0d6c5e1d94d (plain) (blame)
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
"""Generate C class metadata from the model-ordered index,mid,display_name CSV."""

import argparse
import csv
import json
from pathlib import Path


def render(path: Path) -> str:
    with path.open(newline="", encoding="utf-8") as source:
        rows = list(csv.DictReader(source))
    if len(rows) != 447:
        raise ValueError("frame_mn10 requires exactly 447 classes")
    mids = set()
    labels = set()
    lines = ["/* Model output order; regenerate with make regenerate-classes. */"]
    for index, row in enumerate(rows):
        mid, label = row["mid"], row["display_name"]
        if int(row["index"]) != index:
            raise ValueError("class indices must be contiguous and ordered from zero")
        if not mid or not label or mid in mids or label in labels:
            raise ValueError("class IDs and labels must be nonempty and unique")
        if any(ord(c) < 32 for c in mid + label):
            raise ValueError("class metadata must not contain control characters")
        mids.add(mid)
        labels.add(label)
        lines.append(f"    {{{json.dumps(mid)}, {json.dumps(label)}}},")
    return "\n".join(lines) + "\n"


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("labels", type=Path)
    parser.add_argument("output", type=Path)
    parser.add_argument("--check", action="store_true")
    args = parser.parse_args()
    content = render(args.labels)
    if args.check:
        if args.output.read_text(encoding="utf-8") != content:
            parser.exit(1, f"{args.output}: regenerate with make regenerate-classes\n")
    else:
        args.output.write_text(content, encoding="utf-8")


if __name__ == "__main__":
    main()