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 | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 77 | </ul> |
| 78 | """) |
| 79 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 80 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 81 | class ServiceHandler(webapp.RequestHandler): |
| 82 | |
| 83 | def get(self, service_name, version): |
| 84 | service = build(service_name, version) |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 85 | page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % ( |
| 86 | pydoc.plain(render_doc(service)),) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 87 | |
| 88 | collections = [] |
| 89 | for name in dir(service): |
| 90 | if not "_" in name and callable(getattr(service, name)): |
| 91 | collections.append(name) |
| 92 | |
| 93 | for name in collections: |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 94 | page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % ( |
| 95 | service_name, version, name), page) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 96 | |
| 97 | self.response.out.write(page) |
| 98 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 99 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 100 | class CollectionHandler(webapp.RequestHandler): |
| 101 | |
| 102 | def get(self, service_name, version, collection): |
| 103 | service = build(service_name, version) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 104 | # descend the object path |
| 105 | path = collection.split("/") |
| 106 | if path: |
| 107 | for method in path[:-1]: |
| 108 | service = getattr(service, method)() |
| 109 | method = getattr(service, path[-1]) |
| 110 | obj = method() |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 111 | page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % ( |
| 112 | pydoc.plain(render_doc(obj)),) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 113 | |
| 114 | if hasattr(method, '__is_resource__'): |
| 115 | collections = [] |
| 116 | for name in dir(obj): |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 117 | if not "_" in name and callable(getattr(obj, name)) and hasattr( |
| 118 | getattr(obj, name), '__is_resource__'): |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 119 | collections.append(name) |
| 120 | |
| 121 | for name in collections: |
Joe Gregorio | af276d2 | 2010-12-09 14:26:58 -0500 | [diff] [blame] | 122 | page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % ( |
| 123 | service_name, version, collection + "/" + name), page) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 124 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 125 | self.response.out.write(page) |
| 126 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 127 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 128 | def main(): |
| 129 | application = webapp.WSGIApplication( |
| 130 | [ |
| 131 | (r'/', MainHandler), |
Joe Gregorio | a12c7d6 | 2010-11-29 07:55:08 -0500 | [diff] [blame] | 132 | (r'/([^\/]*)/([^\/]*)', ServiceHandler), |
| 133 | (r'/([^\/]*)/([^\/]*)/(.*)', CollectionHandler), |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 134 | ], |
| 135 | debug=True) |
| 136 | util.run_wsgi_app(application) |
| 137 | |
| 138 | |
| 139 | if __name__ == '__main__': |
| 140 | main() |