-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtree_from_graph.py
More file actions
61 lines (47 loc) · 2.21 KB
/
Copy pathtree_from_graph.py
File metadata and controls
61 lines (47 loc) · 2.21 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
"""Print sessions as a tree from `site/graph.jsonld`.
Demonstrates programmatic access to the AI-consumable JSON-LD graph
that `llmwiki build` emits. Stdlib-only, runs anywhere.
Usage:
python3 examples/scripts/tree_from_graph.py [path/to/graph.jsonld]
Default path: site/graph.jsonld (relative to cwd).
"""
from __future__ import annotations
import json
import sys
from collections import defaultdict
from pathlib import Path
def main(graph_path: str = "site/graph.jsonld") -> int:
path = Path(graph_path)
if not path.is_file():
print(f"error: {path} not found — run `llmwiki build` first", file=sys.stderr)
return 1
nodes = json.loads(path.read_text(encoding="utf-8"))["@graph"]
root = next((n for n in nodes if n["@id"] == "llmwiki"), {})
projects = {n["@id"]: n for n in nodes if n["@id"].startswith("project/")}
sessions = [n for n in nodes if n["@id"].startswith("session/")]
by_proj: dict[str, list[dict]] = defaultdict(list)
for s in sessions:
by_proj[s["isPartOf"]["@id"]].append(s)
total = sum(p.get("numberOfItems", 0) for p in projects.values())
print()
print(f"📚 {total} sessions across {len(projects)} projects")
print(f" ({path} v{root.get('version', '?')})")
print()
print(f"{root.get('name', 'llmwiki')}/")
proj_items = sorted(projects.items(), key=lambda kv: -kv[1].get("numberOfItems", 0))
for pi, (pid, p) in enumerate(proj_items):
last_proj = pi == len(proj_items) - 1
proj_prefix = "└──" if last_proj else "├──"
proj_sessions = sorted(by_proj[pid], key=lambda s: s.get("dateCreated", ""))
n = len(proj_sessions)
print(f"{proj_prefix} {p['name']}/ ({n} session{'s' if n != 1 else ''})")
leg = " " if last_proj else "│ "
for i, s in enumerate(proj_sessions):
child_prefix = "└──" if i == n - 1 else "├──"
date = s.get("dateCreated", "")[:10]
title = s.get("name", "").replace("Session: ", "").split(" — ")[0]
print(f"{leg}{child_prefix} {date} {title}")
print()
return 0
if __name__ == "__main__":
raise SystemExit(main(sys.argv[1] if len(sys.argv) > 1 else "site/graph.jsonld"))