-
Notifications
You must be signed in to change notification settings - Fork 550
/
generator.py
164 lines (142 loc) · 5.86 KB
/
generator.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import sys
import inspect
from .template import (
header_template,
regex_pattern_body_template,
regex_pattern_line_template,
)
from .template import variable_body_template, variable_line_template
from nba_api.stats.library import parameters
from tools.library.file_handler import save_file, get_file_path
from tools.stats.library.mapping import parameter_map, parameter_variations
def _get_class_information(cls, variable_names_to_exclude=None):
if not variable_names_to_exclude:
variable_names_to_exclude = []
variables = []
default = getattr(cls, "default")
for var in dir(cls):
variable = {}
if "__" in var or var == "default" or var in variable_names_to_exclude:
continue
var_name = var
is_function = False
if inspect.isfunction(var):
is_function = True
var_name = "{var_name}()".format(var_name=var_name)
default_value = getattr(cls, var)
variable["name"] = var_name
variable["default_value"] = default_value
variable["default"] = default_value == default
variable["function"] = is_function
variable_names_to_exclude.append(var)
variables.append(variable)
return variables, variable_names_to_exclude
def get_library_classes(module=parameters):
library_classes = {}
classes = inspect.getmembers(sys.modules[module.__name__], inspect.isclass)
for name, cls in classes:
if name == cls.__module__: # Skip imports
continue
variables, variable_names = _get_class_information(cls=cls)
for c in cls.mro(): # Check parent classes as well.
if name == c.__module__: # Skip imports
continue
additional_variables, variable_names = _get_class_information(
cls=cls, variable_names_to_exclude=variable_names
)
variables += additional_variables
library_classes[name] = variables
return library_classes
def get_parameter_map_parameters():
parameters = list(parameter_map.keys())
parameters.sort()
return parameters
def get_parameter_map_patterns():
parameter_patterns = {}
for parameter in get_parameter_map_parameters():
pattern_info = {}
nullable_dict = parameter_map[parameter]["nullable"]
non_nullable_dict = parameter_map[parameter]["non-nullable"]
for pattern, pattern_key in nullable_dict.items():
if pattern_key not in pattern_info:
pattern_info[pattern_key] = [pattern]
else:
pattern_info[pattern_key].append(pattern)
for pattern, pattern_key in non_nullable_dict.items():
if pattern_key not in pattern_info:
pattern_info[pattern_key] = [pattern]
else:
pattern_info[pattern_key].append(pattern)
parameter_patterns[parameter] = pattern_info
return parameter_patterns
def _get_variable_table_from_library_class(library_class):
variable_lines = []
for variable in library_class:
name = variable["name"]
value = variable["default_value"]
is_default = variable["default"]
is_function = variable["function"]
additional_tags = []
if is_default:
additional_tags.append("`default` ")
if is_function:
additional_tags.append("`function` ")
if additional_tags:
additional_tags = " ".join(additional_tags)
else:
additional_tags = ""
if callable(value):
value = "{}()".format(name)
variable_line = variable_line_template.format(
name=name, value=value, additional_tags=additional_tags
).replace("``", "")
if is_default:
variable_lines.insert(0, variable_line)
else:
variable_lines.append(variable_line)
variable_body = variable_body_template.format(variables="\n".join(variable_lines))
return variable_body
def _get_class_documentation_text(parameter, pattern_info, library_classes):
file_contents = header_template.format(parameter_name=parameter)
empty_contents = False
for pattern_key in sorted(pattern_info.keys()):
patterns = pattern_info[pattern_key]
regex_patterns = [
regex_pattern_line_template.format(pattern=pattern)
for pattern in patterns
if pattern
]
default_py_value = parameter_variations[pattern_key]["default_py_value"]
if default_py_value and ".default" in default_py_value:
default_py_value = default_py_value.replace(".default", "")
class_name = default_py_value
if regex_patterns:
file_contents += regex_pattern_body_template.format(
regex_patterns="\n".join(regex_patterns), python_class=class_name
)
variable_body = _get_variable_table_from_library_class(
library_class=library_classes[class_name]
)
file_contents += variable_body
else:
empty_contents = True
if empty_contents:
file_contents += "No available information.\n\n"
return file_contents
def get_parameter_documentation_text():
library_classes = get_library_classes()
parameter_patterns = get_parameter_map_patterns()
file_contents = "# Endpoint Parameters\n\n"
for parameter, pattern_info in parameter_patterns.items():
file_contents += _get_class_documentation_text(
parameter=parameter,
pattern_info=pattern_info,
library_classes=library_classes,
)
return file_contents
def generate_parameter_documentation_file(
directory="parameter_documentation", file_name="parameters.md"
):
contents = get_parameter_documentation_text()
file_path = get_file_path(directory)
save_file(file_path=file_path, file_name=file_name, contents=contents)