File size: 1,222 Bytes
aaef24a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
477e2cb
 
 
aaef24a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
"""Extract lesson metadata and notebook lists into a JSON file."""

import argparse
import json
import re
from pathlib import Path

import frontmatter


NOTEBOOK_PATTERN = re.compile(r"^\d{2}_.*\.py$")


def extract_lessons(root: Path) -> dict:
    lessons = {}
    for readme_file in sorted(root.glob("*/README.md")):
        lesson_dir = readme_file.parent
        post = frontmatter.load(readme_file)
        notebooks = sorted(
            p.name
            for p in lesson_dir.glob("*.py")
            if NOTEBOOK_PATTERN.match(p.name)
        )
        lessons[lesson_dir.name] = {
            **post.metadata,
            "notebooks": notebooks,
        }
    return lessons


def main():
    parser = argparse.ArgumentParser(description="Extract lesson metadata to JSON")
    parser.add_argument("--root", required=True, help="Project root directory")
    parser.add_argument("--data", required=True, help="Output JSON file")
    args = parser.parse_args()

    root = Path(args.root)
    data = Path(args.data)
    data.parent.mkdir(parents=True, exist_ok=True)

    lessons = extract_lessons(root)
    data.write_text(json.dumps(lessons, indent=2))


if __name__ == "__main__":
    main()