Add some command-line flags to control describe.
Reviewed in https://codereview.appspot.com/7438047/
This commit is contained in:
61
describe.py
61
describe.py
@@ -31,12 +31,13 @@ import httplib2
|
|||||||
from string import Template
|
from string import Template
|
||||||
|
|
||||||
from apiclient.discovery import build
|
from apiclient.discovery import build
|
||||||
|
from apiclient.discovery import build_from_document
|
||||||
|
from apiclient.discovery import DISCOVERY_URI
|
||||||
from oauth2client.anyjson import simplejson
|
from oauth2client.anyjson import simplejson
|
||||||
|
import gflags
|
||||||
import uritemplate
|
import uritemplate
|
||||||
|
|
||||||
|
|
||||||
BASE = 'docs/dyn'
|
|
||||||
|
|
||||||
CSS = """<style>
|
CSS = """<style>
|
||||||
|
|
||||||
body, h1, h2, h3, div, span, p, pre, a {
|
body, h1, h2, h3, div, span, p, pre, a {
|
||||||
@@ -111,9 +112,6 @@ h1, h2, h3 {
|
|||||||
</style>
|
</style>
|
||||||
"""
|
"""
|
||||||
|
|
||||||
DISCOVERY_URI = ('https://www.googleapis.com/discovery/v1/apis/'
|
|
||||||
'{api}/{apiVersion}/rest')
|
|
||||||
|
|
||||||
METHOD_TEMPLATE = """<div class="method">
|
METHOD_TEMPLATE = """<div class="method">
|
||||||
<code class="details" id="$name">$name($params)</code>
|
<code class="details" id="$name">$name($params)</code>
|
||||||
<pre>$doc</pre>
|
<pre>$doc</pre>
|
||||||
@@ -130,6 +128,24 @@ METHOD_LINK = """<p class="toc_element">
|
|||||||
<code><a href="#$name">$name($params)</a></code></p>
|
<code><a href="#$name">$name($params)</a></code></p>
|
||||||
<p class="firstline">$firstline</p>"""
|
<p class="firstline">$firstline</p>"""
|
||||||
|
|
||||||
|
BASE = 'docs/dyn'
|
||||||
|
|
||||||
|
DIRECTORY_URI = 'https://www.googleapis.com/discovery/v1/apis?preferred=true',
|
||||||
|
|
||||||
|
FLAGS = gflags.FLAGS
|
||||||
|
|
||||||
|
gflags.DEFINE_string('discovery_uri_template', DISCOVERY_URI,
|
||||||
|
'URI Template for discovery.')
|
||||||
|
|
||||||
|
gflags.DEFINE_string('discovery_uri', '', 'URI of discovery document. '
|
||||||
|
'If supplied then only this API will be documented.')
|
||||||
|
|
||||||
|
gflags.DEFINE_string('directory_uri', DIRECTORY_URI,
|
||||||
|
'URI of directory document. '
|
||||||
|
'Unused if --discovery_uri is supplied.')
|
||||||
|
|
||||||
|
gflags.DEFINE_string('dest', BASE, 'Directory name to write documents into.')
|
||||||
|
|
||||||
|
|
||||||
def safe_version(version):
|
def safe_version(version):
|
||||||
"""Create a safe version of the verion string.
|
"""Create a safe version of the verion string.
|
||||||
@@ -300,7 +316,7 @@ def document_collection_recursive(resource, path, root_discovery, discovery):
|
|||||||
|
|
||||||
html = document_collection(resource, path, root_discovery, discovery)
|
html = document_collection(resource, path, root_discovery, discovery)
|
||||||
|
|
||||||
f = open(os.path.join(BASE, path + 'html'), 'w')
|
f = open(os.path.join(FLAGS.dest, path + 'html'), 'w')
|
||||||
f.write(html.encode('utf-8'))
|
f.write(html.encode('utf-8'))
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
@@ -323,7 +339,7 @@ def document_api(name, version):
|
|||||||
service = build(name, version)
|
service = build(name, version)
|
||||||
response, content = http.request(
|
response, content = http.request(
|
||||||
uritemplate.expand(
|
uritemplate.expand(
|
||||||
DISCOVERY_URI, {
|
FLAGS.discovery_uri_template, {
|
||||||
'api': name,
|
'api': name,
|
||||||
'apiVersion': version})
|
'apiVersion': version})
|
||||||
)
|
)
|
||||||
@@ -335,10 +351,39 @@ def document_api(name, version):
|
|||||||
service, '%s_%s.' % (name, version), discovery, discovery)
|
service, '%s_%s.' % (name, version), discovery, discovery)
|
||||||
|
|
||||||
|
|
||||||
|
def document_api_from_discovery_document(uri):
|
||||||
|
"""Document the given API.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
uri: string, URI of discovery document.
|
||||||
|
"""
|
||||||
|
http = httplib2.Http()
|
||||||
|
response, content = http.request(FLAGS.discovery_uri)
|
||||||
|
discovery = simplejson.loads(content)
|
||||||
|
|
||||||
|
service = build_from_document(discovery)
|
||||||
|
|
||||||
|
name = discovery['version']
|
||||||
|
version = safe_version(discovery['version'])
|
||||||
|
|
||||||
|
document_collection_recursive(
|
||||||
|
service, '%s_%s.' % (name, version), discovery, discovery)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
# Let the gflags module process the command-line arguments
|
||||||
|
try:
|
||||||
|
argv = FLAGS(sys.argv)
|
||||||
|
except gflags.FlagsError, e:
|
||||||
|
print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
if FLAGS.discovery_uri:
|
||||||
|
document_api_from_discovery_document(FLAGS.discovery_uri)
|
||||||
|
else:
|
||||||
http = httplib2.Http()
|
http = httplib2.Http()
|
||||||
resp, content = http.request(
|
resp, content = http.request(
|
||||||
'https://www.googleapis.com/discovery/v1/apis?preferred=true',
|
FLAGS.directory_uri,
|
||||||
headers={'X-User-IP': '0.0.0.0'})
|
headers={'X-User-IP': '0.0.0.0'})
|
||||||
if resp.status == 200:
|
if resp.status == 200:
|
||||||
directory = simplejson.loads(content)['items']
|
directory = simplejson.loads(content)['items']
|
||||||
|
|||||||
Reference in New Issue
Block a user