blob: 8918c9b7ab47dd347ad71f79506ad9bad1b55dee [file] [log] [blame]
Joe Gregorioc204b642010-09-21 12:01:23 -04001#!/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
21import httplib2
Joe Gregoriod44bb002010-09-21 14:34:44 -040022import inspect
Joe Gregorioc204b642010-09-21 12:01:23 -040023import logging
24import os
25import pydoc
26import re
27
28from apiclient.discovery import build
29
30from google.appengine.api import users
31from google.appengine.ext import webapp
32from google.appengine.ext.webapp import template
33from google.appengine.ext.webapp import util
34
35STEP2_URI = 'http://%s.appspot.com/auth_return' % os.environ['APPLICATION_ID']
36
Joe Gregoriod44bb002010-09-21 14:34:44 -040037
38# Replicate render_doc here from pydoc.py as it isn't available in Python 2.5
39class _OldStyleClass: pass
40
41def 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 Gregorioc204b642010-09-21 12:01:23 -040067class MainHandler(webapp.RequestHandler):
68
69 def get(self):
70 self.response.out.write("""
Joe Gregoriod44bb002010-09-21 14:34:44 -040071 <h1>Google API Client for Python Documentation</h1>
Joe Gregorioc204b642010-09-21 12:01:23 -040072 <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
79class ServiceHandler(webapp.RequestHandler):
80
81 def get(self, service_name, version):
82 service = build(service_name, version)
Joe Gregoriod44bb002010-09-21 14:34:44 -040083 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(service))
Joe Gregorioc204b642010-09-21 12:01:23 -040084
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
95class CollectionHandler(webapp.RequestHandler):
96
97 def get(self, service_name, version, collection):
98 service = build(service_name, version)
Joe Gregoriod44bb002010-09-21 14:34:44 -040099 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(getattr(service, collection)()))
Joe Gregorioc204b642010-09-21 12:01:23 -0400100 self.response.out.write(page)
101
102def 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
113if __name__ == '__main__':
114 main()