blob: 1315c7f22b42e600c5b3ef6a1b508264aadb4b97 [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 Gregorioc5edb3c2011-03-18 10:54:10 -040021import httplib2
Joe Gregoriod44bb002010-09-21 14:34:44 -040022import inspect
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040023import os
Joe Gregorioc204b642010-09-21 12:01:23 -040024import pydoc
25import re
26
27from apiclient.discovery import build
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040028from apiclient.anyjson import simplejson
29from google.appengine.api import memcache
Joe Gregorioc204b642010-09-21 12:01:23 -040030from google.appengine.ext import webapp
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040031from google.appengine.ext.webapp import template
Joe Gregorioc204b642010-09-21 12:01:23 -040032from google.appengine.ext.webapp import util
33
Joe Gregoriod44bb002010-09-21 14:34:44 -040034
Joe Gregorioc204b642010-09-21 12:01:23 -040035class MainHandler(webapp.RequestHandler):
36
37 def get(self):
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040038 http = httplib2.Http(memcache)
39 resp, content = http.request('https://www.googleapis.com/discovery/v0.3/directory?preferred=true')
40 directory = simplejson.loads(content)['items']
41 path = os.path.join(os.path.dirname(__file__), 'index.html')
42 self.response.out.write(
43 template.render(
44 path, {'directory': directory,
45 }))
Joe Gregorioc204b642010-09-21 12:01:23 -040046
Joe Gregorio140062f2010-09-21 16:12:41 -040047
Joe Gregorioafc45f22011-02-20 16:11:28 -050048def render(resource):
49 obj, name = pydoc.resolve(type(resource))
50 return pydoc.html.page(
51 pydoc.describe(obj), pydoc.html.document(obj, name))
52
Joe Gregorioc204b642010-09-21 12:01:23 -040053
Joe Gregorio0fb19e12011-02-22 07:53:24 -050054class ResourceHandler(webapp.RequestHandler):
Joe Gregorioc204b642010-09-21 12:01:23 -040055
56 def get(self, service_name, version, collection):
Joe Gregorio0fb19e12011-02-22 07:53:24 -050057 resource = build(service_name, version)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040058 # descend the object path
Joe Gregorio0fb19e12011-02-22 07:53:24 -050059 if collection:
60 path = collection.split('/')
61 if path:
62 for method in path:
63 resource = getattr(resource, method)()
64 page = render(resource)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040065
Joe Gregorio0fb19e12011-02-22 07:53:24 -050066 collections = []
67 for name in dir(resource):
68 if not "_" in name and callable(getattr(resource, name)) and hasattr(
69 getattr(resource, name), '__is_resource__'):
70 collections.append(name)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040071
Joe Gregorio0fb19e12011-02-22 07:53:24 -050072 if collection is None:
73 collection_path = ''
74 else:
75 collection_path = collection + '/'
76 for name in collections:
77 page = re.sub('strong>(%s)<' % name,
78 r'strong><a href="/%s/%s/%s">\1</a><' % (
79 service_name, version, collection_path + name), page)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040080
Joe Gregorio0fb19e12011-02-22 07:53:24 -050081 # TODO(jcgregorio) breadcrumbs
82 # TODO(jcgregorio) sample code?
83 page = re.sub('<p>', r'<a href="/">Home</a><p>', page, 1)
Joe Gregorioc204b642010-09-21 12:01:23 -040084 self.response.out.write(page)
85
Joe Gregorio140062f2010-09-21 16:12:41 -040086
Joe Gregorioc204b642010-09-21 12:01:23 -040087def main():
88 application = webapp.WSGIApplication(
89 [
90 (r'/', MainHandler),
Joe Gregorio0fb19e12011-02-22 07:53:24 -050091 (r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -040092 ],
93 debug=True)
94 util.run_wsgi_app(application)
95
96
97if __name__ == '__main__':
98 main()