-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_toc.py
50 lines (43 loc) · 1.33 KB
/
update_toc.py
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
from itertools import chain, groupby, starmap, takewhile
from pathlib import Path
def main():
def toc_level(contents, level=0):
def get_key(init):
return Path(init).parents[1]
def inner(key, group):
return group
toc_level()
# f'{ count }. { title_name }'
# f'''{
# " " * level * 4
# }- [{
# kabob_case
# }]({
# relative_folder
# }) {
# description
# }'''
return chain.from_iterable(starmap(inner, groupby(contents, get_key)))
this_file = Path(__file__)
readme = this_file.resolve().with_name("README.md")
toc_line = "# Table of Contents\n"
with readme.open() as istream:
lines = tuple(takewhile(lambda line: line != toc_line, istream))
contents = sorted(
map(
lambda init: f"./{ init.resolve().relative_to(readme.parent) }",
filter(
lambda init: "site-packages" not in str(init),
this_file.parent.rglob("**/__init__.py"),
),
)
)
with readme.open("w") as ostream:
ostream.writelines(lines)
ostream.write(toc_line)
ostream.writelines(toc_level(contents))
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print()