blob: bb0dc0402ec4a23f1d4a2be1929f2b78e5d1a710 [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 Gregorio88243be2011-01-11 14:13:06 -050077 <li><a href='/urlshortener/v1'>urlshortener</a>
Joe Gregorioc204b642010-09-21 12:01:23 -040078 </ul>
79 """)
80
Joe Gregorio140062f2010-09-21 16:12:41 -040081
Joe Gregorioafc45f22011-02-20 16:11:28 -050082def render(resource):
83 obj, name = pydoc.resolve(type(resource))
84 return pydoc.html.page(
85 pydoc.describe(obj), pydoc.html.document(obj, name))
86
Joe Gregorioc204b642010-09-21 12:01:23 -040087class ServiceHandler(webapp.RequestHandler):
88
89 def get(self, service_name, version):
90 service = build(service_name, version)
Joe Gregorioafc45f22011-02-20 16:11:28 -050091 page = render(service)
Joe Gregorioc204b642010-09-21 12:01:23 -040092
93 collections = []
94 for name in dir(service):
Joe Gregorioc3fae8a2011-02-18 14:19:50 -050095 if not "_" in name and callable(getattr(service, name)) and hasattr(
96 getattr(service, name), '__is_resource__'):
Joe Gregorioc204b642010-09-21 12:01:23 -040097 collections.append(name)
98
99 for name in collections:
Joe Gregorioafc45f22011-02-20 16:11:28 -0500100 page = re.sub('strong>(%s)<' % name, r'strong><a href="/%s/%s/%s">\1</a><' % (
Joe Gregorioaf276d22010-12-09 14:26:58 -0500101 service_name, version, name), page)
Joe Gregorioc204b642010-09-21 12:01:23 -0400102
103 self.response.out.write(page)
104
Joe Gregorio140062f2010-09-21 16:12:41 -0400105
Joe Gregorioc204b642010-09-21 12:01:23 -0400106class CollectionHandler(webapp.RequestHandler):
107
108 def get(self, service_name, version, collection):
109 service = build(service_name, version)
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400110 # descend the object path
111 path = collection.split("/")
112 if path:
113 for method in path[:-1]:
114 service = getattr(service, method)()
115 method = getattr(service, path[-1])
116 obj = method()
Joe Gregorioafc45f22011-02-20 16:11:28 -0500117 page = render(obj)
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400118
119 if hasattr(method, '__is_resource__'):
120 collections = []
121 for name in dir(obj):
Joe Gregorioaf276d22010-12-09 14:26:58 -0500122 if not "_" in name and callable(getattr(obj, name)) and hasattr(
123 getattr(obj, name), '__is_resource__'):
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400124 collections.append(name)
125
126 for name in collections:
Joe Gregorioafc45f22011-02-20 16:11:28 -0500127 page = re.sub('strong>(%s)<' % name, r'strong><a href="/%s/%s/%s">\1</a><' % (
Joe Gregorioaf276d22010-12-09 14:26:58 -0500128 service_name, version, collection + "/" + name), page)
Joe Gregorio20cfcda2010-10-26 11:58:08 -0400129
Joe Gregorioc204b642010-09-21 12:01:23 -0400130 self.response.out.write(page)
131
Joe Gregorio140062f2010-09-21 16:12:41 -0400132
Joe Gregorioc204b642010-09-21 12:01:23 -0400133def main():
134 application = webapp.WSGIApplication(
135 [
136 (r'/', MainHandler),
Joe Gregorioa12c7d62010-11-29 07:55:08 -0500137 (r'/([^\/]*)/([^\/]*)', ServiceHandler),
138 (r'/([^\/]*)/([^\/]*)/(.*)', CollectionHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -0400139 ],
140 debug=True)
141 util.run_wsgi_app(application)
142
143
144if __name__ == '__main__':
145 main()