blob: b92b18d802d6b7ad5b41a25dff7461560d9d3098 [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 Gregorio430178b2010-11-05 12:35:14 -040071 <li><a href='/prediction/v1'>prediction</a>
Joe Gregorioc204b642010-09-21 12:01:23 -040072 </ul>
73 """)
74
Joe Gregorio140062f2010-09-21 16:12:41 -040075
Joe Gregorioc204b642010-09-21 12:01:23 -040076class ServiceHandler(webapp.RequestHandler):
77
78 def get(self, service_name, version):
79 service = build(service_name, version)
Joe Gregoriod44bb002010-09-21 14:34:44 -040080 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(service))
Joe Gregorioc204b642010-09-21 12:01:23 -040081
82 collections = []
83 for name in dir(service):
84 if not "_" in name and callable(getattr(service, name)):
85 collections.append(name)
86
87 for name in collections:
88 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, name), page)
89
90 self.response.out.write(page)
91
Joe Gregorio140062f2010-09-21 16:12:41 -040092
Joe Gregorioc204b642010-09-21 12:01:23 -040093class CollectionHandler(webapp.RequestHandler):
94
95 def get(self, service_name, version, collection):
96 service = build(service_name, version)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040097 # descend the object path
98 path = collection.split("/")
99 if path:
100 for method in path[:-1]:
101 service = getattr(service, method)()
102 method = getattr(service, path[-1])
103 obj = method()
104 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % pydoc.plain(render_doc(obj))
105
106 if hasattr(method, '__is_resource__'):
107 collections = []
108 for name in dir(obj):
109 if not "_" in name and callable(getattr(obj, name)) and hasattr(getattr(obj, name), '__is_resource__'):
110 collections.append(name)
111
112 for name in collections:
113 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (service_name, version, collection + "/" + name), page)
114
Joe Gregorioc204b642010-09-21 12:01:23 -0400115 self.response.out.write(page)
116
Joe Gregorio140062f2010-09-21 16:12:41 -0400117
Joe Gregorioc204b642010-09-21 12:01:23 -0400118def main():
119 application = webapp.WSGIApplication(
120 [
121 (r'/', MainHandler),
122 (r'/(\w*)/(\w*)', ServiceHandler),
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400123 (r'/(\w*)/(\w*)/(.*)', CollectionHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -0400124 ],
125 debug=True)
126 util.run_wsgi_app(application)
127
128
129if __name__ == '__main__':
130 main()