blob: ae87b3e181352646ee424b8427d81d791e0895a2 [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
Joe Gregoriod44bb002010-09-21 14:34:44 -040021import inspect
Joe Gregorioc204b642010-09-21 12:01:23 -040022import pydoc
23import re
24
25from apiclient.discovery import build
26
Joe Gregorioc204b642010-09-21 12:01:23 -040027from google.appengine.ext import webapp
Joe Gregorioc204b642010-09-21 12:01:23 -040028from google.appengine.ext.webapp import util
29
Joe Gregoriod44bb002010-09-21 14:34:44 -040030# Replicate render_doc here from pydoc.py as it isn't available in Python 2.5
31class _OldStyleClass: pass
32
33def render_doc(thing, title='Python Library Documentation: %s', forceload=0):
34 """Render text documentation, given an object or a path to an object."""
35 object, name = pydoc.resolve(thing, forceload)
36 desc = pydoc.describe(object)
37 module = inspect.getmodule(object)
38 if name and '.' in name:
39 desc += ' in ' + name[:name.rfind('.')]
40 elif module and module is not object:
41 desc += ' in module ' + module.__name__
42 if type(object) is type(_OldStyleClass()):
43 # If the passed object is an instance of an old-style class,
44 # document its available methods instead of its value.
45 object = object.__class__
46 elif not (inspect.ismodule(object) or
47 inspect.isclass(object) or
48 inspect.isroutine(object) or
49 inspect.isgetsetdescriptor(object) or
50 inspect.ismemberdescriptor(object) or
51 isinstance(object, property)):
52 # If the passed object is a piece of data or an instance,
53 # document its available methods instead of its value.
54 object = type(object)
55 desc += ' object'
56 return title % desc + '\n\n' + pydoc.text.document(object, name)
57
58
Joe Gregorioc204b642010-09-21 12:01:23 -040059class MainHandler(webapp.RequestHandler):
60
61 def get(self):
62 self.response.out.write("""
Joe Gregoriod44bb002010-09-21 14:34:44 -040063 <h1>Google API Client for Python Documentation</h1>
Joe Gregorioc204b642010-09-21 12:01:23 -040064 <ul>
65 <li><a href='/buzz/v1'>buzz</a>
66 <li><a href='/moderator/v1'>moderator</a>
67 <li><a href='/latitude/v1'>latitude</a>
68 </ul>
69 """)
70
Joe Gregorio140062f2010-09-21 16:12:41 -040071
Joe Gregorioc204b642010-09-21 12:01:23 -040072class ServiceHandler(webapp.RequestHandler):
73
74 def get(self, service_name, version):
75 service = build(service_name, version)
Joe Gregoriod44bb002010-09-21 14:34:44 -040076 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(service))
Joe Gregorioc204b642010-09-21 12:01:23 -040077
78 collections = []
79 for name in dir(service):
80 if not "_" in name and callable(getattr(service, name)):
81 collections.append(name)
82
83 for name in collections:
84 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, name), page)
85
86 self.response.out.write(page)
87
Joe Gregorio140062f2010-09-21 16:12:41 -040088
Joe Gregorioc204b642010-09-21 12:01:23 -040089class CollectionHandler(webapp.RequestHandler):
90
91 def get(self, service_name, version, collection):
92 service = build(service_name, version)
Joe Gregoriod44bb002010-09-21 14:34:44 -040093 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(getattr(service, collection)()))
Joe Gregorioc204b642010-09-21 12:01:23 -040094 self.response.out.write(page)
95
Joe Gregorio140062f2010-09-21 16:12:41 -040096
Joe Gregorioc204b642010-09-21 12:01:23 -040097def main():
98 application = webapp.WSGIApplication(
99 [
100 (r'/', MainHandler),
101 (r'/(\w*)/(\w*)', ServiceHandler),
102 (r'/(\w*)/(\w*)/(\w*)', CollectionHandler),
103 ],
104 debug=True)
105 util.run_wsgi_app(application)
106
107
108if __name__ == '__main__':
109 main()