|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Sync myst.yml TOC with notebooks on disk. |
| 4 | +Finds lec/lecNN/, lab/labNN/, hw/hwNN/, project/projectN.ipynb, and sandbox/sandbox.ipynb, |
| 5 | +builds the table of contents in order, and updates myst.yml. |
| 6 | +Run from repository root. Exits 0 if no change, 2 on error. |
| 7 | +""" |
| 8 | +import re |
| 9 | +import sys |
| 10 | +from pathlib import Path |
| 11 | + |
| 12 | +try: |
| 13 | + import yaml |
| 14 | +except ImportError: |
| 15 | + print("PyYAML required: pip install pyyaml", file=sys.stderr) |
| 16 | + sys.exit(2) |
| 17 | + |
| 18 | +REPO_ROOT = Path(__file__).resolve().parent.parent |
| 19 | +MYST_PATH = REPO_ROOT / "myst.yml" |
| 20 | + |
| 21 | +EXIT_SUCCESS = 0 |
| 22 | +EXIT_ERROR = 2 |
| 23 | + |
| 24 | + |
| 25 | +def find_numbered_notebooks(parent_dir: str, prefix: str, title_fmt: str) -> list[dict]: |
| 26 | + """Find all prefixNN/prefixNN.ipynb under parent_dir, sorted by NN.""" |
| 27 | + base = REPO_ROOT / parent_dir |
| 28 | + if not base.is_dir(): |
| 29 | + return [] |
| 30 | + pattern = re.compile(rf"^{prefix}(\d+)$") |
| 31 | + entries = [] |
| 32 | + for path in sorted(base.iterdir()): |
| 33 | + if not path.is_dir(): |
| 34 | + continue |
| 35 | + m = pattern.match(path.name) |
| 36 | + if not m: |
| 37 | + continue |
| 38 | + num = m.group(1) |
| 39 | + nb = path / f"{path.name}.ipynb" |
| 40 | + if nb.is_file(): |
| 41 | + rel = str(nb.relative_to(REPO_ROOT)) |
| 42 | + title = title_fmt.format(num=int(num), znum=num.zfill(2)) |
| 43 | + entries.append({"title": title, "file": rel}) |
| 44 | + # Sort by number |
| 45 | + entries.sort(key=lambda e: int(re.search(r"\d+", e["file"]).group())) |
| 46 | + return entries |
| 47 | + |
| 48 | + |
| 49 | +def find_flat_numbered_notebooks(parent_dir: str, file_prefix: str, title_fmt: str) -> list[dict]: |
| 50 | + """Find all parent_dir/file_prefixN.ipynb (flat), sorted by N.""" |
| 51 | + base = REPO_ROOT / parent_dir |
| 52 | + if not base.is_dir(): |
| 53 | + return [] |
| 54 | + pattern = re.compile(rf"^{re.escape(file_prefix)}(\d+)\.ipynb$") |
| 55 | + entries = [] |
| 56 | + for path in base.iterdir(): |
| 57 | + if not path.is_file() or path.suffix != ".ipynb": |
| 58 | + continue |
| 59 | + m = pattern.match(path.name) |
| 60 | + if not m: |
| 61 | + continue |
| 62 | + num = m.group(1) |
| 63 | + rel = str(path.relative_to(REPO_ROOT)) |
| 64 | + title = title_fmt.format(num=int(num), znum=num.zfill(2)) |
| 65 | + entries.append({"title": title, "file": rel}) |
| 66 | + entries.sort(key=lambda e: int(re.search(r"\d+", e["file"]).group())) |
| 67 | + return entries |
| 68 | + |
| 69 | + |
| 70 | +def main() -> int: |
| 71 | + if not MYST_PATH.is_file(): |
| 72 | + print(f"Error: {MYST_PATH} not found", file=sys.stderr) |
| 73 | + return EXIT_ERROR |
| 74 | + |
| 75 | + toc = [] |
| 76 | + |
| 77 | + # Intro (no title, just file) |
| 78 | + intro = REPO_ROOT / "intro.md" |
| 79 | + if intro.exists(): |
| 80 | + toc.append({"file": "intro.md"}) |
| 81 | + |
| 82 | + # Lectures |
| 83 | + lectures = find_numbered_notebooks("lec", "lec", "Lecture {znum}") |
| 84 | + if lectures: |
| 85 | + toc.append({"title": "Lectures", "children": lectures}) |
| 86 | + |
| 87 | + # Labs |
| 88 | + labs = find_numbered_notebooks("lab", "lab", "Lab {znum}") |
| 89 | + if labs: |
| 90 | + toc.append({"title": "Labs", "children": labs}) |
| 91 | + |
| 92 | + # Homework |
| 93 | + homework = find_numbered_notebooks("hw", "hw", "Homework {znum}") |
| 94 | + if homework: |
| 95 | + toc.append({"title": "Homework", "children": homework}) |
| 96 | + |
| 97 | + # Projects (flat: project/project1.ipynb, project/project2.ipynb, ...) |
| 98 | + projects = find_flat_numbered_notebooks("project", "project", "Project {znum}") |
| 99 | + if projects: |
| 100 | + toc.append({"title": "Projects", "children": projects}) |
| 101 | + |
| 102 | + # Sandbox |
| 103 | + sandbox_nb = REPO_ROOT / "sandbox" / "sandbox.ipynb" |
| 104 | + if sandbox_nb.exists(): |
| 105 | + toc.append({ |
| 106 | + "title": "Sandbox", |
| 107 | + "children": [{"title": "Sandbox", "file": "sandbox/sandbox.ipynb"}], |
| 108 | + }) |
| 109 | + |
| 110 | + try: |
| 111 | + with open(MYST_PATH, "r") as f: |
| 112 | + data = yaml.safe_load(f) |
| 113 | + except yaml.YAMLError as e: |
| 114 | + print(f"Error: invalid YAML in {MYST_PATH}: {e}", file=sys.stderr) |
| 115 | + return EXIT_ERROR |
| 116 | + |
| 117 | + if data is None: |
| 118 | + data = {} |
| 119 | + if "project" not in data: |
| 120 | + data["project"] = {} |
| 121 | + |
| 122 | + old_toc = data["project"].get("toc", []) |
| 123 | + if old_toc == toc: |
| 124 | + return EXIT_SUCCESS |
| 125 | + |
| 126 | + data["project"]["toc"] = toc |
| 127 | + with open(MYST_PATH, "w") as f: |
| 128 | + yaml.dump(data, f, default_flow_style=False, allow_unicode=True, sort_keys=False) |
| 129 | + |
| 130 | + return EXIT_SUCCESS |
| 131 | + |
| 132 | + |
| 133 | +if __name__ == "__main__": |
| 134 | + try: |
| 135 | + sys.exit(main()) |
| 136 | + except Exception as e: |
| 137 | + print(f"Error: {e}", file=sys.stderr) |
| 138 | + sys.exit(EXIT_ERROR) |
0 commit comments