This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerate_nodesidebar.py
78 lines (60 loc) · 2.33 KB
/
generate_nodesidebar.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
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
from os import path, walk
import json
from collections import defaultdict
NODES_DIR = path.join("docs", "nodes")
def write_file(file_path: str, content: str):
# Write the file
with open(file_path, "w") as file:
file.write(content)
def load_file(file_path: str):
with open(file_path, "r") as f:
content = f.read()
return json.loads(content)
def update_map(
map_file: dict[str, list[str]],
nodes_map: dict[str, str | list[str]],
file_path: str,
):
for key, item in nodes_map.items():
if item == "":
continue
formatted_path = file_path.replace("\\", "/").replace(".md", "")
if isinstance(item, list):
for i in item:
if f"/{i}/" in formatted_path.upper():
map_file[key].append(formatted_path)
else:
if f"/{item}/" in formatted_path.upper():
map_file[key].append(formatted_path)
return map_file
__ignore_dirs = ["appendix", "examples", "a1-[autogen]"]
def write_nodesidebar():
nodes_map: dict[str, str | list[str]] = load_file("nodes_sidebar_map.json")
new_map: dict[str, list[str]] = defaultdict(list)
for root, dirnames, files in walk(NODES_DIR):
# only care about directories for actual nodes
if any((d not in __ignore_dirs) for d in dirnames):
continue
if any((dir in root) for dir in __ignore_dirs):
continue
file_proccessed = []
for file in files:
if file.endswith(".md"):
if file in file_proccessed:
raise FileExistsError(
f"Error: multiple md file found in {root} for {file}, there should only be one!"
)
file_proccessed.append(file)
path_index = root.index("nodes")
path_from_second_dir = root[path_index:]
file_path = path.join(path_from_second_dir, file)
new_map = update_map(new_map, nodes_map, file_path)
print(path_from_second_dir)
sorted_map = dict(sorted(new_map.items()))
for key, items in sorted_map.items():
sorted_map[key] = sorted(items)
write_file(
path.join(path.curdir, "nodeSidebar.json"), json.dumps(sorted_map, indent=4)
)
if __name__ == "__main__":
write_nodesidebar()