-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_api_docs.py
359 lines (318 loc) · 10.8 KB
/
generate_api_docs.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
import requests
import json
import os
try:
from urllib import parse as urlparse
except ImportError:
import urlparse
from settings import TS_URL, TS_USERNAME, TS_PASSWORD
# modified from http://stackoverflow.com/a/12539081/56069
def make_table(grid):
max_cols = [
max(out)
for out in map(list, zip(*[[len(item) for item in row] for row in grid]))
]
rst = table_div(max_cols, 1)
for i, row in enumerate(grid):
header_flag = False
if i == 0 or i == len(grid) - 1:
header_flag = True
rst += normalize_row(row, max_cols)
rst += table_div(max_cols, header_flag)
return rst
def table_div(max_cols, header_flag=1):
out = ""
if header_flag == 1:
style = "="
else:
style = "-"
for max_col in max_cols:
out += max_col * style + " "
out += "\n"
return out
def normalize_row(row, max_cols):
r = ""
for i, max_col in enumerate(max_cols):
r += row[i] + (max_col - len(row[i]) + 1) * " "
return r + "\n"
def lower_case_to_title_case(string):
"""
This function takes text like 'bigredtruck' and turns it into 'big red truck' by magic.
"""
string = string.replace("_", "").lower()
dictionary = sorted(
[
"active",
"adapter",
"analysis",
"analysis",
"annotation",
"appl",
"application",
"args",
"attribute",
"available",
"basic",
"chef",
"cluster",
"common",
"composite",
"content",
"cv",
"data",
"db",
"dna",
"email",
"event",
"experiment",
"file",
"flow",
"genome",
"global",
"group",
"history",
"info",
"ion",
"item",
"job",
"kit",
"lib",
"library",
"location",
"log",
"management",
"metrics",
"mesh",
"monitor",
"node",
"onetouch",
"pgm",
"plan",
"planned",
"plugin",
"prep",
"prime",
"product",
"project",
"proton",
"qc",
"quality",
"reference",
"result",
"results",
"run",
"sample",
"sequencing",
"server",
"set",
"settings",
"suite",
"summary",
"support",
"support",
"template",
"tf",
"three",
"torrent",
"type",
"upload",
"get",
"script",
"info",
],
key=len,
reverse=True,
)
found_words = []
for _ in range(0, 10):
for word in dictionary:
if string.startswith(word):
found_words.append(word)
string = string[len(word) :]
break
found_words.append(string)
return " ".join(found_words)
# Auth
auth = (TS_USERNAME, TS_PASSWORD)
# API Docs Settings
API_BASE_URL = urlparse.urljoin(TS_URL, "/rundb/api/v1/")
API_DOCS_OUTPUT_PATH = os.path.join("source", "api", "references_auto_generated")
MANUAL_API_DOCS_OUTPUT_PATH = os.path.join("source", "api", "references_manual_extras")
EXCLUDE_RESOURCES = [
"ionreporter", # 500
"account", # 401
"prepopulatedplanningsession", # 500
"obsoletereferencegenome", # Obsolete
"getsoftwareinfo",
]
API_VERSION = "v1"
DEMO_API_BASE_URL = "http://mytorrentserver/rundb/api/v1/"
DEMO_URL_ARGS = "?format=json&limit=1"
# Functions
# API Auto-gen Documentation
# Get API schema for all objects
main_schema_request = requests.get(API_BASE_URL, params={"format": "json"}, auth=auth)
main_schema_request.raise_for_status()
# Track locations of API docs for listing page
resource_doc_paths = []
# Write out docs pages for each API resource
for resource_name, resource_values in list(main_schema_request.json().items()):
if resource_name not in EXCLUDE_RESOURCES:
# Get schema for the current resource
print("Using TS API to fetch %s schema..." % resource_name)
single_resource_schema_request = requests.get(
urlparse.urljoin(API_BASE_URL, resource_values["schema"]),
params={"format": "json"},
auth=auth,
)
print(single_resource_schema_request.url)
single_resource_schema_request.raise_for_status()
# Make a request to get an example response
single_resource_demo_request = requests.get(
urlparse.urljoin(API_BASE_URL, resource_values["list_endpoint"])
+ DEMO_URL_ARGS,
auth=auth,
)
single_resource_demo_request.raise_for_status()
# We get warnings if the manual files do not exist. Try to create them now if they do not exist
manual_resource_doc_path = os.path.join(
MANUAL_API_DOCS_OUTPUT_PATH, resource_name.lower() + ".rst"
)
if not os.path.exists(manual_resource_doc_path):
print(
"Missing manual rst file for %s resource. Creating one now."
% resource_name
)
with open(manual_resource_doc_path, "w+") as output_file:
output_file.write("")
# Generate an rst for the resource
resource_doc_path = os.path.join(
API_DOCS_OUTPUT_PATH, resource_name.lower() + ".rst"
)
with open(resource_doc_path, "w+") as output_file:
json_formatted_demo = json.dumps(
single_resource_demo_request.json(), indent=4
)
json_formatted_demo = "\n".join(
["\t" + json_line for json_line in json_formatted_demo.split("\n")]
)
json_formatted_schema = json.dumps(
single_resource_schema_request.json()["fields"], indent=4
)
json_formatted_schema = "\n".join(
["\t" + json_line for json_line in json_formatted_schema.split("\n")]
)
allowed_detail_http_methods = single_resource_schema_request.json()[
"allowed_detail_http_methods"
]
allowed_list_http_methods = single_resource_schema_request.json()[
"allowed_list_http_methods"
]
title = lower_case_to_title_case(resource_name).title() + " Resource\n"
output_file.write(".. _api_reference_%s:\n\n" % resource_name.lower())
output_file.write(title)
output_file.write("=" * (len(title) - 1) + "\n\n")
output_file.write(
"| Resource URL ``%s``\n"
% urlparse.urljoin(DEMO_API_BASE_URL, resource_values["list_endpoint"])
)
output_file.write(
"| Schema URL ``%s``\n"
% urlparse.urljoin(DEMO_API_BASE_URL, resource_values["schema"])
)
output_file.write("| ")
# Write out manual docs portion
output_file.write(
"\n\n"
+ ".. include:: ../references_manual_extras/"
+ resource_name.lower()
+ ".rst"
+ "\n\n"
)
# Write out resource schema
cols = [
"help_text",
"default",
"nullable",
"readonly",
"blank",
"unique",
"type",
]
header_cols = [
"help text",
"default ",
"nullable",
"readonly",
"blank",
"unique",
"type",
]
header_cols = [
"help text",
"default",
"nullable",
"readonly",
"blank",
"unique",
"type",
]
output_file.write("Resource Fields\n")
output_file.write("---------------" + "\n\n")
toprow = ["field"]
toprow.extend(header_cols)
table = [toprow]
for field, values in single_resource_schema_request.json()[
"fields"
].items():
row = ["**" + field + "**"]
for col in cols:
if col == "default" and values[col] == "No default provided.":
values[col] = "n/a"
if col == "default" and values[col] == "No default provided.":
values[col] = "n/a"
if str(values[col]) == "False":
values[col] = "false"
if str(values[col]) == "True":
values[col] = "true"
row.append(str(values[col]))
table.append(row)
output_file.write(make_table(table))
output_file.write("\n")
output_file.write("Example Response\n")
output_file.write("^^^^^^^^^^^^^^^^" + "\n\n")
output_file.write(".. code-block:: javascript\n\n")
output_file.write(json_formatted_demo + "\n")
# Write out http methods
output_file.write("\nAllowed list HTTP methods\n")
output_file.write("-------------------------" + "\n\n")
if not allowed_list_http_methods:
output_file.write("None\n")
else:
for allowed_http_method in allowed_list_http_methods:
output_file.write("- " + allowed_http_method.upper() + "\n")
output_file.write("\n")
output_file.write("\nAllowed detail HTTP methods\n")
output_file.write("---------------------------" + "\n\n")
if not allowed_detail_http_methods:
output_file.write("None\n")
else:
for allowed_http_method in allowed_detail_http_methods:
output_file.write("- " + allowed_http_method.upper() + "\n")
output_file.write("\n")
"""
Filtering
--------
Enable all basic ORM filters but do not allow filtering across relationships.
ALL = 1
# Enable all ORM filters, including across relationships
ALL_WITH_RELATIONS = 2
"default_format": "application/json",
"default_limit": 20,
"filtering": {},
"ordering": []
"""
resource_doc_paths.append(
os.path.relpath(resource_doc_path, "source").replace("\\", "/")
)