-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_parameters.py
72 lines (56 loc) · 2.72 KB
/
get_parameters.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
import re
def get_parameters(docstring: str) -> list[dict[str]] | None:
"""
Goes through the docstring and looks for parameters annotated by the `@param` tag.
If no tags are found, it returns `None`. Otherwise, it returns a list of dicts. Each dict has a single key
with the name of the parameter, and the key value is the description of what that parameter does.
For example:
- `@param MyParam This is my parameter description.
- Returns: `{"MyParam": "This is my parameter description."}`
"""
parsed = docstring.splitlines()
parameters = []
parameter_name = ""
parameter_description = ""
def add_parameter(name:str, desc: str) -> None:
"""
Adds the parameter and description to the parameters list.
If the parameter was already added, it was overwrites it.
"""
nonlocal parameter_name
nonlocal parameter_description
for param in parameters:
if name in param:
param[name] = parameter_description.strip()
return
parameters.append({name.strip(): desc.strip()})
parameter_name = ""
parameter_description = ""
for line in parsed:
stripped_line = line.strip()
if stripped_line[0:7] == "@param ":
if parameter_name != "":
# Previous parameter complete, about to start a new one
add_parameter(parameter_name, parameter_description)
# We have encountered a new parameter, start recording the info
end_of_param_name = re.search("[A-Za-z0-9] ", stripped_line[8:len(stripped_line)]).end()
parameter_name = stripped_line[7:7 + end_of_param_name]
parameter_description = stripped_line[8 + end_of_param_name:len(stripped_line)]
continue
if parameter_name != "" and stripped_line[0:1] == "@":
# Already started parsing a parameter, but now encountering a new tag
add_parameter(parameter_name, parameter_description)
continue
if parameter_name != "":
# Have found a parameter already, and now its description has spilled on to another line
if stripped_line == "": # Add a paragraph break
parameter_description += "\n"
elif parameter_description[-1:] == "\n": # Do not add an extra space for new paragraphs.
parameter_description += stripped_line
else:
parameter_description += " " + stripped_line
if parameter_name != "": # Final catch for parameters not added yet
add_parameter(parameter_name, parameter_description)
if len(parameters) > 0:
return parameters
return None