Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 3 | # Copyright 2011 Google Inc. |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 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 | # |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 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 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 24 | |
| 25 | __author__ = 'jcgregorio@google.com (Joe Gregorio)' |
| 26 | |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 27 | import httplib2 |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 28 | import inspect |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 29 | import logging |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 30 | import os |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 31 | import pydoc |
| 32 | import re |
| 33 | |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 34 | import describe |
| 35 | import uritemplate |
| 36 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 37 | from apiclient import discovery |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 38 | from apiclient.errors import HttpError |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 39 | from google.appengine.api import memcache |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 40 | from google.appengine.ext import webapp |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 41 | from google.appengine.ext.webapp import template |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 42 | from google.appengine.ext.webapp import util |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 43 | from oauth2client.anyjson import simplejson |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 44 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 45 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 46 | DISCOVERY_URI = 'https://www.googleapis.com/discovery/v1/apis?preferred=true' |
| 47 | |
| 48 | |
| 49 | def get_directory_doc(): |
| 50 | http = httplib2.Http(memcache) |
| 51 | ip = os.environ.get('REMOTE_ADDR', None) |
| 52 | uri = DISCOVERY_URI |
| 53 | if ip: |
| 54 | uri += ('&userIp=' + ip) |
| 55 | resp, content = http.request(uri) |
| 56 | directory = simplejson.loads(content)['items'] |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 57 | for item in directory: |
| 58 | item['title'] = item.get('title', item.get('description', '')) |
| 59 | item['safe_version'] = describe.safe_version(item['version']) |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 60 | return directory |
| 61 | |
| 62 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 63 | class MainHandler(webapp.RequestHandler): |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 64 | """Handles serving the main landing page. |
| 65 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 66 | |
| 67 | def get(self): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 68 | directory = get_directory_doc() |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 69 | path = os.path.join(os.path.dirname(__file__), 'index.html') |
| 70 | self.response.out.write( |
| 71 | template.render( |
| 72 | path, {'directory': directory, |
| 73 | })) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 74 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 75 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 76 | class GadgetHandler(webapp.RequestHandler): |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 77 | """Handles serving the Google Gadget.""" |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 78 | |
| 79 | def get(self): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 80 | directory = get_directory_doc() |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 81 | path = os.path.join(os.path.dirname(__file__), 'gadget.html') |
| 82 | self.response.out.write( |
| 83 | template.render( |
| 84 | path, {'directory': directory, |
| 85 | })) |
| 86 | self.response.headers.add_header('Content-Type', 'application/xml') |
| 87 | |
| 88 | |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 89 | class EmbedHandler(webapp.RequestHandler): |
| 90 | """Handles serving a front page suitable for embedding.""" |
| 91 | |
| 92 | def get(self): |
| 93 | directory = get_directory_doc() |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 94 | path = os.path.join(os.path.dirname(__file__), 'embed.html') |
| 95 | self.response.out.write( |
| 96 | template.render( |
| 97 | path, {'directory': directory, |
| 98 | })) |
| 99 | |
| 100 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 101 | class ResourceHandler(webapp.RequestHandler): |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 102 | """Handles serving the PyDoc for a given collection. |
| 103 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 104 | |
| 105 | def get(self, service_name, version, collection): |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 106 | |
| 107 | real_version = describe.unsafe_version(version) |
Joe Gregorio | 994123c | 2012-12-10 15:14:18 -0500 | [diff] [blame^] | 108 | return self.redirect('https://google-api-client-libraries.appspot.com/documentation/%s/%s/python/latest/%s_%s.%s.html' |
| 109 | % (service_name, real_version, service_name, real_version, collection)) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 110 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 111 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 112 | def main(): |
| 113 | application = webapp.WSGIApplication( |
| 114 | [ |
| 115 | (r'/', MainHandler), |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 116 | (r'/_gadget/', GadgetHandler), |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame] | 117 | (r'/_embed/', EmbedHandler), |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 118 | (r'/([^_]+)_([^\.]+)(?:\.(.*))?\.html$', ResourceHandler), |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 119 | ], |
Joe Gregorio | 81d92cc | 2012-07-09 16:46:02 -0400 | [diff] [blame] | 120 | debug=True) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 121 | util.run_wsgi_app(application) |
| 122 | |
| 123 | |
| 124 | if __name__ == '__main__': |
| 125 | main() |