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 | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 29 | import os |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 30 | import pydoc |
| 31 | import re |
| 32 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 33 | from apiclient import discovery |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 34 | from apiclient.errors import HttpError |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 35 | from google.appengine.api import memcache |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 36 | from google.appengine.ext import webapp |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 37 | from google.appengine.ext.webapp import template |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 38 | from google.appengine.ext.webapp import util |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame^] | 39 | from oauth2client.anyjson import simplejson |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 40 | |
Joe Gregorio | d44bb00 | 2010-09-21 14:34:44 -0400 | [diff] [blame] | 41 | |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 42 | DISCOVERY_URI = 'https://www.googleapis.com/discovery/v1/apis?preferred=true' |
| 43 | |
| 44 | |
| 45 | def get_directory_doc(): |
| 46 | http = httplib2.Http(memcache) |
| 47 | ip = os.environ.get('REMOTE_ADDR', None) |
| 48 | uri = DISCOVERY_URI |
| 49 | if ip: |
| 50 | uri += ('&userIp=' + ip) |
| 51 | resp, content = http.request(uri) |
| 52 | directory = simplejson.loads(content)['items'] |
| 53 | return directory |
| 54 | |
| 55 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 56 | class MainHandler(webapp.RequestHandler): |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 57 | """Handles serving the main landing page. |
| 58 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 59 | |
| 60 | def get(self): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 61 | directory = get_directory_doc() |
Joe Gregorio | 6a63a76 | 2011-05-02 22:36:05 -0400 | [diff] [blame] | 62 | for item in directory: |
| 63 | item['title'] = item.get('title', item.get('description', '')) |
Joe Gregorio | c5edb3c | 2011-03-18 10:54:10 -0400 | [diff] [blame] | 64 | path = os.path.join(os.path.dirname(__file__), 'index.html') |
| 65 | self.response.out.write( |
| 66 | template.render( |
| 67 | path, {'directory': directory, |
| 68 | })) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 69 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 70 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 71 | class GadgetHandler(webapp.RequestHandler): |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame^] | 72 | """Handles serving the Google Gadget.""" |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 73 | |
| 74 | def get(self): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 75 | directory = get_directory_doc() |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 76 | for item in directory: |
| 77 | item['title'] = item.get('title', item.get('description', '')) |
| 78 | path = os.path.join(os.path.dirname(__file__), 'gadget.html') |
| 79 | self.response.out.write( |
| 80 | template.render( |
| 81 | path, {'directory': directory, |
| 82 | })) |
| 83 | self.response.headers.add_header('Content-Type', 'application/xml') |
| 84 | |
| 85 | |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame^] | 86 | class EmbedHandler(webapp.RequestHandler): |
| 87 | """Handles serving a front page suitable for embedding.""" |
| 88 | |
| 89 | def get(self): |
| 90 | directory = get_directory_doc() |
| 91 | for item in directory: |
| 92 | item['title'] = item.get('title', item.get('description', '')) |
| 93 | path = os.path.join(os.path.dirname(__file__), 'embed.html') |
| 94 | self.response.out.write( |
| 95 | template.render( |
| 96 | path, {'directory': directory, |
| 97 | })) |
| 98 | |
| 99 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 100 | def _render(resource): |
| 101 | """Use pydoc helpers on an instance to generate the help documentation. |
| 102 | """ |
Joe Gregorio | afc45f2 | 2011-02-20 16:11:28 -0500 | [diff] [blame] | 103 | obj, name = pydoc.resolve(type(resource)) |
| 104 | return pydoc.html.page( |
| 105 | pydoc.describe(obj), pydoc.html.document(obj, name)) |
| 106 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 107 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 108 | class ResourceHandler(webapp.RequestHandler): |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 109 | """Handles serving the PyDoc for a given collection. |
| 110 | """ |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 111 | |
| 112 | def get(self, service_name, version, collection): |
Joe Gregorio | 583d9e4 | 2011-09-16 15:54:15 -0400 | [diff] [blame] | 113 | http = httplib2.Http(memcache) |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 114 | try: |
| 115 | resource = discovery.build(service_name, version, http=http) |
| 116 | except: |
| 117 | return self.error(404) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 118 | # descend the object path |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 119 | if collection: |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 120 | try: |
| 121 | path = collection.split('/') |
| 122 | if path: |
| 123 | for method in path: |
| 124 | resource = getattr(resource, method)() |
| 125 | except: |
| 126 | return self.error(404) |
| 127 | |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 128 | page = _render(resource) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 129 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 130 | collections = [] |
| 131 | for name in dir(resource): |
| 132 | if not "_" in name and callable(getattr(resource, name)) and hasattr( |
| 133 | getattr(resource, name), '__is_resource__'): |
| 134 | collections.append(name) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 135 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 136 | if collection is None: |
| 137 | collection_path = '' |
| 138 | else: |
| 139 | collection_path = collection + '/' |
| 140 | for name in collections: |
| 141 | page = re.sub('strong>(%s)<' % name, |
| 142 | r'strong><a href="/%s/%s/%s">\1</a><' % ( |
| 143 | service_name, version, collection_path + name), page) |
Joe Gregorio | 20cfcda | 2010-10-26 11:58:08 -0400 | [diff] [blame] | 144 | |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 145 | # TODO(jcgregorio) breadcrumbs |
| 146 | # TODO(jcgregorio) sample code? |
| 147 | page = re.sub('<p>', r'<a href="/">Home</a><p>', page, 1) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 148 | self.response.out.write(page) |
| 149 | |
Joe Gregorio | 140062f | 2010-09-21 16:12:41 -0400 | [diff] [blame] | 150 | |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 151 | def main(): |
| 152 | application = webapp.WSGIApplication( |
| 153 | [ |
| 154 | (r'/', MainHandler), |
Joe Gregorio | 9c8588a | 2011-07-01 12:01:36 -0400 | [diff] [blame] | 155 | (r'/_gadget/', GadgetHandler), |
Joe Gregorio | b4169b5 | 2012-02-14 14:42:01 -0500 | [diff] [blame^] | 156 | (r'/_embed/', EmbedHandler), |
Joe Gregorio | 0fb19e1 | 2011-02-22 07:53:24 -0500 | [diff] [blame] | 157 | (r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler), |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 158 | ], |
Joe Gregorio | 985e540 | 2011-12-08 12:21:14 -0500 | [diff] [blame] | 159 | debug=False) |
Joe Gregorio | c204b64 | 2010-09-21 12:01:23 -0400 | [diff] [blame] | 160 | util.run_wsgi_app(application) |
| 161 | |
| 162 | |
| 163 | if __name__ == '__main__': |
| 164 | main() |