blob: 14f222e1f3b0655e490af3941146c23b793777d8 [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
Joe Gregorioaf276d22010-12-09 14:26:58 -050031
32
33class _OldStyleClass:
34 pass
35
Joe Gregoriod44bb002010-09-21 14:34:44 -040036
37def render_doc(thing, title='Python Library Documentation: %s', forceload=0):
38 """Render text documentation, given an object or a path to an object."""
39 object, name = pydoc.resolve(thing, forceload)
40 desc = pydoc.describe(object)
41 module = inspect.getmodule(object)
42 if name and '.' in name:
43 desc += ' in ' + name[:name.rfind('.')]
44 elif module and module is not object:
45 desc += ' in module ' + module.__name__
46 if type(object) is type(_OldStyleClass()):
47 # If the passed object is an instance of an old-style class,
48 # document its available methods instead of its value.
49 object = object.__class__
50 elif not (inspect.ismodule(object) or
51 inspect.isclass(object) or
52 inspect.isroutine(object) or
53 inspect.isgetsetdescriptor(object) or
54 inspect.ismemberdescriptor(object) or
55 isinstance(object, property)):
56 # If the passed object is a piece of data or an instance,
57 # document its available methods instead of its value.
58 object = type(object)
59 desc += ' object'
60 return title % desc + '\n\n' + pydoc.text.document(object, name)
61
62
Joe Gregorioc204b642010-09-21 12:01:23 -040063class MainHandler(webapp.RequestHandler):
64
65 def get(self):
66 self.response.out.write("""
Joe Gregoriod44bb002010-09-21 14:34:44 -040067 <h1>Google API Client for Python Documentation</h1>
Joe Gregorioc204b642010-09-21 12:01:23 -040068 <ul>
69 <li><a href='/buzz/v1'>buzz</a>
70 <li><a href='/moderator/v1'>moderator</a>
71 <li><a href='/latitude/v1'>latitude</a>
Joe Gregorio06d60a02010-11-05 10:10:15 -040072 <li><a href='/customsearch/v1'>customsearch</a>
73 <li><a href='/diacritize/v1'>diacritize</a>
74 <li><a href='/translate/v2'>translate</a>
Joe Gregorioa12c7d62010-11-29 07:55:08 -050075 <li><a href='/prediction/v1.1'>prediction</a>
Joe Gregorioec736d92010-12-21 16:26:14 -050076 <li><a href='/shopping/v1'>shopping</a>
Joe Gregorioc204b642010-09-21 12:01:23 -040077 </ul>
78 """)
79
Joe Gregorio140062f2010-09-21 16:12:41 -040080
Joe Gregorioc204b642010-09-21 12:01:23 -040081class ServiceHandler(webapp.RequestHandler):
82
83 def get(self, service_name, version):
84 service = build(service_name, version)
Joe Gregorioaf276d22010-12-09 14:26:58 -050085 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % (
86 pydoc.plain(render_doc(service)),)
Joe Gregorioc204b642010-09-21 12:01:23 -040087
88 collections = []
89 for name in dir(service):
90 if not "_" in name and callable(getattr(service, name)):
91 collections.append(name)
92
93 for name in collections:
Joe Gregorioaf276d22010-12-09 14:26:58 -050094 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (
95 service_name, version, name), page)
Joe Gregorioc204b642010-09-21 12:01:23 -040096
97 self.response.out.write(page)
98
Joe Gregorio140062f2010-09-21 16:12:41 -040099
Joe Gregorioc204b642010-09-21 12:01:23 -0400100class CollectionHandler(webapp.RequestHandler):
101
102 def get(self, service_name, version, collection):
103 service = build(service_name, version)
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400104 # descend the object path
105 path = collection.split("/")
106 if path:
107 for method in path[:-1]:
108 service = getattr(service, method)()
109 method = getattr(service, path[-1])
110 obj = method()
Joe Gregorioaf276d22010-12-09 14:26:58 -0500111 page = "<p><a href='/'>Home</a></p><pre>%s</pre>" % (
112 pydoc.plain(render_doc(obj)),)
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400113
114 if hasattr(method, '__is_resource__'):
115 collections = []
116 for name in dir(obj):
Joe Gregorioaf276d22010-12-09 14:26:58 -0500117 if not "_" in name and callable(getattr(obj, name)) and hasattr(
118 getattr(obj, name), '__is_resource__'):
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400119 collections.append(name)
120
121 for name in collections:
Joe Gregorioaf276d22010-12-09 14:26:58 -0500122 page = re.sub('(%s) =' % name, r'<a href="/%s/%s/%s">\1</a> =' % (
123 service_name, version, collection + "/" + name), page)
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400124
Joe Gregorioc204b642010-09-21 12:01:23 -0400125 self.response.out.write(page)
126
Joe Gregorio140062f2010-09-21 16:12:41 -0400127
Joe Gregorioc204b642010-09-21 12:01:23 -0400128def main():
129 application = webapp.WSGIApplication(
130 [
131 (r'/', MainHandler),
Joe Gregorioa12c7d62010-11-29 07:55:08 -0500132 (r'/([^\/]*)/([^\/]*)', ServiceHandler),
133 (r'/([^\/]*)/([^\/]*)/(.*)', CollectionHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -0400134 ],
135 debug=True)
136 util.run_wsgi_app(application)
137
138
139if __name__ == '__main__':
140 main()