Repository navigation
Expand file tree
/
Copy pathgenerate_ref.py
More file actions
127 lines (93 loc) · 3.4 KB
/
Copy pathgenerate_ref.py
File metadata and controls
127 lines (93 loc) · 3.4 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from pathlib import Path
import importlib.util
import os
import re
import sys
from urllib.parse import quote
ROOT = Path(__file__).resolve().parent
EXAMPLES = ROOT / "examples"
README = ROOT / "README.md"
BEGIN = "<!-- BEGIN GENERATED REFERENCES -->"
END = "<!-- END GENERATED REFERENCES -->"
def collect_references():
references = []
index = 0
for folder, dirs, files in os.walk(EXAMPLES, followlinks=False):
# Avoid recursive links and Python cache directories.
dirs[:] = sorted(
d for d in dirs
if d != "__pycache__"
and not (Path(folder) / d).is_symlink()
)
for filename in sorted(files):
file = Path(folder) / filename
if file.suffix != ".py" or file.name == "__init__.py":
continue
index += 1
module_name = f"reference_meta_{index}"
try:
spec = importlib.util.spec_from_file_location(module_name, file)
if spec is None or spec.loader is None:
continue
module = importlib.util.module_from_spec(spec)
sys.modules[module_name] = module
spec.loader.exec_module(module)
meta = getattr(module, "meta", None)
if callable(meta):
citation = str(meta()).strip()
relative_path = file.relative_to(ROOT).as_posix()
references.append((relative_path, citation))
except Exception as exc:
print(f"Skipping {file}: {exc}")
return references
def generate_markdown(references):
lines = [BEGIN]
if not references:
lines.append("No references found.")
else:
for i, (filepath, citation) in enumerate(references, 1):
link = quote(filepath, safe="/")
filename = Path(filepath).name
citation = " ".join(citation.split())
lines.append(f"{i}. [{filename}]({link}) — {citation}")
lines.append(END)
return "\n".join(lines)
def update_readme(markdown):
content = README.read_text(encoding="utf-8")
# Replace an existing References section, if present.
heading = re.search(
r"(?m)^(#{1,6})[ \t]+References[ \t]*$",
content,
re.IGNORECASE
)
if heading:
level = len(heading.group(1))
remainder = content[heading.end():]
# Find the next heading at the same or a higher level.
next_heading = None
for match in re.finditer(r"(?m)^(#{1,6})[ \t]+.*$", remainder):
if len(match.group(1)) <= level:
next_heading = match
break
end = (
heading.end() + next_heading.start()
if next_heading
else len(content)
)
replacement = (
heading.group(0)
+ "\n\n"
+ markdown
+ "\n\n"
)
content = content[:heading.start()] + replacement + content[end:].lstrip("\n")
else:
# Add a References section if one does not exist.
content = content.rstrip() + "\n\n## References\n\n" + markdown + "\n"
README.write_text(content, encoding="utf-8")
if __name__ == "__main__":
references = collect_references()
markdown = generate_markdown(references)
update_readme(markdown)
print(f"Updated {README}")
print(f"Collected {len(references)} references.")