• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2#
3# Copyright 2014 Google Inc. All Rights Reserved.
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"""Sample application for Python documentation of APIs.
18
19This is running live at http://api-python-client-doc.appspot.com where it
20provides a list of APIs and PyDoc documentation for all the generated API
21surfaces as they appear in the google-api-python-client. In addition it also
22provides a Google Gadget.
23"""
24
25__author__ = 'jcgregorio@google.com (Joe Gregorio)'
26
27import httplib2
28import inspect
29import json
30import logging
31import os
32import pydoc
33import re
34
35import describe
36import uritemplate
37
38from googleapiclient import discovery
39from googleapiclient.errors import HttpError
40from google.appengine.api import memcache
41from google.appengine.ext import webapp
42from google.appengine.ext.webapp import template
43from google.appengine.ext.webapp import util
44
45
46DISCOVERY_URI = 'https://www.googleapis.com/discovery/v1/apis?preferred=true'
47
48
49def 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 = json.loads(content)['items']
57  for item in directory:
58    item['title'] = item.get('title', item.get('description', ''))
59    item['safe_version'] = describe.safe_version(item['version'])
60  return directory
61
62
63class MainHandler(webapp.RequestHandler):
64  """Handles serving the main landing page.
65  """
66
67  def get(self):
68    directory = get_directory_doc()
69    path = os.path.join(os.path.dirname(__file__), 'index.html')
70    self.response.out.write(
71        template.render(
72            path, {'directory': directory,
73                   }))
74
75
76class GadgetHandler(webapp.RequestHandler):
77  """Handles serving the Google Gadget."""
78
79  def get(self):
80    directory = get_directory_doc()
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
89class EmbedHandler(webapp.RequestHandler):
90  """Handles serving a front page suitable for embedding."""
91
92  def get(self):
93    directory = get_directory_doc()
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
101class ResourceHandler(webapp.RequestHandler):
102  """Handles serving the PyDoc for a given collection.
103  """
104
105  def get(self, service_name, version, collection):
106
107    return self.redirect('https://google-api-client-libraries.appspot.com/documentation/%s/%s/python/latest/%s_%s.%s.html'
108        % (service_name, version, service_name, version, collection))
109
110
111def main():
112  application = webapp.WSGIApplication(
113      [
114      (r'/', MainHandler),
115      (r'/_gadget/', GadgetHandler),
116      (r'/_embed/', EmbedHandler),
117      (r'/([^_]+)_([^\.]+)(?:\.(.*))?\.html$', ResourceHandler),
118      ],
119      debug=True)
120  util.run_wsgi_app(application)
121
122
123if __name__ == '__main__':
124  main()
125