#!/usr/bin/env python3 """Count, per layout, what moves between two index versions (served vs a revision, or two revisions). The index prints every question twice: in the "By TODO item" section the row's first cell is the TODO-item letter and the second names the question; in the "By id" section the row starts with the question. Both are table lines; every table line carries the question id. This counts changed lines and the ids behind them, per layout. python count-moved.py --a served.md --b revision.md [--labels served,revision] """ import argparse import io import sys def load(path): texts = {"by_id": {}, "by_todo": {}, "rows": 0} for line in open(path, encoding="utf-8").read().splitlines(): if not line.startswith("| ") or "`Q-" not in line: continue cells = [c.strip() for c in line.split(" | ")] texts["rows"] += 1 if line.startswith("| `Q-"): texts["by_id"][line.split(" | ")[0].strip("| ").strip("`")] = line else: texts["by_todo"][cells[1].strip("`")] = line return texts def main(): ap = argparse.ArgumentParser() ap.add_argument("--a", required=True) ap.add_argument("--b", required=True) ap.add_argument("--labels", default="a,b") a = ap.parse_args() sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace") la, lb = a.labels.split(",") A, B = load(a.a), load(a.b) print("%s: %d table rows, %d lines total, %d bytes" % (la, A["rows"], len(open(a.a, encoding="utf-8").read().splitlines()), len(open(a.a, "rb").read()))) print("%s: %d table rows, %d lines total, %d bytes" % (lb, B["rows"], len(open(a.b, encoding="utf-8").read().splitlines()), len(open(a.b, "rb").read()))) for key, name in (("by_id", "by id"), ("by_todo", "by todo")): ids = set(A[key]) | set(B[key]) moved = [r for r in sorted(ids) if A[key].get(r) != B[key].get(r)] only_a = [r for r in sorted(ids) if r in A[key] and r not in B[key]] only_b = [r for r in sorted(ids) if r in B[key] and r not in A[key]] print("%-8s rows: %s %d, %s %d | moved %d | only-in-%s %d | only-in-%s %d" % (name, la, len(A[key]), lb, len(B[key]), len(moved), la, len(only_a), lb, len(only_b))) if moved: print(" moved: %s" % ", ".join(moved)) return 0 if __name__ == "__main__": raise SystemExit(main())