blob: 9af42ad3f1d2a910675b35c7b837abd2129f9236 [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 Gregorio20cfcda2010-10-26 11:58:08 -040093 # descend the object path
94 path = collection.split("/")
95 if path:
96 for method in path[:-1]:
97 service = getattr(service, method)()
98 method = getattr(service, path[-1])
99 obj = method()
100 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(obj))
101
102 if hasattr(method, '__is_resource__'):
103 collections = []
104 for name in dir(obj):
105 if not "_" in name and callable(getattr(obj, name)) and hasattr(getattr(obj, name), '__is_resource__'):
106 collections.append(name)
107
108 for name in collections:
109 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, collection + "/" + name), page)
110
Joe Gregorioc204b642010-09-21 12:01:23 -0400111 self.response.out.write(page)
112
Joe Gregorio140062f2010-09-21 16:12:41 -0400113
Joe Gregorioc204b642010-09-21 12:01:23 -0400114def main():
115 application = webapp.WSGIApplication(
116 [
117 (r'/', MainHandler),
118 (r'/(\w*)/(\w*)', ServiceHandler),
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400119 (r'/(\w*)/(\w*)/(.*)', CollectionHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -0400120 ],
121 debug=True)
122 util.run_wsgi_app(application)
123
124
125if __name__ == '__main__':
126 main()