forked from ringcentral/slate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_docs.stoneg.py
415 lines (323 loc) · 13.4 KB
/
print_docs.stoneg.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
from __future__ import unicode_literals
from typing import Text, Optional, Any
import json
import six
from stone.backend import CodeBackend
from stone.ir.api import Api, ApiNamespace, ApiRoute
from stone.ir.data_types import (
DataType,
Nullable,
Void,
Alias,
List as ListDataType,
is_user_defined_type,
is_primitive_type,
is_void_type,
Struct,
Union,
UserDefined,
Example,
)
ENDPOINT_FORMATS = {
"rpc": "RPC",
"download": "Content-download",
"upload": "Content-upload",
}
AUTH_TYPES = {
"noauth": "No Authentication",
"user": "User Authentication",
"team": "Team Authentication",
"app": "App Authentication",
}
class ListRoutesBackend(CodeBackend):
methods = set()
def generate(self, api):
# type: (Api) -> None
with self.output_to_relative_path('index.html.md'):
with open('teamplate.html.md') as f:
self.emit_raw(f.read())
for namespace in api.namespaces.values():
self.generate_namespace(namespace)
with self.output_to_relative_path('_datatypes.md'):
self.emit("# Data Types")
for namespace in api.namespaces.values():
for data_type in namespace.data_types:
if isinstance(data_type, Struct):
self.emit("## {}".format(data_type.name))
self.generate_examples(data_type)
self.generate_struct_type(data_type, namespace)
elif isinstance(data_type, Union):
self.emit("## {}".format(data_type.name))
self.generate_examples(data_type)
self.generate_union_type(data_type, namespace)
def generate_namespace(self, namespace):
# type: (ApiNamespace) -> None
if len(namespace.routes) > 0:
self.emit("# {}".format(namespace.name.capitalize()))
for route in namespace.routes:
self.generate_route(namespace, route)
def generate_route(self, namespace, route):
# type: (ApiNamespace, ApiRoute) -> None
if route.version == 1:
self.emit("## {}".format(route.name))
else:
self.emit("## {}_v{}".format(route.name, route.version))
self.generate_route_curl_example(namespace, route)
self.generate_route_return_example(route.result_data_type)
self.generate_route_doc(namespace, route)
self.generate_route_auth_type(route)
self.emit("### ENDPOINT FORMAT")
self.emit(ENDPOINT_FORMATS[route.attrs.get("style", "rpc")])
# generate scope
if route.attrs.get("scope") is not None:
self.emit("### Required Scope")
self.emit(route.attrs.get("scope"))
self.generate_route_data_types(route, namespace)
def generate_route_curl_example(self, namespace, route):
# type: (ApiNamespace, ApiRoute) -> None
example_str = dropbox_curl_example(namespace, route)
if example_str is not None:
self.emit("```shell")
self.emit_raw(example_str + "\n")
self.emit("```")
self.emit("")
def generate_route_return_example(self, data_type):
# type: (DataType) -> None
if isinstance(data_type, UserDefined):
examples = data_type.get_examples()
if not examples:
return
default_example = examples.get("default") or examples.values()[0]
example_dict = default_example.value
if not example_dict:
# example is just {}
return
self.emit("> The above command returns JSON structured like this:")
self.emit("")
self.emit("```json")
self.emit_raw(json.dumps(example_dict, indent=4) + "\n")
self.emit("```")
self.emit("")
def generate_examples(self, data_type):
# type: (DataType) -> None
if not isinstance(data_type, UserDefined):
return
examples = data_type.get_examples()
if not examples:
return
for tag, example in examples.items():
example_dict = example.value
if not example_dict:
continue
self.emit("")
self.emit("> Example: {}".format(tag))
self.emit("")
self.emit("```json")
self.emit_raw(json.dumps(example_dict, indent=4) + "\n")
self.emit("```")
self.emit("")
def generate_route_auth_type(self, route):
# type: (ApiRoute) -> None
self.emit("### Authentication")
auth_types = route.attrs.get("auth", "user")
auth_type_strs = [AUTH_TYPES[auth] for auth in auth_types.split(", ")]
self.emit(", ".join(auth_type_strs))
def generate_route_doc(self, namespace, route):
# type: (ApiNamespace, ApiRoute) -> None
if route.doc is not None:
self.emit("### Description")
self.emit_raw(self.handle_doc(route.doc, None, namespace))
else:
print("{}{} is missing doc".format(namespace.name, route.name))
def generate_route_data_types(self, route, namespace):
# type: (ApiRoute, ApiNamespace) -> None
self.emit("### Query Parameters")
self.generate_top_level_data_type(route.arg_data_type, namespace)
self.emit("### Return Values")
self.generate_top_level_data_type(route.result_data_type, namespace)
self.emit("### Error Values")
self.generate_top_level_data_type(route.error_data_type, namespace)
def generate_top_level_data_type(self, datatype, namespace):
# type: (DataType, ApiNamespace) -> None
if is_user_defined_type(datatype):
if isinstance(datatype, Struct):
self.generate_struct_type(datatype, namespace)
elif isinstance(datatype, Union):
self.generate_union_type(datatype, namespace)
else:
print("Found new user defined type: {}".format(datatype.name))
self.emit(self.handle_data_type(datatype))
else:
self.emit(self.handle_data_type(datatype))
def generate_struct_type(self, struct, namespace):
# type: (Struct, ApiNamespace) -> None
self.emit(self.get_data_type_link(struct.name))
self.emit("")
if struct.doc is not None:
self.emit_raw(self.handle_doc(struct.doc, struct, namespace))
self.emit("")
self.emit("Field Name | Data Type | Description")
self.emit("--------- | ------- | -----------")
for field in struct.all_fields:
doc_cell = ""
if field.doc is not None:
doc_cell = self.handle_doc(field.doc, struct, namespace).replace("\n", "<br>")
self.emit("{} | {} | {}".format(
field.name,
self.handle_data_type(field.data_type),
doc_cell)
)
def generate_union_type(self, union, namespace):
# type: (Union, ApiNamespace) -> None
self.emit(self.get_data_type_link(union.name))
self.emit("")
if union.doc is not None:
self.emit_raw(self.handle_doc(union.doc, union, namespace))
self.emit("")
self.emit("The value will be one of the following datatypes.")
self.emit("")
self.emit("Tag Name | Data Type | Description")
self.emit("--------- | ------- | -----------")
for field in union.all_fields:
doc_cell = ""
if field.doc is not None:
doc_cell = self.handle_doc(field.doc, union, namespace).replace("\n", "<br>")
self.emit("{} | {} | {}".format(
field.name,
self.handle_data_type(field.data_type),
doc_cell)
)
def handle_data_type(self, datatype):
# type: (DataType) -> Text
if isinstance(datatype, Nullable):
return "Optional[{}]".format(self.handle_data_type(datatype.data_type))
if isinstance(datatype, ListDataType):
return "List[{}]".format(self.handle_data_type(datatype.data_type))
if isinstance(datatype, Alias):
return "{}, alias of {}".format(datatype.name, self.handle_data_type(
datatype.data_type))
if is_user_defined_type(datatype):
return self.get_data_type_link(datatype.name)
return six.text_type(datatype.name)
@staticmethod
def get_data_type_link(datatype_name):
# type: (Text) -> Text
return "[{}](#data-types-{})".format(datatype_name, datatype_name.lower())
@staticmethod
def get_route_link(route_name, namespace_name):
# type: (Text, Text) -> Text
processed_route_name = route_name.replace(":", "_v").replace("/", "-")
return "[{}/{}](#{}-{})".format(
namespace_name,
route_name,
namespace_name.lower(),
processed_route_name.lower(),
)
@staticmethod
def get_field_link(current_data_type, val):
# type: (Optional[DataType], Text) -> Text
if "." not in val:
if current_data_type is not None:
return "[{}](#data-types-{})".format(val, current_data_type.name.lower())
else:
return val
data_type_name, _ = val.split(".")
return "[{}](#data-types-{})".format(val, data_type_name.lower())
@staticmethod
def get_direct_link(val):
elements = val.split(" ")
url = elements[-1]
display_item = " ".join(elements[:-1])
return "[{}]({})".format(display_item, url)
def handle_doc(self, doc, data_type, namespace):
# type: (Text, DataType, ApiNamespace) -> Text
assert doc is not None
def doc_processer(tag, val):
# type: (Text, Text) -> Text
if tag == "type":
return self.get_data_type_link(val)
if tag == "route":
if data_type and namespace:
return self.get_route_link(val, namespace.name)
else:
return val
if tag == "field":
return self.get_field_link(data_type, val)
if tag == "link":
return self.get_direct_link(val)
return val
return self.process_doc(doc, doc_processer) + "\n"
return self.process_doc(doc, doc_processer) + "\n"
## code copied from DBX code base.
def dropbox_curl_example(namespace, route):
# type: (ApiNamespace, ApiRoute) -> Optional[Text]
"""
Returns pyxl with curl examples code or None if no example could be
generated.
full_route_name - string with "<namespace>/<route_name>".
attrs - a dictionary of string keys to values that are either int,
float, bool, str, or None. These are the route attributes assigned
in the spec.
"""
datatype, full_route_name, attrs = route.arg_data_type, format_route(
namespace.name, route.name, version=route.version), route.attrs
if is_void_type(datatype):
arg_dict = None
else:
# Always demo compact serialization for requests.
examples = datatype.get_examples(compact=True)
if not examples:
print("{} is missing example".format(datatype.name))
return None
arg_dict = examples.get("default")
if not arg_dict:
# If there is no example with label "default", let's just take
# the first example from the spec.
arg_dict = examples.values()[0]
subdomain = attrs.get("host", "api")
style = attrs.get("style", "rpc")
auth = attrs.get("auth", "user")
command = "curl -X POST https://{subdomain}.dropboxapi.com/2/{full_route_name}".format(
subdomain=subdomain, full_route_name=full_route_name
)
curl_data = None
if arg_dict is not None:
curl_data = escape_shell_argument(
json.dumps(arg_dict.value, separators=(",", ": "))
)
if auth == "app":
command += ' \\\n --header "Authorization: Basic [app_key_and_secret]"'
elif auth != "noauth":
command += ' \\\n --header "Authorization: Bearer [access_token]"'
if style in ("upload", "download"):
if curl_data is not None:
command += ' \\\n --header "Dropbox-API-Arg: {curl_data}"'.format(
curl_data=curl_data
)
if style == "upload":
command += ' \\\n --header "Content-Type: application/octet-stream"'
command += " \\\n --data-binary @local_file.txt"
elif style == "rpc" and (curl_data is not None):
command += (
' \\\n --header "Content-Type: application/json" \\\n'
+ ' --data "{curl_data}"'
).format(curl_data=curl_data)
return command
def escape_shell_argument(s):
# type: (Text) -> Text
# Feel free to improve, but remember that it should work for Unix and Windows.
return s.replace('"', '\\"')
def format_route(namespace, name, version=None, as_link_url=False):
# type: (Text, Any, Optional[Any], bool) -> Text
str = "{}/".format(namespace)
if version is not None and (version > 1 or as_link_url):
str += "{}_v{}".format(name, version)
else:
str += name
if as_link_url:
str = _format_link_url(str)
return str
def _format_link_url(name):
# type: (Text) -> Text
"""Format String for identifier representation given route or type names."""
return "-".join(name.split("/"))