24
24
25
25
__author__ = "[email protected] (Joe Gregorio)"
26
26
27
- from collections import OrderedDict
28
27
import argparse
29
28
import collections
30
29
import json
36
35
from googleapiclient .discovery import DISCOVERY_URI
37
36
from googleapiclient .discovery import build
38
37
from googleapiclient .discovery import build_from_document
39
- from googleapiclient .discovery import UnknownApiNameOrVersion
40
38
from googleapiclient .http import build_http
41
- from googleapiclient .errors import HttpError
42
39
43
40
import uritemplate
44
41
45
42
DISCOVERY_DOC_DIR = (
46
- pathlib .Path (__file__ ).parent .resolve () / "googleapiclient" / "discovery_cache" / "documents"
43
+ pathlib .Path (__file__ ).parent .resolve ()
44
+ / "googleapiclient"
45
+ / "discovery_cache"
46
+ / "documents"
47
47
)
48
48
49
49
CSS = """<style>
171
171
def safe_version (version ):
172
172
"""Create a safe version of the verion string.
173
173
174
- Needed so that we can distinguish between versions
175
- and sub-collections in URIs. I.e. we don't want
176
- adsense_v1.1 to refer to the '1' collection in the v1
177
- version of the adsense api.
174
+ Needed so that we can distinguish between versions
175
+ and sub-collections in URIs. I.e. we don't want
176
+ adsense_v1.1 to refer to the '1' collection in the v1
177
+ version of the adsense api.
178
178
179
- Args:
180
- version: string, The version string.
181
- Returns:
182
- The string with '.' replaced with '_'.
183
- """
179
+ Args:
180
+ version: string, The version string.
181
+ Returns:
182
+ The string with '.' replaced with '_'.
183
+ """
184
184
185
185
return version .replace ("." , "_" )
186
186
187
187
188
188
def unsafe_version (version ):
189
189
"""Undoes what safe_version() does.
190
190
191
- See safe_version() for the details.
191
+ See safe_version() for the details.
192
192
193
193
194
- Args:
195
- version: string, The safe version string.
196
- Returns:
197
- The string with '_' replaced with '.'.
198
- """
194
+ Args:
195
+ version: string, The safe version string.
196
+ Returns:
197
+ The string with '_' replaced with '.'.
198
+ """
199
199
200
200
return version .replace ("_" , "." )
201
201
202
202
203
203
def method_params (doc ):
204
204
"""Document the parameters of a method.
205
205
206
- Args:
207
- doc: string, The method's docstring.
206
+ Args:
207
+ doc: string, The method's docstring.
208
208
209
- Returns:
210
- The method signature as a string.
211
- """
209
+ Returns:
210
+ The method signature as a string.
211
+ """
212
212
doclines = doc .splitlines ()
213
213
if "Args:" in doclines :
214
214
begin = doclines .index ("Args:" )
@@ -253,10 +253,10 @@ def add_param(pname, desc):
253
253
def method (name , doc ):
254
254
"""Documents an individual method.
255
255
256
- Args:
257
- name: string, Name of the method.
258
- doc: string, The methods docstring.
259
- """
256
+ Args:
257
+ name: string, Name of the method.
258
+ doc: string, The methods docstring.
259
+ """
260
260
import html
261
261
262
262
params = method_params (doc )
@@ -269,13 +269,13 @@ def method(name, doc):
269
269
def breadcrumbs (path , root_discovery ):
270
270
"""Create the breadcrumb trail to this page of documentation.
271
271
272
- Args:
273
- path: string, Dot separated name of the resource.
274
- root_discovery: Deserialized discovery document.
272
+ Args:
273
+ path: string, Dot separated name of the resource.
274
+ root_discovery: Deserialized discovery document.
275
275
276
- Returns:
277
- HTML with links to each of the parent resources of this resource.
278
- """
276
+ Returns:
277
+ HTML with links to each of the parent resources of this resource.
278
+ """
279
279
parts = path .split ("." )
280
280
281
281
crumbs = []
@@ -299,14 +299,14 @@ def breadcrumbs(path, root_discovery):
299
299
def document_collection (resource , path , root_discovery , discovery , css = CSS ):
300
300
"""Document a single collection in an API.
301
301
302
- Args:
303
- resource: Collection or service being documented.
304
- path: string, Dot separated name of the resource.
305
- root_discovery: Deserialized discovery document.
306
- discovery: Deserialized discovery document, but just the portion that
307
- describes the resource.
308
- css: string, The CSS to include in the generated file.
309
- """
302
+ Args:
303
+ resource: Collection or service being documented.
304
+ path: string, Dot separated name of the resource.
305
+ root_discovery: Deserialized discovery document.
306
+ discovery: Deserialized discovery document, but just the portion that
307
+ describes the resource.
308
+ css: string, The CSS to include in the generated file.
309
+ """
310
310
collections = []
311
311
methods = []
312
312
resource_name = path .split ("." )[- 2 ]
@@ -357,7 +357,9 @@ def document_collection(resource, path, root_discovery, discovery, css=CSS):
357
357
return "\n " .join (html )
358
358
359
359
360
- def document_collection_recursive (resource , path , root_discovery , discovery , doc_destination_dir ):
360
+ def document_collection_recursive (
361
+ resource , path , root_discovery , discovery , doc_destination_dir
362
+ ):
361
363
html = document_collection (resource , path , root_discovery , discovery )
362
364
363
365
f = open (pathlib .Path (doc_destination_dir ).joinpath (path + "html" ), "w" )
@@ -379,7 +381,7 @@ def document_collection_recursive(resource, path, root_discovery, discovery, doc
379
381
path + name + "." ,
380
382
root_discovery ,
381
383
discovery ["resources" ].get (dname , {}),
382
- doc_destination_dir
384
+ doc_destination_dir ,
383
385
)
384
386
385
387
@@ -392,10 +394,11 @@ def document_api(name, version, uri, doc_destination_dir):
392
394
uri (str): URI of the API's discovery document
393
395
doc_destination_dir (str): relative path where the reference
394
396
documentation should be saved.
395
- """
397
+ """
396
398
http = build_http ()
397
399
resp , content = http .request (
398
- uri or uritemplate .expand (
400
+ uri
401
+ or uritemplate .expand (
399
402
FLAGS .discovery_uri_template , {"api" : name , "apiVersion" : version }
400
403
)
401
404
)
@@ -413,11 +416,11 @@ def document_api(name, version, uri, doc_destination_dir):
413
416
with open (discovery_file_path , "r+" ) as f :
414
417
try :
415
418
json_data = json .load (f )
416
- revision = json_data [' revision' ]
419
+ revision = json_data [" revision" ]
417
420
except json .JSONDecodeError :
418
421
revision = None
419
422
420
- if revision is None or discovery [' revision' ] >= revision :
423
+ if revision is None or discovery [" revision" ] >= revision :
421
424
# Reset position to the beginning
422
425
f .seek (0 )
423
426
# Write the changes to disk
@@ -426,25 +429,35 @@ def document_api(name, version, uri, doc_destination_dir):
426
429
f .truncate ()
427
430
428
431
elif resp .status == 404 :
429
- print ("Warning: {} {} not found. HTTP Code: {}" .format (name , version , resp .status ))
432
+ print (
433
+ "Warning: {} {} not found. HTTP Code: {}" .format (name , version , resp .status )
434
+ )
430
435
return
431
436
else :
432
- print ("Warning: {} {} could not be built. HTTP Code: {}" .format (name , version , resp .status ))
437
+ print (
438
+ "Warning: {} {} could not be built. HTTP Code: {}" .format (
439
+ name , version , resp .status
440
+ )
441
+ )
433
442
return
434
443
435
444
document_collection_recursive (
436
- service , "{}_{}." .format (name , safe_version (version )), discovery , discovery , doc_destination_dir
445
+ service ,
446
+ "{}_{}." .format (name , safe_version (version )),
447
+ discovery ,
448
+ discovery ,
449
+ doc_destination_dir ,
437
450
)
438
451
439
452
440
453
def document_api_from_discovery_document (discovery_url , doc_destination_dir ):
441
454
"""Document the given API.
442
455
443
- Args:
444
- discovery_url (str): URI of discovery document.
445
- doc_destination_dir (str): relative path where the reference
446
- documentation should be saved.
447
- """
456
+ Args:
457
+ discovery_url (str): URI of discovery document.
458
+ doc_destination_dir (str): relative path where the reference
459
+ documentation should be saved.
460
+ """
448
461
http = build_http ()
449
462
response , content = http .request (discovery_url )
450
463
discovery = json .loads (content )
@@ -455,11 +468,16 @@ def document_api_from_discovery_document(discovery_url, doc_destination_dir):
455
468
version = safe_version (discovery ["version" ])
456
469
457
470
document_collection_recursive (
458
- service , "{}_{}." .format (name , version ), discovery , discovery , doc_destination_dir
471
+ service ,
472
+ "{}_{}." .format (name , version ),
473
+ discovery ,
474
+ discovery ,
475
+ doc_destination_dir ,
459
476
)
460
477
478
+
461
479
def generate_all_api_documents (directory_uri = DIRECTORY_URI , doc_destination_dir = BASE ):
462
- """ Retrieve discovery artifacts and fetch reference documentations
480
+ """Retrieve discovery artifacts and fetch reference documentations
463
481
for all apis listed in the public discovery directory.
464
482
args:
465
483
directory_uri (str): uri of the public discovery directory.
@@ -472,13 +490,18 @@ def generate_all_api_documents(directory_uri=DIRECTORY_URI, doc_destination_dir=
472
490
if resp .status == 200 :
473
491
directory = json .loads (content )["items" ]
474
492
for api in directory :
475
- document_api (api ["name" ], api ["version" ], api ["discoveryRestUrl" ], doc_destination_dir )
493
+ document_api (
494
+ api ["name" ],
495
+ api ["version" ],
496
+ api ["discoveryRestUrl" ],
497
+ doc_destination_dir ,
498
+ )
476
499
api_directory [api ["name" ]].append (api ["version" ])
477
500
478
501
# sort by api name and version number
479
502
for api in api_directory :
480
503
api_directory [api ] = sorted (api_directory [api ])
481
- api_directory = OrderedDict (
504
+ api_directory = collections . OrderedDict (
482
505
sorted (api_directory .items (), key = lambda x : x [0 ])
483
506
)
484
507
@@ -499,9 +522,14 @@ def generate_all_api_documents(directory_uri=DIRECTORY_URI, doc_destination_dir=
499
522
else :
500
523
sys .exit ("Failed to load the discovery document." )
501
524
525
+
502
526
if __name__ == "__main__" :
503
527
FLAGS = parser .parse_args (sys .argv [1 :])
504
528
if FLAGS .discovery_uri :
505
- document_api_from_discovery_document (discovery_url = FLAGS .discovery_uri , doc_destination_dir = FLAGS .dest )
529
+ document_api_from_discovery_document (
530
+ discovery_url = FLAGS .discovery_uri , doc_destination_dir = FLAGS .dest
531
+ )
506
532
else :
507
- generate_all_api_documents (directory_uri = FLAGS .directory_uri , doc_destination_dir = FLAGS .dest )
533
+ generate_all_api_documents (
534
+ directory_uri = FLAGS .directory_uri , doc_destination_dir = FLAGS .dest
535
+ )
0 commit comments