Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 1 | #!/usr/bin/python |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 2 | # |
Craig Citro | 751b7fb | 2014-09-23 11:20:38 -0700 | [diff] [blame] | 3 | # Copyright 2014 Google Inc. All Rights Reserved. |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 17 | """Create documentation for generate API surfaces. |
| 18 | |
| 19 | Command-line tool that creates documentation for all APIs listed in discovery. |
| 20 | The documentation is generated from a combination of the discovery document and |
| 21 | the generated API surface itself. |
| 22 | """ |
Christian Clauss | 9fdc2b2 | 2019-07-22 19:43:21 +0200 | [diff] [blame] | 23 | from __future__ import print_function |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 24 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 25 | __author__ = "jcgregorio@google.com (Joe Gregorio)" |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 26 | |
Bu Sun Kim | c977304 | 2019-07-17 14:03:17 -0700 | [diff] [blame] | 27 | from collections import OrderedDict |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 28 | import argparse |
Bu Sun Kim | c977304 | 2019-07-17 14:03:17 -0700 | [diff] [blame] | 29 | import collections |
Craig Citro | 6ae34d7 | 2014-08-18 23:10:09 -0700 | [diff] [blame] | 30 | import json |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 31 | import os |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 32 | import re |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 33 | import string |
Joe Gregorio | 20a5aa9 | 2011-04-01 17:44:25 -0400 | [diff] [blame] | 34 | import sys |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 35 | |
John Asmuth | 864311d | 2014-04-24 15:46:08 -0400 | [diff] [blame] | 36 | from googleapiclient.discovery import DISCOVERY_URI |
| 37 | from googleapiclient.discovery import build |
| 38 | from googleapiclient.discovery import build_from_document |
Jon Wayne Parrott | fd2f99c | 2016-02-19 16:02:04 -0800 | [diff] [blame] | 39 | from googleapiclient.discovery import UnknownApiNameOrVersion |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 40 | from googleapiclient.http import build_http |
Dan O'Meara | dd49464 | 2020-05-01 07:42:23 -0700 | [diff] [blame^] | 41 | from googleapiclient.errors import HttpError |
| 42 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 43 | import uritemplate |
| 44 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 45 | CSS = """<style> |
| 46 | |
| 47 | body, h1, h2, h3, div, span, p, pre, a { |
| 48 | margin: 0; |
| 49 | padding: 0; |
| 50 | border: 0; |
| 51 | font-weight: inherit; |
| 52 | font-style: inherit; |
| 53 | font-size: 100%; |
| 54 | font-family: inherit; |
| 55 | vertical-align: baseline; |
| 56 | } |
| 57 | |
| 58 | body { |
| 59 | font-size: 13px; |
| 60 | padding: 1em; |
| 61 | } |
| 62 | |
| 63 | h1 { |
| 64 | font-size: 26px; |
| 65 | margin-bottom: 1em; |
| 66 | } |
| 67 | |
| 68 | h2 { |
| 69 | font-size: 24px; |
| 70 | margin-bottom: 1em; |
| 71 | } |
| 72 | |
| 73 | h3 { |
| 74 | font-size: 20px; |
| 75 | margin-bottom: 1em; |
| 76 | margin-top: 1em; |
| 77 | } |
| 78 | |
| 79 | pre, code { |
| 80 | line-height: 1.5; |
| 81 | font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Lucida Console', monospace; |
| 82 | } |
| 83 | |
| 84 | pre { |
| 85 | margin-top: 0.5em; |
| 86 | } |
| 87 | |
| 88 | h1, h2, h3, p { |
| 89 | font-family: Arial, sans serif; |
| 90 | } |
| 91 | |
| 92 | h1, h2, h3 { |
| 93 | border-bottom: solid #CCC 1px; |
| 94 | } |
| 95 | |
| 96 | .toc_element { |
| 97 | margin-top: 0.5em; |
| 98 | } |
| 99 | |
| 100 | .firstline { |
| 101 | margin-left: 2 em; |
| 102 | } |
| 103 | |
| 104 | .method { |
| 105 | margin-top: 1em; |
| 106 | border: solid 1px #CCC; |
| 107 | padding: 1em; |
| 108 | background: #EEE; |
| 109 | } |
| 110 | |
| 111 | .details { |
| 112 | font-weight: bold; |
| 113 | font-size: 14px; |
| 114 | } |
| 115 | |
| 116 | </style> |
| 117 | """ |
| 118 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 119 | METHOD_TEMPLATE = """<div class="method"> |
| 120 | <code class="details" id="$name">$name($params)</code> |
| 121 | <pre>$doc</pre> |
| 122 | </div> |
| 123 | """ |
| 124 | |
| 125 | COLLECTION_LINK = """<p class="toc_element"> |
| 126 | <code><a href="$href">$name()</a></code> |
| 127 | </p> |
| 128 | <p class="firstline">Returns the $name Resource.</p> |
| 129 | """ |
| 130 | |
| 131 | METHOD_LINK = """<p class="toc_element"> |
| 132 | <code><a href="#$name">$name($params)</a></code></p> |
| 133 | <p class="firstline">$firstline</p>""" |
| 134 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 135 | BASE = "docs/dyn" |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 136 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 137 | DIRECTORY_URI = "https://www.googleapis.com/discovery/v1/apis" |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 138 | |
Joe Gregorio | 79daca0 | 2013-03-29 16:25:52 -0400 | [diff] [blame] | 139 | parser = argparse.ArgumentParser(description=__doc__) |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 140 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 141 | parser.add_argument( |
| 142 | "--discovery_uri_template", |
| 143 | default=DISCOVERY_URI, |
| 144 | help="URI Template for discovery.", |
| 145 | ) |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 146 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 147 | parser.add_argument( |
| 148 | "--discovery_uri", |
| 149 | default="", |
| 150 | help=( |
| 151 | "URI of discovery document. If supplied then only " |
| 152 | "this API will be documented." |
| 153 | ), |
| 154 | ) |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 155 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 156 | parser.add_argument( |
| 157 | "--directory_uri", |
| 158 | default=DIRECTORY_URI, |
| 159 | help=("URI of directory document. Unused if --discovery_uri" " is supplied."), |
| 160 | ) |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 161 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 162 | parser.add_argument( |
| 163 | "--dest", default=BASE, help="Directory name to write documents into." |
| 164 | ) |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 165 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 166 | |
| 167 | def safe_version(version): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 168 | """Create a safe version of the verion string. |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 169 | |
| 170 | Needed so that we can distinguish between versions |
| 171 | and sub-collections in URIs. I.e. we don't want |
| 172 | adsense_v1.1 to refer to the '1' collection in the v1 |
| 173 | version of the adsense api. |
| 174 | |
| 175 | Args: |
| 176 | version: string, The version string. |
| 177 | Returns: |
| 178 | The string with '.' replaced with '_'. |
| 179 | """ |
| 180 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 181 | return version.replace(".", "_") |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 182 | |
| 183 | |
| 184 | def unsafe_version(version): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 185 | """Undoes what safe_version() does. |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 186 | |
| 187 | See safe_version() for the details. |
| 188 | |
| 189 | |
| 190 | Args: |
| 191 | version: string, The safe version string. |
| 192 | Returns: |
| 193 | The string with '_' replaced with '.'. |
| 194 | """ |
| 195 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 196 | return version.replace("_", ".") |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 197 | |
| 198 | |
| 199 | def method_params(doc): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 200 | """Document the parameters of a method. |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 201 | |
| 202 | Args: |
| 203 | doc: string, The method's docstring. |
| 204 | |
| 205 | Returns: |
| 206 | The method signature as a string. |
| 207 | """ |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 208 | doclines = doc.splitlines() |
| 209 | if "Args:" in doclines: |
| 210 | begin = doclines.index("Args:") |
| 211 | if "Returns:" in doclines[begin + 1 :]: |
| 212 | end = doclines.index("Returns:", begin) |
| 213 | args = doclines[begin + 1 : end] |
| 214 | else: |
| 215 | args = doclines[begin + 1 :] |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 216 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 217 | parameters = [] |
| 218 | pname = None |
| 219 | desc = "" |
| 220 | |
| 221 | def add_param(pname, desc): |
| 222 | if pname is None: |
| 223 | return |
| 224 | if "(required)" not in desc: |
| 225 | pname = pname + "=None" |
| 226 | parameters.append(pname) |
| 227 | |
| 228 | for line in args: |
| 229 | m = re.search("^\s+([a-zA-Z0-9_]+): (.*)", line) |
| 230 | if m is None: |
| 231 | desc += line |
| 232 | continue |
| 233 | add_param(pname, desc) |
| 234 | pname = m.group(1) |
| 235 | desc = m.group(2) |
| 236 | add_param(pname, desc) |
| 237 | parameters = ", ".join(parameters) |
| 238 | else: |
| 239 | parameters = "" |
| 240 | return parameters |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 241 | |
| 242 | |
| 243 | def method(name, doc): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 244 | """Documents an individual method. |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 245 | |
| 246 | Args: |
| 247 | name: string, Name of the method. |
| 248 | doc: string, The methods docstring. |
| 249 | """ |
| 250 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 251 | params = method_params(doc) |
Billy SU | 45cace8 | 2020-04-22 03:46:31 +0800 | [diff] [blame] | 252 | if sys.version_info.major >= 3: |
| 253 | import html |
| 254 | doc = html.escape(doc) |
| 255 | else: |
| 256 | import cgi |
| 257 | doc = cgi.escape(doc) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 258 | return string.Template(METHOD_TEMPLATE).substitute( |
| 259 | name=name, params=params, doc=doc |
| 260 | ) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 261 | |
| 262 | |
| 263 | def breadcrumbs(path, root_discovery): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 264 | """Create the breadcrumb trail to this page of documentation. |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 265 | |
| 266 | Args: |
| 267 | path: string, Dot separated name of the resource. |
| 268 | root_discovery: Deserialized discovery document. |
| 269 | |
| 270 | Returns: |
| 271 | HTML with links to each of the parent resources of this resource. |
| 272 | """ |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 273 | parts = path.split(".") |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 274 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 275 | crumbs = [] |
| 276 | accumulated = [] |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 277 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 278 | for i, p in enumerate(parts): |
| 279 | prefix = ".".join(accumulated) |
| 280 | # The first time through prefix will be [], so we avoid adding in a |
| 281 | # superfluous '.' to prefix. |
| 282 | if prefix: |
| 283 | prefix += "." |
| 284 | display = p |
| 285 | if i == 0: |
| 286 | display = root_discovery.get("title", display) |
| 287 | crumbs.append('<a href="%s.html">%s</a>' % (prefix + p, display)) |
| 288 | accumulated.append(p) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 289 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 290 | return " . ".join(crumbs) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 291 | |
| 292 | |
| 293 | def document_collection(resource, path, root_discovery, discovery, css=CSS): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 294 | """Document a single collection in an API. |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 295 | |
| 296 | Args: |
| 297 | resource: Collection or service being documented. |
| 298 | path: string, Dot separated name of the resource. |
| 299 | root_discovery: Deserialized discovery document. |
| 300 | discovery: Deserialized discovery document, but just the portion that |
| 301 | describes the resource. |
| 302 | css: string, The CSS to include in the generated file. |
| 303 | """ |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 304 | collections = [] |
| 305 | methods = [] |
| 306 | resource_name = path.split(".")[-2] |
| 307 | html = [ |
| 308 | "<html><body>", |
| 309 | css, |
| 310 | "<h1>%s</h1>" % breadcrumbs(path[:-1], root_discovery), |
| 311 | "<h2>Instance Methods</h2>", |
| 312 | ] |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 313 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 314 | # Which methods are for collections. |
| 315 | for name in dir(resource): |
| 316 | if not name.startswith("_") and callable(getattr(resource, name)): |
| 317 | if hasattr(getattr(resource, name), "__is_resource__"): |
| 318 | collections.append(name) |
| 319 | else: |
| 320 | methods.append(name) |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 321 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 322 | # TOC |
| 323 | if collections: |
| 324 | for name in collections: |
| 325 | if not name.startswith("_") and callable(getattr(resource, name)): |
| 326 | href = path + name + ".html" |
| 327 | html.append( |
| 328 | string.Template(COLLECTION_LINK).substitute(href=href, name=name) |
| 329 | ) |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 330 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 331 | if methods: |
| 332 | for name in methods: |
| 333 | if not name.startswith("_") and callable(getattr(resource, name)): |
| 334 | doc = getattr(resource, name).__doc__ |
| 335 | params = method_params(doc) |
| 336 | firstline = doc.splitlines()[0] |
| 337 | html.append( |
| 338 | string.Template(METHOD_LINK).substitute( |
| 339 | name=name, params=params, firstline=firstline |
| 340 | ) |
| 341 | ) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 342 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 343 | if methods: |
| 344 | html.append("<h3>Method Details</h3>") |
| 345 | for name in methods: |
| 346 | dname = name.rsplit("_")[0] |
| 347 | html.append(method(name, getattr(resource, name).__doc__)) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 348 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 349 | html.append("</body></html>") |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 350 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 351 | return "\n".join(html) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 352 | |
| 353 | |
| 354 | def document_collection_recursive(resource, path, root_discovery, discovery): |
| 355 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 356 | html = document_collection(resource, path, root_discovery, discovery) |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 357 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 358 | f = open(os.path.join(FLAGS.dest, path + "html"), "w") |
Billy SU | 84d4561 | 2020-04-21 06:15:56 +0800 | [diff] [blame] | 359 | if sys.version_info.major < 3: |
| 360 | html = html.encode("utf-8") |
| 361 | |
| 362 | f.write(html) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 363 | f.close() |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 364 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 365 | for name in dir(resource): |
| 366 | if ( |
| 367 | not name.startswith("_") |
| 368 | and callable(getattr(resource, name)) |
| 369 | and hasattr(getattr(resource, name), "__is_resource__") |
| 370 | and discovery != {} |
| 371 | ): |
| 372 | dname = name.rsplit("_")[0] |
| 373 | collection = getattr(resource, name)() |
| 374 | document_collection_recursive( |
| 375 | collection, |
| 376 | path + name + ".", |
| 377 | root_discovery, |
| 378 | discovery["resources"].get(dname, {}), |
| 379 | ) |
| 380 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 381 | |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 382 | def document_api(name, version): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 383 | """Document the given API. |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 384 | |
| 385 | Args: |
| 386 | name: string, Name of the API. |
| 387 | version: string, Version of the API. |
| 388 | """ |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 389 | try: |
| 390 | service = build(name, version) |
| 391 | except UnknownApiNameOrVersion as e: |
| 392 | print("Warning: {} {} found but could not be built.".format(name, version)) |
| 393 | return |
Dan O'Meara | dd49464 | 2020-05-01 07:42:23 -0700 | [diff] [blame^] | 394 | except HttpError as e: |
| 395 | print("Warning: {} {} returned {}.".format(name, version, e)) |
| 396 | return |
Jon Wayne Parrott | fd2f99c | 2016-02-19 16:02:04 -0800 | [diff] [blame] | 397 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 398 | http = build_http() |
| 399 | response, content = http.request( |
| 400 | uritemplate.expand( |
| 401 | FLAGS.discovery_uri_template, {"api": name, "apiVersion": version} |
| 402 | ) |
| 403 | ) |
| 404 | discovery = json.loads(content) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 405 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 406 | version = safe_version(version) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 407 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 408 | document_collection_recursive( |
| 409 | service, "%s_%s." % (name, version), discovery, discovery |
| 410 | ) |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 411 | |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 412 | |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 413 | def document_api_from_discovery_document(uri): |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 414 | """Document the given API. |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 415 | |
| 416 | Args: |
| 417 | uri: string, URI of discovery document. |
| 418 | """ |
Igor Maravić | 2243529 | 2017-01-19 22:28:22 +0100 | [diff] [blame] | 419 | http = build_http() |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 420 | response, content = http.request(FLAGS.discovery_uri) |
| 421 | discovery = json.loads(content) |
Bu Sun Kim | c977304 | 2019-07-17 14:03:17 -0700 | [diff] [blame] | 422 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 423 | service = build_from_document(discovery) |
Bu Sun Kim | c977304 | 2019-07-17 14:03:17 -0700 | [diff] [blame] | 424 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 425 | name = discovery["version"] |
| 426 | version = safe_version(discovery["version"]) |
Bu Sun Kim | c977304 | 2019-07-17 14:03:17 -0700 | [diff] [blame] | 427 | |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 428 | document_collection_recursive( |
| 429 | service, "%s_%s." % (name, version), discovery, discovery |
| 430 | ) |
| 431 | |
| 432 | |
| 433 | if __name__ == "__main__": |
| 434 | FLAGS = parser.parse_args(sys.argv[1:]) |
| 435 | if FLAGS.discovery_uri: |
| 436 | document_api_from_discovery_document(FLAGS.discovery_uri) |
Joe Gregorio | bb96435 | 2013-03-03 20:45:29 -0500 | [diff] [blame] | 437 | else: |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 438 | api_directory = collections.defaultdict(list) |
| 439 | http = build_http() |
| 440 | resp, content = http.request( |
| 441 | FLAGS.directory_uri, headers={"X-User-IP": "0.0.0.0"} |
| 442 | ) |
| 443 | if resp.status == 200: |
| 444 | directory = json.loads(content)["items"] |
| 445 | for api in directory: |
| 446 | document_api(api["name"], api["version"]) |
| 447 | api_directory[api["name"]].append(api["version"]) |
| 448 | |
| 449 | # sort by api name and version number |
| 450 | for api in api_directory: |
| 451 | api_directory[api] = sorted(api_directory[api]) |
| 452 | api_directory = OrderedDict( |
| 453 | sorted(api_directory.items(), key=lambda x: x[0]) |
| 454 | ) |
| 455 | |
| 456 | markdown = [] |
| 457 | for api, versions in api_directory.items(): |
| 458 | markdown.append("## %s" % api) |
| 459 | for version in versions: |
| 460 | markdown.append( |
| 461 | "* [%s](http://googleapis.github.io/google-api-python-client/docs/dyn/%s_%s.html)" |
| 462 | % (version, api, version) |
| 463 | ) |
| 464 | markdown.append("\n") |
| 465 | |
| 466 | with open("docs/dyn/index.md", "w") as f: |
Billy SU | 84d4561 | 2020-04-21 06:15:56 +0800 | [diff] [blame] | 467 | markdown = "\n".join(markdown) |
| 468 | if sys.version_info.major < 3: |
| 469 | markdown = markdown.encode("utf-8") |
| 470 | f.write(markdown) |
Bu Sun Kim | 66bb32c | 2019-10-30 10:11:58 -0700 | [diff] [blame] | 471 | |
| 472 | else: |
| 473 | sys.exit("Failed to load the discovery document.") |