blob: 3d12824fdd26e9bdf92c3838eebaf7e22e909ac9 [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>
Joe Gregorio06d60a02010-11-05 10:10:15 -040068 <li><a href='/customsearch/v1'>customsearch</a>
69 <li><a href='/diacritize/v1'>diacritize</a>
70 <li><a href='/translate/v2'>translate</a>
Joe Gregorioc204b642010-09-21 12:01:23 -040071 </ul>
72 """)
73
Joe Gregorio140062f2010-09-21 16:12:41 -040074
Joe Gregorioc204b642010-09-21 12:01:23 -040075class ServiceHandler(webapp.RequestHandler):
76
77 def get(self, service_name, version):
78 service = build(service_name, version)
Joe Gregoriod44bb002010-09-21 14:34:44 -040079 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(service))
Joe Gregorioc204b642010-09-21 12:01:23 -040080
81 collections = []
82 for name in dir(service):
83 if not "_" in name and callable(getattr(service, name)):
84 collections.append(name)
85
86 for name in collections:
87 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, name), page)
88
89 self.response.out.write(page)
90
Joe Gregorio140062f2010-09-21 16:12:41 -040091
Joe Gregorioc204b642010-09-21 12:01:23 -040092class CollectionHandler(webapp.RequestHandler):
93
94 def get(self, service_name, version, collection):
95 service = build(service_name, version)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040096 # descend the object path
97 path = collection.split("/")
98 if path:
99 for method in path[:-1]:
100 service = getattr(service, method)()
101 method = getattr(service, path[-1])
102 obj = method()
103 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(obj))
104
105 if hasattr(method, '__is_resource__'):
106 collections = []
107 for name in dir(obj):
108 if not "_" in name and callable(getattr(obj, name)) and hasattr(getattr(obj, name), '__is_resource__'):
109 collections.append(name)
110
111 for name in collections:
112 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, collection + "/" + name), page)
113
Joe Gregorioc204b642010-09-21 12:01:23 -0400114 self.response.out.write(page)
115
Joe Gregorio140062f2010-09-21 16:12:41 -0400116
Joe Gregorioc204b642010-09-21 12:01:23 -0400117def main():
118 application = webapp.WSGIApplication(
119 [
120 (r'/', MainHandler),
121 (r'/(\w*)/(\w*)', ServiceHandler),
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400122 (r'/(\w*)/(\w*)/(.*)', CollectionHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -0400123 ],
124 debug=True)
125 util.run_wsgi_app(application)
126
127
128if __name__ == '__main__':
129 main()