|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Import revised copy from CSV back into a copydeck JSON. |
| 3 | +
|
| 4 | +Reads the reviewed CSV (produced by export_copydeck_csv.py), updates title/summary/why/steps |
| 5 | +for each variant, and writes the result to a new JSON file. The original if_condition and |
| 6 | +_placeholders metadata are preserved from the source copydeck. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/import_copydeck_csv.py |
| 10 | + python scripts/import_copydeck_csv.py --csv copydeck_review.csv --copydeck copydecks/en/copydeck.json --out copydecks/en/copydeck_revised.json |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +import csv |
| 15 | +import json |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | + |
| 20 | +def import_csv(csv_path: Path, copydeck_path: Path, out_path: Path) -> None: |
| 21 | + with open(copydeck_path) as f: |
| 22 | + data = json.load(f) |
| 23 | + |
| 24 | + with open(csv_path, newline="", encoding="utf-8") as f: |
| 25 | + rows = list(csv.DictReader(f)) |
| 26 | + |
| 27 | + updated = 0 |
| 28 | + for row in rows: |
| 29 | + error_type = row["error_type"] |
| 30 | + variant_index = int(row["variant_index"]) |
| 31 | + |
| 32 | + if error_type not in data["errors"]: |
| 33 | + print(f"Warning: error type '{error_type}' not found in copydeck, skipping", file=sys.stderr) |
| 34 | + continue |
| 35 | + |
| 36 | + variants = data["errors"][error_type]["variants"] |
| 37 | + if variant_index >= len(variants): |
| 38 | + print(f"Warning: variant index {variant_index} out of range for {error_type}, skipping", file=sys.stderr) |
| 39 | + continue |
| 40 | + |
| 41 | + variant = variants[variant_index] |
| 42 | + variant["title"] = row["title"] |
| 43 | + variant["summary"] = row["summary"] |
| 44 | + variant["why"] = row["why"] |
| 45 | + |
| 46 | + steps = [row[f"step_{j}"] for j in range(1, 6) if row.get(f"step_{j}", "").strip()] |
| 47 | + if steps: |
| 48 | + variant["steps"] = steps |
| 49 | + elif "steps" in variant: |
| 50 | + del variant["steps"] |
| 51 | + |
| 52 | + updated += 1 |
| 53 | + |
| 54 | + with open(out_path, "w", encoding="utf-8") as f: |
| 55 | + json.dump(data, f, indent=2, ensure_ascii=False) |
| 56 | + f.write("\n") |
| 57 | + |
| 58 | + print(f"Updated {updated} variants → {out_path}") |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + parser = argparse.ArgumentParser(description="Import reviewed CSV back into copydeck JSON") |
| 63 | + parser.add_argument("--csv", default="copydeck_review.csv", |
| 64 | + help="Input CSV path (default: copydeck_review.csv)") |
| 65 | + parser.add_argument("--copydeck", default="copydecks/en/copydeck.json", |
| 66 | + help="Source copydeck JSON to update (default: copydecks/en/copydeck.json)") |
| 67 | + parser.add_argument("--out", default="copydecks/en/copydeck.json", |
| 68 | + help="Output JSON path (default: overwrites source copydeck)") |
| 69 | + args = parser.parse_args() |
| 70 | + |
| 71 | + for p in [Path(args.csv), Path(args.copydeck)]: |
| 72 | + if not p.exists(): |
| 73 | + print(f"Error: {p} not found", file=sys.stderr) |
| 74 | + sys.exit(1) |
| 75 | + |
| 76 | + import_csv(Path(args.csv), Path(args.copydeck), Path(args.out)) |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + main() |
0 commit comments