3838from googleapiclient .http import build_http
3939
4040DISCOVERY_DOC_DIR = (
41- pathlib .Path (__file__ ).parent . resolve ()
41+ pathlib .Path (__file__ ).resolve (). parent
4242 / "googleapiclient"
4343 / "discovery_cache"
4444 / "documents"
134134 <code><a href="#$name">$name($params)</a></code></p>
135135<p class="firstline">$firstline</p>"""
136136
137- BASE = pathlib .Path (__file__ ).parent . resolve () / "docs" / "dyn"
137+ BASE = pathlib .Path (__file__ ).resolve (). parent / "docs" / "dyn"
138138
139139DIRECTORY_URI = "https://www.googleapis.com/discovery/v1/apis"
140140
@@ -356,7 +356,12 @@ def document_collection(resource, path, root_discovery, discovery, css=CSS):
356356
357357
358358def document_collection_recursive (
359- resource , path , root_discovery , discovery , doc_destination_dir
359+ resource ,
360+ path ,
361+ root_discovery ,
362+ discovery ,
363+ doc_destination_dir ,
364+ artifact_destination_dir = DISCOVERY_DOC_DIR ,
360365):
361366 html = document_collection (resource , path , root_discovery , discovery )
362367
@@ -380,10 +385,13 @@ def document_collection_recursive(
380385 root_discovery ,
381386 discovery ["resources" ].get (dname , {}),
382387 doc_destination_dir ,
388+ artifact_destination_dir ,
383389 )
384390
385391
386- def document_api (name , version , uri , doc_destination_dir ):
392+ def document_api (
393+ name , version , uri , doc_destination_dir , artifact_destination_dir = DISCOVERY_DOC_DIR
394+ ):
387395 """Document the given API.
388396
389397 Args:
@@ -392,6 +400,8 @@ def document_api(name, version, uri, doc_destination_dir):
392400 uri (str): URI of the API's discovery document
393401 doc_destination_dir (str): relative path where the reference
394402 documentation should be saved.
403+ artifact_destination_dir (str): relative path where the discovery
404+ artifacts should be saved.
395405 """
396406 http = build_http ()
397407 resp , content = http .request (
@@ -405,7 +415,7 @@ def document_api(name, version, uri, doc_destination_dir):
405415 discovery = json .loads (content )
406416 service = build_from_document (discovery )
407417 doc_name = "{}.{}.json" .format (name , version )
408- discovery_file_path = DISCOVERY_DOC_DIR / doc_name
418+ discovery_file_path = artifact_destination_dir / doc_name
409419 revision = None
410420
411421 pathlib .Path (discovery_file_path ).touch (exist_ok = True )
@@ -445,16 +455,21 @@ def document_api(name, version, uri, doc_destination_dir):
445455 discovery ,
446456 discovery ,
447457 doc_destination_dir ,
458+ artifact_destination_dir ,
448459 )
449460
450461
451- def document_api_from_discovery_document (discovery_url , doc_destination_dir ):
462+ def document_api_from_discovery_document (
463+ discovery_url , doc_destination_dir , artifact_destination_dir = DISCOVERY_DOC_DIR
464+ ):
452465 """Document the given API.
453466
454467 Args:
455468 discovery_url (str): URI of discovery document.
456469 doc_destination_dir (str): relative path where the reference
457470 documentation should be saved.
471+ artifact_destination_dir (str): relative path where the discovery
472+ artifacts should be saved.
458473 """
459474 http = build_http ()
460475 response , content = http .request (discovery_url )
@@ -471,16 +486,23 @@ def document_api_from_discovery_document(discovery_url, doc_destination_dir):
471486 discovery ,
472487 discovery ,
473488 doc_destination_dir ,
489+ artifact_destination_dir ,
474490 )
475491
476492
477- def generate_all_api_documents (directory_uri = DIRECTORY_URI , doc_destination_dir = BASE ):
493+ def generate_all_api_documents (
494+ directory_uri = DIRECTORY_URI ,
495+ doc_destination_dir = BASE ,
496+ artifact_destination_dir = DISCOVERY_DOC_DIR ,
497+ ):
478498 """Retrieve discovery artifacts and fetch reference documentations
479499 for all apis listed in the public discovery directory.
480500 args:
481501 directory_uri (str): uri of the public discovery directory.
482502 doc_destination_dir (str): relative path where the reference
483503 documentation should be saved.
504+ artifact_destination_dir (str): relative path where the discovery
505+ artifacts should be saved.
484506 """
485507 api_directory = collections .defaultdict (list )
486508 http = build_http ()
@@ -493,6 +515,7 @@ def generate_all_api_documents(directory_uri=DIRECTORY_URI, doc_destination_dir=
493515 api ["version" ],
494516 api ["discoveryRestUrl" ],
495517 doc_destination_dir ,
518+ artifact_destination_dir ,
496519 )
497520 api_directory [api ["name" ]].append (api ["version" ])
498521
@@ -513,7 +536,7 @@ def generate_all_api_documents(directory_uri=DIRECTORY_URI, doc_destination_dir=
513536 )
514537 markdown .append ("\n " )
515538
516- with open (BASE / "index.md" , "w" ) as f :
539+ with open (doc_destination_dir / "index.md" , "w" ) as f :
517540 markdown = "\n " .join (markdown )
518541 f .write (markdown )
519542
@@ -525,9 +548,11 @@ def generate_all_api_documents(directory_uri=DIRECTORY_URI, doc_destination_dir=
525548 FLAGS = parser .parse_args (sys .argv [1 :])
526549 if FLAGS .discovery_uri :
527550 document_api_from_discovery_document (
528- discovery_url = FLAGS .discovery_uri , doc_destination_dir = FLAGS .dest
551+ discovery_url = FLAGS .discovery_uri ,
552+ doc_destination_dir = FLAGS .dest ,
529553 )
530554 else :
531555 generate_all_api_documents (
532- directory_uri = FLAGS .directory_uri , doc_destination_dir = FLAGS .dest
556+ directory_uri = FLAGS .directory_uri ,
557+ doc_destination_dir = FLAGS .dest ,
533558 )
0 commit comments