11#!/usr/bin/env python
22#
3- # Copyright 2007 Google Inc.
3+ # Copyright 2011 Google Inc.
44#
55# Licensed under the Apache License, Version 2.0 (the "License");
66# you may not use this file except in compliance with the License.
1414# See the License for the specific language governing permissions and
1515# limitations under the License.
1616#
17+ """Sample application for Python documentation of APIs.
18+
19+ This is running live at http://api-python-client-doc.appspot.com where it
20+ provides a list of APIs and PyDoc documentation for all the generated API
21+ surfaces as they appear in the google-api-python-client. In addition it also
22+ provides a Google Gadget.
23+ """
1724
1825__author__ = 'jcgregorio@google.com (Joe Gregorio)'
1926
2330import pydoc
2431import re
2532
26- from apiclient .discovery import build
2733from apiclient .anyjson import simplejson
34+ from apiclient import discovery
2835from google .appengine .api import memcache
2936from google .appengine .ext import webapp
3037from google .appengine .ext .webapp import template
3138from google .appengine .ext .webapp import util
3239
3340
3441class MainHandler (webapp .RequestHandler ):
42+ """Handles serving the main landing page.
43+ """
3544
3645 def get (self ):
3746 http = httplib2 .Http (memcache )
@@ -46,23 +55,45 @@ def get(self):
4655 }))
4756
4857
49- def render (resource ):
58+ class GadgetHandler (webapp .RequestHandler ):
59+ """Handles serving the Google Gadget.
60+ """
61+
62+ def get (self ):
63+ http = httplib2 .Http (memcache )
64+ resp , content = http .request ('https://www.googleapis.com/discovery/v1/apis?preferred=true' )
65+ directory = simplejson .loads (content )['items' ]
66+ for item in directory :
67+ item ['title' ] = item .get ('title' , item .get ('description' , '' ))
68+ path = os .path .join (os .path .dirname (__file__ ), 'gadget.html' )
69+ self .response .out .write (
70+ template .render (
71+ path , {'directory' : directory ,
72+ }))
73+ self .response .headers .add_header ('Content-Type' , 'application/xml' )
74+
75+
76+ def _render (resource ):
77+ """Use pydoc helpers on an instance to generate the help documentation.
78+ """
5079 obj , name = pydoc .resolve (type (resource ))
5180 return pydoc .html .page (
5281 pydoc .describe (obj ), pydoc .html .document (obj , name ))
5382
5483
5584class ResourceHandler (webapp .RequestHandler ):
85+ """Handles serving the PyDoc for a given collection.
86+ """
5687
5788 def get (self , service_name , version , collection ):
58- resource = build (service_name , version )
89+ resource = discovery . build (service_name , version )
5990 # descend the object path
6091 if collection :
6192 path = collection .split ('/' )
6293 if path :
6394 for method in path :
6495 resource = getattr (resource , method )()
65- page = render (resource )
96+ page = _render (resource )
6697
6798 collections = []
6899 for name in dir (resource ):
@@ -89,6 +120,7 @@ def main():
89120 application = webapp .WSGIApplication (
90121 [
91122 (r'/' , MainHandler ),
123+ (r'/_gadget/' , GadgetHandler ),
92124 (r'/([^\/]*)/([^\/]*)(?:/(.*))?' , ResourceHandler ),
93125 ],
94126 debug = True )
0 commit comments