forked from networkx/nx-parallel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_get_info.py
More file actions
149 lines (127 loc) · 4.76 KB
/
update_get_info.py
File metadata and controls
149 lines (127 loc) · 4.76 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import ast
import os
__all__ = [
"get_funcs_info",
"extract_docstrings_from_file",
"extract_add_docs",
"extract_add_params",
"get_url",
]
# Helper functions for get_info
def get_funcs_info():
"""Return a dictionary with information about all the functions."""
funcs = {}
nx_parallel_dir = os.path.join(os.getcwd(), "nx_parallel")
for root, dirs, files in os.walk(nx_parallel_dir):
for file in files:
if (
file.endswith(".py")
and file != "__init__.py"
and not file.startswith("test_")
):
path = os.path.join(root, file)
d = extract_docstrings_from_file(path)
for func in d:
funcs[func] = {
"url": get_url(path, func),
"additional_docs": extract_add_docs(d[func]),
"additional_parameters": extract_add_params(d[func]),
}
return funcs
def extract_docstrings_from_file(file_path):
"""
Extract docstrings from functions listed in the __all__ list of a Python file.
Args:
- file_path: The path to the Python file.
Returns:
- A dictionary mapping function names to their docstrings.
"""
docstrings = {}
with open(file_path, "r") as f:
tree = ast.parse(f.read(), filename=file_path)
all_list = None
for node in tree.body:
if isinstance(node, ast.Assign):
if (
isinstance(node.targets[0], ast.Name)
and node.targets[0].id == "__all__"
):
all_list = [
expr.s for expr in node.value.elts if isinstance(expr, ast.Str)
]
elif isinstance(node, ast.FunctionDef):
if all_list and node.name in all_list:
docstring = ast.get_docstring(node) or "No docstring found."
docstrings[node.name] = docstring
return docstrings
def extract_add_docs(docstring):
"""Extract the parallel documentation description from the given doctring."""
try:
# Extracting Parallel Computation description
# Assuming that the first para in docstring is the function's PC desc
# "par" is short for "parallel"
par_docs_ = docstring.split("\n\n")[0]
par_docs_ = par_docs_.split("\n")
par_docs_ = [line.strip() for line in par_docs_ if line.strip()]
par_docs = " ".join(par_docs_)
par_docs = par_docs.replace("\n", " ")
except IndexError:
par_docs = None
except Exception as e:
print(e)
par_docs = None
return par_docs
def extract_add_params(docstring):
"""Extract the parallel parameter description from the given docstring."""
try:
# Extracting extra parameters
# Assuming that the last para in docstring is the function's extra params
par_params = {}
par_params_ = docstring.split("----------\n")[1]
par_params_ = par_params_.split("\n")
i = 0
while i < len(par_params_):
line = par_params_[i]
if " : " in line:
key = line.strip()
n = par_params_.index(key) + 1
par_desc = ""
while n < len(par_params_) and par_params_[n] != "":
par_desc += par_params_[n].strip() + " "
n += 1
par_params[key] = par_desc.strip()
i = n + 1
else:
i += 1
except IndexError:
par_params = None
except Exception as e:
print(e)
par_params = None
return par_params
def get_url(file_path, function_name):
"""Return the URL to the given function in the given file."""
file_url = (
"https://github.com/networkx/nx-parallel/blob/main/nx_parallel"
+ file_path.split("nx_parallel")[-1]
+ "#L"
)
with open(file_path, "r") as f:
tree = ast.parse(f.read(), filename=file_path)
for node in ast.walk(tree):
if isinstance(node, ast.FunctionDef) and node.name == function_name:
return file_url + str(node.lineno)
return file_url
# Creating a temp__init__.py file
string = '''# This file was automatically generated by update_get_info.py
def get_info():
"""Return a dictionary with information about the package."""
return {
"backend_name": "parallel",
"project": "nx-parallel",
"package": "nx_parallel",
"url": "https://github.com/networkx/nx-parallel",
"short_summary": "Parallel backend for NetworkX algorithms",
"functions": '''
with open("_nx_parallel/temp__init__.py", "w") as f:
f.write(string + str(get_funcs_info()) + "}\n")