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 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 31 | class MainHandler(webapp.RequestHandler): |
| 32 | |
| 33 | def get(self): |
| 34 | self.response.out.write(""" |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 35 | <h1>Google API Client for Python Documentation</h1> |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 36 | <ul> |
| 37 | <li><a href='/buzz/v1'>buzz</a> |
| 38 | <li><a href='/moderator/v1'>moderator</a> |
| 39 | <li><a href='/latitude/v1'>latitude</a> |
Joe Gregorio | 06d60a0 | 2010-11-05 10:10:15 -0400 | [diff] [blame] | 40 | <li><a href='/customsearch/v1'>customsearch</a> |
| 41 | <li><a href='/diacritize/v1'>diacritize</a> |
| 42 | <li><a href='/translate/v2'>translate</a> |
Joe Gregorio | a12c7d6 | 2010-11-29 07:55:08 -0500 | [diff] [blame] | 43 | <li><a href='/prediction/v1.1'>prediction</a> |
Joe Gregorio | ec736d9 | 2010-12-21 16:26:14 -0500 | [diff] [blame] | 44 | <li><a href='/shopping/v1'>shopping</a> |
Joe Gregorio | 88243be | 2011-01-11 14:13:06 -0500 | [diff] [blame] | 45 | <li><a href='/urlshortener/v1'>urlshortener</a> |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 46 | </ul> |
| 47 | """) |
| 48 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 49 | |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 50 | def render(resource): |
| 51 | obj, name = pydoc.resolve(type(resource)) |
| 52 | return pydoc.html.page( |
| 53 | pydoc.describe(obj), pydoc.html.document(obj, name)) |
| 54 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 55 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame^] | 56 | class ResourceHandler(webapp.RequestHandler): |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 57 | |
| 58 | def get(self, service_name, version, collection): |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame^] | 59 | resource = build(service_name, version) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 60 | # descend the object path |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame^] | 61 | if collection: |
| 62 | path = collection.split('/') |
| 63 | if path: |
| 64 | for method in path: |
| 65 | resource = getattr(resource, method)() |
| 66 | page = render(resource) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 67 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame^] | 68 | collections = [] |
| 69 | for name in dir(resource): |
| 70 | if not "_" in name and callable(getattr(resource, name)) and hasattr( |
| 71 | getattr(resource, name), '__is_resource__'): |
| 72 | collections.append(name) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 73 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame^] | 74 | if collection is None: |
| 75 | collection_path = '' |
| 76 | else: |
| 77 | collection_path = collection + '/' |
| 78 | for name in collections: |
| 79 | page = re.sub('strong>(%s)<' % name, |
| 80 | r'strong><a href="/%s/%s/%s">\1</a><' % ( |
| 81 | service_name, version, collection_path + name), page) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 82 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame^] | 83 | # TODO(jcgregorio) breadcrumbs |
| 84 | # TODO(jcgregorio) sample code? |
| 85 | page = re.sub('<p>', r'<a href="/">Home</a><p>', page, 1) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 86 | self.response.out.write(page) |
| 87 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 88 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 89 | def main(): |
| 90 | application = webapp.WSGIApplication( |
| 91 | [ |
| 92 | (r'/', MainHandler), |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame^] | 93 | (r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler), |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 94 | ], |
| 95 | debug=True) |
| 96 | util.run_wsgi_app(application) |
| 97 | |
| 98 | |
| 99 | if __name__ == '__main__': |
| 100 | main() |