blob: a2b67d56d453652615c656d7fc0f9180c849b337 [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
Joe Gregorioc204b642010-09-21 12:01:23 -040031class MainHandler(webapp.RequestHandler):
32
33 def get(self):
34 self.response.out.write("""
Joe Gregoriod44bb002010-09-21 14:34:44 -040035 <h1>Google API Client for Python Documentation</h1>
Joe Gregorioc204b642010-09-21 12:01:23 -040036 <ul>
37 <li><a href='/buzz/v1'>buzz</a>
38 <li><a href='/moderator/v1'>moderator</a>
39 <li><a href='/latitude/v1'>latitude</a>
Joe Gregorio06d60a02010-11-05 10:10:15 -040040 <li><a href='/customsearch/v1'>customsearch</a>
41 <li><a href='/diacritize/v1'>diacritize</a>
42 <li><a href='/translate/v2'>translate</a>
Joe Gregorioa12c7d62010-11-29 07:55:08 -050043 <li><a href='/prediction/v1.1'>prediction</a>
Joe Gregorioec736d92010-12-21 16:26:14 -050044 <li><a href='/shopping/v1'>shopping</a>
Joe Gregorio88243be2011-01-11 14:13:06 -050045 <li><a href='/urlshortener/v1'>urlshortener</a>
Joe Gregorioc204b642010-09-21 12:01:23 -040046 </ul>
47 """)
48
Joe Gregorio140062f2010-09-21 16:12:41 -040049
Joe Gregorioafc45f22011-02-20 16:11:28 -050050def render(resource):
51 obj, name = pydoc.resolve(type(resource))
52 return pydoc.html.page(
53 pydoc.describe(obj), pydoc.html.document(obj, name))
54
Joe Gregorioc204b642010-09-21 12:01:23 -040055
Joe Gregorio0fb19e12011-02-22 07:53:24 -050056class ResourceHandler(webapp.RequestHandler):
Joe Gregorioc204b642010-09-21 12:01:23 -040057
58 def get(self, service_name, version, collection):
Joe Gregorio0fb19e12011-02-22 07:53:24 -050059 resource = build(service_name, version)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040060 # descend the object path
Joe Gregorio0fb19e12011-02-22 07:53:24 -050061 if collection:
62 path = collection.split('/')
63 if path:
64 for method in path:
65 resource = getattr(resource, method)()
66 page = render(resource)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040067
Joe Gregorio0fb19e12011-02-22 07:53:24 -050068 collections = []
69 for name in dir(resource):
70 if not "_" in name and callable(getattr(resource, name)) and hasattr(
71 getattr(resource, name), '__is_resource__'):
72 collections.append(name)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040073
Joe Gregorio0fb19e12011-02-22 07:53:24 -050074 if collection is None:
75 collection_path = ''
76 else:
77 collection_path = collection + '/'
78 for name in collections:
79 page = re.sub('strong>(%s)<' % name,
80 r'strong><a href="/%s/%s/%s">\1</a><' % (
81 service_name, version, collection_path + name), page)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040082
Joe Gregorio0fb19e12011-02-22 07:53:24 -050083 # TODO(jcgregorio) breadcrumbs
84 # TODO(jcgregorio) sample code?
85 page = re.sub('<p>', r'<a href="/">Home</a><p>', page, 1)
Joe Gregorioc204b642010-09-21 12:01:23 -040086 self.response.out.write(page)
87
Joe Gregorio140062f2010-09-21 16:12:41 -040088
Joe Gregorioc204b642010-09-21 12:01:23 -040089def main():
90 application = webapp.WSGIApplication(
91 [
92 (r'/', MainHandler),
Joe Gregorio0fb19e12011-02-22 07:53:24 -050093 (r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -040094 ],
95 debug=True)
96 util.run_wsgi_app(application)
97
98
99if __name__ == '__main__':
100 main()