blob: 506f27aafa41b4e8341fc96305b09fe23633274a [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
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040020import httplib2
Joe Gregoriod44bb002010-09-21 14:34:44 -040021import inspect
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040022import os
Joe Gregorioc204b642010-09-21 12:01:23 -040023import pydoc
24import re
25
26from apiclient.discovery import build
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040027from apiclient.anyjson import simplejson
28from google.appengine.api import memcache
Joe Gregorioc204b642010-09-21 12:01:23 -040029from google.appengine.ext import webapp
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040030from google.appengine.ext.webapp import template
Joe Gregorioc204b642010-09-21 12:01:23 -040031from google.appengine.ext.webapp import util
32
Joe Gregoriod44bb002010-09-21 14:34:44 -040033
Joe Gregorioc204b642010-09-21 12:01:23 -040034class MainHandler(webapp.RequestHandler):
35
36 def get(self):
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040037 http = httplib2.Http(memcache)
Joe Gregorio6a63a762011-05-02 22:36:05 -040038 resp, content = http.request('https://www.googleapis.com/discovery/v1/apis?preferred=true')
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040039 directory = simplejson.loads(content)['items']
Joe Gregorio6a63a762011-05-02 22:36:05 -040040 for item in directory:
41 item['title'] = item.get('title', item.get('description', ''))
Joe Gregorioc5edb3c2011-03-18 10:54:10 -040042 path = os.path.join(os.path.dirname(__file__), 'index.html')
43 self.response.out.write(
44 template.render(
45 path, {'directory': directory,
46 }))
Joe Gregorioc204b642010-09-21 12:01:23 -040047
Joe Gregorio140062f2010-09-21 16:12:41 -040048
Joe Gregorioafc45f22011-02-20 16:11:28 -050049def render(resource):
50 obj, name = pydoc.resolve(type(resource))
51 return pydoc.html.page(
52 pydoc.describe(obj), pydoc.html.document(obj, name))
53
Joe Gregorioc204b642010-09-21 12:01:23 -040054
Joe Gregorio0fb19e12011-02-22 07:53:24 -050055class ResourceHandler(webapp.RequestHandler):
Joe Gregorioc204b642010-09-21 12:01:23 -040056
57 def get(self, service_name, version, collection):
Joe Gregorio0fb19e12011-02-22 07:53:24 -050058 resource = build(service_name, version)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040059 # descend the object path
Joe Gregorio0fb19e12011-02-22 07:53:24 -050060 if collection:
61 path = collection.split('/')
62 if path:
63 for method in path:
64 resource = getattr(resource, method)()
65 page = render(resource)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040066
Joe Gregorio0fb19e12011-02-22 07:53:24 -050067 collections = []
68 for name in dir(resource):
69 if not "_" in name and callable(getattr(resource, name)) and hasattr(
70 getattr(resource, name), '__is_resource__'):
71 collections.append(name)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040072
Joe Gregorio0fb19e12011-02-22 07:53:24 -050073 if collection is None:
74 collection_path = ''
75 else:
76 collection_path = collection + '/'
77 for name in collections:
78 page = re.sub('strong>(%s)<' % name,
79 r'strong><a href="/%s/%s/%s">\1</a><' % (
80 service_name, version, collection_path + name), page)
Joe Gregorio20cfcda2010-10-26 11:58:08 -040081
Joe Gregorio0fb19e12011-02-22 07:53:24 -050082 # TODO(jcgregorio) breadcrumbs
83 # TODO(jcgregorio) sample code?
84 page = re.sub('<p>', r'<a href="/">Home</a><p>', page, 1)
Joe Gregorioc204b642010-09-21 12:01:23 -040085 self.response.out.write(page)
86
Joe Gregorio140062f2010-09-21 16:12:41 -040087
Joe Gregorioc204b642010-09-21 12:01:23 -040088def main():
89 application = webapp.WSGIApplication(
90 [
91 (r'/', MainHandler),
Joe Gregorio0fb19e12011-02-22 07:53:24 -050092 (r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler),
Joe Gregorioc204b642010-09-21 12:01:23 -040093 ],
94 debug=True)
95 util.run_wsgi_app(application)
96
97
98if __name__ == '__main__':
99 main()