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