#!/usr/bin/env python3 """Extract a ranked file-extension census for the committed ledger directory. Source of truth: the committed inventory file `wizard-work/ledger/repo_files.tsv`, a (rank, path) inventory of the troupe worktree. Only rows whose path lives under `wizard-work/ledger/` are counted. Normalization: take the basename, lower-case the suffix, leading dot on the suffix unless there is none, which is emitted as the literal "(none)". Output (ledger-dir-census.csv): rank,extension,file_count 1,.py,217 ... and ledger-dir-census.md (ranked table + totals). Run: python3 extract_ledger_dir_census.py """ import csv import os import sys from collections import Counter REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) LEDGER_DIR = os.path.join(REPO, "wizard-work", "ledger") SRC = os.path.join(LEDGER_DIR, "repo_files.tsv") OUT_CSV = os.path.join(LEDGER_DIR, "ledger-dir-census.csv") OUT_MD = os.path.join(LEDGER_DIR, "ledger-dir-census.md") def clean_path(raw): s = raw.strip() if len(s) >= 2 and s[0] == '"' and s[-1] == '"': s = s[1:-1] s = s.encode("latin-1").decode("unicode_escape", "replace") return s def ext_of(path): base = os.path.basename(path.rstrip("/")) if not base or base in (".", ".."): return "(none)" if "." in base: return "." + base.rsplit(".", 1)[1].lower() return "(none)" def main(): counts = Counter() total = 0 with open(SRC, "r", encoding="utf-8", errors="replace") as f: for line in f: line = line.rstrip("\n").rstrip("\r") if "\t" not in line: continue rank, path = line.split("\t", 1) path = clean_path(path) if not path.startswith("wizard-work/ledger/"): continue total += 1 counts[ext_of(path)] += 1 ordered = sorted(counts.items(), key=lambda kv: (-kv[1], kv[0])) with open(OUT_CSV, "w", newline="", encoding="utf-8") as f: w = csv.writer(f) w.writerow(["rank", "extension", "file_count"]) for i, (ext, n) in enumerate(ordered, 1): w.writerow([i, ext, n]) md = [ "# Ledger directory census", "", "Ranked census of file extensions inside `wizard-work/ledger/`,", "re-derived from the committed inventory `repo_files.tsv` by", "`extract_ledger_dir_census.py`.", "", "| rank | extension | file_count |", "|---|---|---|", ] for i, (ext, n) in enumerate(ordered, 1): md.append("| %d | %s | %d |" % (i, ext, n)) md += [ "", "**Total files: %d**" % total, "**Unique extensions: %d**" % len(ordered), "", ] with open(OUT_MD, "w", encoding="utf-8") as f: f.write("\n".join(md)) print("rows=%d total_files=%d extensions=%d" % (total, total, len(ordered))) print("top10=%s" % ordered[:10]) return 0 if __name__ == "__main__": sys.exit(main())