Skip to content

Commit 0fb19e1

Browse files
committed
Simplify api-python-client-doc and add Home link back in.
1 parent afc45f2 commit 0fb19e1

File tree

2 files changed

+27
-72
lines changed

2 files changed

+27
-72
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
pep8:
2-
find apiclient samples -name "*.py" | xargs pep8 --ignore=E111,E202
2+
find apiclient samples -name "*.py" | xargs pep8 --ignore=E111,E202
33

44
test:
5-
python runtests.py tests
5+
python runtests.py tests
66

77
.PHONY: docs
88
docs:

samples/api-python-client-doc/main.py

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,6 @@
2727
from google.appengine.ext import webapp
2828
from google.appengine.ext.webapp import util
2929

30-
# Replicate render_doc here from pydoc.py as it isn't available in Python 2.5
31-
32-
33-
class _OldStyleClass:
34-
pass
35-
36-
37-
def render_doc(thing, title='Python Library Documentation: %s', forceload=0):
38-
"""Render text documentation, given an object or a path to an object."""
39-
object, name = pydoc.resolve(thing, forceload)
40-
desc = pydoc.describe(object)
41-
module = inspect.getmodule(object)
42-
if name and '.' in name:
43-
desc += ' in ' + name[:name.rfind('.')]
44-
elif module and module is not object:
45-
desc += ' in module ' + module.__name__
46-
if type(object) is type(_OldStyleClass()):
47-
# If the passed object is an instance of an old-style class,
48-
# document its available methods instead of its value.
49-
object = object.__class__
50-
elif not (inspect.ismodule(object) or
51-
inspect.isclass(object) or
52-
inspect.isroutine(object) or
53-
inspect.isgetsetdescriptor(object) or
54-
inspect.ismemberdescriptor(object) or
55-
isinstance(object, property)):
56-
# If the passed object is a piece of data or an instance,
57-
# document its available methods instead of its value.
58-
object = type(object)
59-
desc += ' object'
60-
return title % desc + '\n\n' + pydoc.text.document(object, name)
61-
6230

6331
class MainHandler(webapp.RequestHandler):
6432

@@ -84,58 +52,45 @@ def render(resource):
8452
return pydoc.html.page(
8553
pydoc.describe(obj), pydoc.html.document(obj, name))
8654

87-
class ServiceHandler(webapp.RequestHandler):
8855

89-
def get(self, service_name, version):
90-
service = build(service_name, version)
91-
page = render(service)
56+
class ResourceHandler(webapp.RequestHandler):
57+
58+
def get(self, service_name, version, collection):
59+
resource = build(service_name, version)
60+
# descend the object path
61+
if collection:
62+
path = collection.split('/')
63+
if path:
64+
for method in path:
65+
resource = getattr(resource, method)()
66+
page = render(resource)
9267

9368
collections = []
94-
for name in dir(service):
95-
if not "_" in name and callable(getattr(service, name)) and hasattr(
96-
getattr(service, name), '__is_resource__'):
69+
for name in dir(resource):
70+
if not "_" in name and callable(getattr(resource, name)) and hasattr(
71+
getattr(resource, name), '__is_resource__'):
9772
collections.append(name)
9873

74+
if collection is None:
75+
collection_path = ''
76+
else:
77+
collection_path = collection + '/'
9978
for name in collections:
100-
page = re.sub('strong>(%s)<' % name, r'strong><a href="/%s/%s/%s">\1</a><' % (
101-
service_name, version, name), page)
102-
103-
self.response.out.write(page)
104-
105-
106-
class CollectionHandler(webapp.RequestHandler):
107-
108-
def get(self, service_name, version, collection):
109-
service = build(service_name, version)
110-
# descend the object path
111-
path = collection.split("/")
112-
if path:
113-
for method in path[:-1]:
114-
service = getattr(service, method)()
115-
method = getattr(service, path[-1])
116-
obj = method()
117-
page = render(obj)
118-
119-
if hasattr(method, '__is_resource__'):
120-
collections = []
121-
for name in dir(obj):
122-
if not "_" in name and callable(getattr(obj, name)) and hasattr(
123-
getattr(obj, name), '__is_resource__'):
124-
collections.append(name)
125-
126-
for name in collections:
127-
page = re.sub('strong>(%s)<' % name, r'strong><a href="/%s/%s/%s">\1</a><' % (
128-
service_name, version, collection + "/" + name), page)
79+
page = re.sub('strong>(%s)<' % name,
80+
r'strong><a href="/%s/%s/%s">\1</a><' % (
81+
service_name, version, collection_path + name), page)
12982

83+
# TODO(jcgregorio) breadcrumbs
84+
# TODO(jcgregorio) sample code?
85+
page = re.sub('<p>', r'<a href="/">Home</a><p>', page, 1)
13086
self.response.out.write(page)
13187

13288

13389
def main():
13490
application = webapp.WSGIApplication(
13591
[
13692
(r'/', MainHandler),
137-
(r'/([^\/]*)/([^\/]*)', ServiceHandler),
138-
(r'/([^\/]*)/([^\/]*)/(.*)', CollectionHandler),
93+
(r'/([^\/]*)/([^\/]*)(?:/(.*))?', ResourceHandler),
13994
],
14095
debug=True)
14196
util.run_wsgi_app(application)

0 commit comments

Comments
 (0)