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 | |
| 21 | import httplib2 |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame^] | 22 | import inspect |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 23 | import logging |
| 24 | import os |
| 25 | import pydoc |
| 26 | import re |
| 27 | |
| 28 | from apiclient.discovery import build |
| 29 | |
| 30 | from google.appengine.api import users |
| 31 | from google.appengine.ext import webapp |
| 32 | from google.appengine.ext.webapp import template |
| 33 | from google.appengine.ext.webapp import util |
| 34 | |
| 35 | STEP2_URI = 'http://%s.appspot.com/auth_return' % os.environ['APPLICATION_ID'] |
| 36 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame^] | 37 | |
| 38 | # Replicate render_doc here from pydoc.py as it isn't available in Python 2.5 |
| 39 | class _OldStyleClass: pass |
| 40 | |
| 41 | def render_doc(thing, title='Python Library Documentation: %s', forceload=0): |
| 42 | """Render text documentation, given an object or a path to an object.""" |
| 43 | object, name = pydoc.resolve(thing, forceload) |
| 44 | desc = pydoc.describe(object) |
| 45 | module = inspect.getmodule(object) |
| 46 | if name and '.' in name: |
| 47 | desc += ' in ' + name[:name.rfind('.')] |
| 48 | elif module and module is not object: |
| 49 | desc += ' in module ' + module.__name__ |
| 50 | if type(object) is type(_OldStyleClass()): |
| 51 | # If the passed object is an instance of an old-style class, |
| 52 | # document its available methods instead of its value. |
| 53 | object = object.__class__ |
| 54 | elif not (inspect.ismodule(object) or |
| 55 | inspect.isclass(object) or |
| 56 | inspect.isroutine(object) or |
| 57 | inspect.isgetsetdescriptor(object) or |
| 58 | inspect.ismemberdescriptor(object) or |
| 59 | isinstance(object, property)): |
| 60 | # If the passed object is a piece of data or an instance, |
| 61 | # document its available methods instead of its value. |
| 62 | object = type(object) |
| 63 | desc += ' object' |
| 64 | return title % desc + '\n\n' + pydoc.text.document(object, name) |
| 65 | |
| 66 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 67 | class MainHandler(webapp.RequestHandler): |
| 68 | |
| 69 | def get(self): |
| 70 | self.response.out.write(""" |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame^] | 71 | <h1>Google API Client for Python Documentation</h1> |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 72 | <ul> |
| 73 | <li><a href='/buzz/v1'>buzz</a> |
| 74 | <li><a href='/moderator/v1'>moderator</a> |
| 75 | <li><a href='/latitude/v1'>latitude</a> |
| 76 | </ul> |
| 77 | """) |
| 78 | |
| 79 | class ServiceHandler(webapp.RequestHandler): |
| 80 | |
| 81 | def get(self, service_name, version): |
| 82 | service = build(service_name, version) |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame^] | 83 | page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(service)) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 84 | |
| 85 | collections = [] |
| 86 | for name in dir(service): |
| 87 | if not "_" in name and callable(getattr(service, name)): |
| 88 | collections.append(name) |
| 89 | |
| 90 | for name in collections: |
| 91 | page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, name), page) |
| 92 | |
| 93 | self.response.out.write(page) |
| 94 | |
| 95 | class CollectionHandler(webapp.RequestHandler): |
| 96 | |
| 97 | def get(self, service_name, version, collection): |
| 98 | service = build(service_name, version) |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame^] | 99 | page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(getattr(service, collection)())) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 100 | self.response.out.write(page) |
| 101 | |
| 102 | def main(): |
| 103 | application = webapp.WSGIApplication( |
| 104 | [ |
| 105 | (r'/', MainHandler), |
| 106 | (r'/(\w*)/(\w*)', ServiceHandler), |
| 107 | (r'/(\w*)/(\w*)/(\w*)', CollectionHandler), |
| 108 | ], |
| 109 | debug=True) |
| 110 | util.run_wsgi_app(application) |
| 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | main() |