Skip to content

Instantly share code, notes, and snippets.

@p2
Created March 5, 2014 22:32
Show Gist options
  • Save p2/9378064 to your computer and use it in GitHub Desktop.
Save p2/9378064 to your computer and use it in GitHub Desktop.
Patch for autoflask to sort docstrings by path and group by endpoint/method
diff --git a/httpdomain/sphinxcontrib/autohttp/flask.py b/httpdomain/sphinxcontrib/autohttp/flask.py
--- a/httpdomain/sphinxcontrib/autohttp/flask.py
+++ b/httpdomain/sphinxcontrib/autohttp/flask.py
@@ -93,7 +93,15 @@
def make_rst(self):
app = import_object(self.arguments[0])
- for method, path, endpoint in get_routes(app):
+
+ # order by paths, remember last path per endpoint
+ routes = sorted(get_routes(app), key=lambda x: x[1])
+ show_docstring_for_paths = {}
+ for method, path, endpoint in routes:
+ show_docstring_for_paths[endpoint] = path
+
+ # loop all routes to create docstrings
+ for method, path, endpoint in routes:
try:
blueprint, endpoint_internal = endpoint.split('.')
if self.blueprints and blueprint not in self.blueprints:
@@ -107,6 +115,8 @@
continue
if endpoint in self.undoc_endpoints:
continue
+
+ # skip static if desired
try:
static_url_path = app.static_url_path # Flask 0.7 or higher
except AttributeError:
@@ -114,18 +124,23 @@
if ('undoc-static' in self.options and endpoint == 'static' and
path == static_url_path + '/(path:filename)'):
continue
- view = app.view_functions[endpoint]
- docstring = view.__doc__ or ''
- if hasattr(view, 'view_class'):
- meth_func = getattr(view.view_class, method.lower(), None)
- if meth_func and meth_func.__doc__:
- docstring = meth_func.__doc__
- if not isinstance(docstring, six.text_type):
- analyzer = ModuleAnalyzer.for_module(view.__module__)
- docstring = force_decode(docstring, analyzer.encoding)
-
- if not docstring and 'include-empty-docstring' not in self.options:
- continue
+
+ # get full docstring for select paths
+ docstring = ''
+ if path == show_docstring_for_paths[endpoint]:
+ view = app.view_functions[endpoint]
+ docstring = view.__doc__ or ''
+ if hasattr(view, 'view_class'):
+ meth_func = getattr(view.view_class, method.lower(), None)
+ if meth_func and meth_func.__doc__:
+ docstring = meth_func.__doc__
+ if not isinstance(docstring, six.text_type):
+ analyzer = ModuleAnalyzer.for_module(view.__module__)
+ docstring = force_decode(docstring, analyzer.encoding)
+
+ if not docstring and 'include-empty-docstring' not in self.options:
+ continue
+
docstring = prepare_docstring(docstring)
for line in http_directive(method, path, docstring):
yield line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment