Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright 2007 Google Inc. |
| 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 | # |
| 17 | |
| 18 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 19 | |
| 20 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 21 | import inspect |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 22 | import pydoc |
| 23 | import re |
| 24 | |
| 25 | from apiclient.discovery import build |
| 26 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 27 | from google.appengine.ext import webapp |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 28 | from google.appengine.ext.webapp import util |
| 29 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 30 | # Replicate render_doc here from pydoc.py as it isn't available in Python 2.5 |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 31 | |
| 32 | |
| 33 | class _OldStyleClass: |
| 34 | pass |
| 35 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 36 | |
| 37 | def render_doc(thing, title='Python Library Documentation: %s', forceload=0): |
| 38 | """Render text documentation, given an object or a path to an object.""" |
| 39 | object, name = pydoc.resolve(thing, forceload) |
| 40 | desc = pydoc.describe(object) |
| 41 | module = inspect.getmodule(object) |
| 42 | if name and '.' in name: |
| 43 | desc += ' in ' + name[:name.rfind('.')] |
| 44 | elif module and module is not object: |
| 45 | desc += ' in module ' + module.__name__ |
| 46 | if type(object) is type(_OldStyleClass()): |
| 47 | # If the passed object is an instance of an old-style class, |
| 48 | # document its available methods instead of its value. |
| 49 | object = object.__class__ |
| 50 | elif not (inspect.ismodule(object) or |
| 51 | inspect.isclass(object) or |
| 52 | inspect.isroutine(object) or |
| 53 | inspect.isgetsetdescriptor(object) or |
| 54 | inspect.ismemberdescriptor(object) or |
| 55 | isinstance(object, property)): |
| 56 | # If the passed object is a piece of data or an instance, |
| 57 | # document its available methods instead of its value. |
| 58 | object = type(object) |
| 59 | desc += ' object' |
| 60 | return title % desc + '\n\n' + pydoc.text.document(object, name) |
| 61 | |
| 62 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 63 | class MainHandler(webapp.RequestHandler): |
| 64 | |
| 65 | def get(self): |
| 66 | self.response.out.write(""" |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 67 | <h1>Google API Client for Python Documentation</h1> |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 68 | <ul> |
| 69 | <li><a href='/buzz/v1'>buzz</a> |
| 70 | <li><a href='/moderator/v1'>moderator</a> |
| 71 | <li><a href='/latitude/v1'>latitude</a> |
Joe Gregorio | 06d60a0 | 2010-11-05 10:10:15 -0400 | [diff] [blame] | 72 | <li><a href='/customsearch/v1'>customsearch</a> |
| 73 | <li><a href='/diacritize/v1'>diacritize</a> |
| 74 | <li><a href='/translate/v2'>translate</a> |
Joe Gregorio | a12c7d6 | 2010-11-29 07:55:08 -0500 | [diff] [blame] | 75 | <li><a href='/prediction/v1.1'>prediction</a> |
Joe Gregorio | ec736d9 | 2010-12-21 16:26:14 -0500 | [diff] [blame] | 76 | <li><a href='/shopping/v1'>shopping</a> |
Joe Gregorio | 88243be | 2011-01-11 14:13:06 -0500 | [diff] [blame] | 77 | <li><a href='/urlshortener/v1'>urlshortener</a> |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 78 | </ul> |
| 79 | """) |
| 80 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 81 | |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame^] | 82 | def render(resource): |
| 83 | obj, name = pydoc.resolve(type(resource)) |
| 84 | return pydoc.html.page( |
| 85 | pydoc.describe(obj), pydoc.html.document(obj, name)) |
| 86 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 87 | class ServiceHandler(webapp.RequestHandler): |
| 88 | |
| 89 | def get(self, service_name, version): |
| 90 | service = build(service_name, version) |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame^] | 91 | page = render(service) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 92 | |
| 93 | collections = [] |
| 94 | for name in dir(service): |
Joe Gregorio | c3fae8a | 2011-02-18 14:19:50 -0500 | [diff] [blame] | 95 | if not "_" in name and callable(getattr(service, name)) and hasattr( |
| 96 | getattr(service, name), '__is_resource__'): |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 97 | collections.append(name) |
| 98 | |
| 99 | for name in collections: |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame^] | 100 | page = re.sub('strong>(%s)<' % name, r'strong><a href="/%s/%s/%s">\1</a><' % ( |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 101 | service_name, version, name), page) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 102 | |
| 103 | self.response.out.write(page) |
| 104 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 105 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 106 | class CollectionHandler(webapp.RequestHandler): |
| 107 | |
| 108 | def get(self, service_name, version, collection): |
| 109 | service = build(service_name, version) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 110 | # descend the object path |
| 111 | path = collection.split("/") |
| 112 | if path: |
| 113 | for method in path[:-1]: |
| 114 | service = getattr(service, method)() |
| 115 | method = getattr(service, path[-1]) |
| 116 | obj = method() |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame^] | 117 | page = render(obj) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 118 | |
| 119 | if hasattr(method, '__is_resource__'): |
| 120 | collections = [] |
| 121 | for name in dir(obj): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 122 | if not "_" in name and callable(getattr(obj, name)) and hasattr( |
| 123 | getattr(obj, name), '__is_resource__'): |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 124 | collections.append(name) |
| 125 | |
| 126 | for name in collections: |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame^] | 127 | page = re.sub('strong>(%s)<' % name, r'strong><a href="/%s/%s/%s">\1</a><' % ( |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 128 | service_name, version, collection + "/" + name), page) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 129 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 130 | self.response.out.write(page) |
| 131 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 132 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 133 | def main(): |
| 134 | application = webapp.WSGIApplication( |
| 135 | [ |
| 136 | (r'/', MainHandler), |
Joe Gregorio | a12c7d6 | 2010-11-29 07:55:08 -0500 | [diff] [blame] | 137 | (r'/([^\/]*)/([^\/]*)', ServiceHandler), |
| 138 | (r'/([^\/]*)/([^\/]*)/(.*)', CollectionHandler), |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 139 | ], |
| 140 | debug=True) |
| 141 | util.run_wsgi_app(application) |
| 142 | |
| 143 | |
| 144 | if __name__ == '__main__': |
| 145 | main() |