#!/usr/bin/env python3 """Write crates/cotton-core/kits/shell/reworded.json from the history. Every database is written with the kit's shell lines as rows, add-only (cotton_tools::presentation::write_lines), so a line reworded in the kit after a database was made keeps its old words there. reworded.json lists, for each line, every wording the kit gave it before and no longer does; the core passes over a row holding one of them (cotton_core::rules::shell_lines::earlier_kit_words). Run after rewording a kit line, from the repository's root: python3 scripts/lines-reworded.py Wordings already in the file are kept, so a line reworded twice keeps both. """ import json import subprocess import sys from collections import defaultdict from pathlib import Path ROOT = Path(sys.argv[1]) if len(sys.argv) > 1 else Path(".") LINES = "crates/cotton-core/kits/shell/lines" OUT = ROOT / "crates/cotton-core/kits/shell/reworded.json" FILE_KEYS = {"format_version", "id", "maps"} ABOUT = ( "Words a kit line once had and has no more, by the line's id (set.line). Every database is written " "with the kit's lines as rows (presentation::write_lines), add-only, so a line reworded in the kit " "after a database was made still holds its old words there; a row that holds words listed here is " "the kit's own earlier wording, not a person's, and the kit's present words are said instead. When a " "line is reworded, its old words are added here (scripts/lines-reworded.py reads them from the history)." ) def git(*args: str) -> str: return subprocess.run(["git", "-C", str(ROOT), *args], capture_output=True, text=True, check=False).stdout def leaves(value, path, out): if isinstance(value, str): out[path] = value elif isinstance(value, dict): for key, inner in value.items(): leaves(inner, f"{path}.{key}", out) def flat(text: str) -> dict: try: file = json.loads(text) except ValueError: return {} out = {} for key, value in file.items(): if key not in FILE_KEYS: leaves(value, key, out) return out def sets_at(commit: str) -> dict: found = {} for name in git("ls-tree", "--name-only", commit, LINES + "/").split(): found[Path(name).stem] = name return found current = {set_id: flat(git("show", f"HEAD:{name}")) for set_id, name in sets_at("HEAD").items()} earlier = defaultdict(set) if OUT.exists(): for line, words in json.loads(OUT.read_text())["lines"].items(): earlier[line].update(words) for commit in git("log", "--format=%H", "--", LINES).split(): for set_id, name in sets_at(commit).items(): if set_id not in current: continue for key, words in flat(git("show", f"{commit}:{name}")).items(): earlier[f"{set_id}.{key}"].add(words) kept = {} for line in sorted(earlier): set_id, key = line.split(".", 1) now = current.get(set_id, {}).get(key) words = sorted(w for w in earlier[line] if now is not None and w != now) if words: kept[line] = words OUT.write_text(json.dumps({"format_version": 1, "about": ABOUT, "lines": kept}, indent=2, ensure_ascii=False) + "\n") print(f"{len(kept)} lines, {sum(map(len, kept.values()))} earlier wordings", file=sys.stderr)