2 sphinxcontrib.autohttp.flask
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 The sphinx.ext.autodoc-style HTTP API reference builder (from Flask)
6 for sphinxcontrib.httpdomain.
8 :copyright: Copyright 2011 by Hong Minhee
9 :license: BSD, see LICENSE
for details.
17from docutils import nodes
18from docutils.parsers.rst import directives
19from docutils.statemachine import ViewList
21from sphinx.util import force_decode
22from sphinx.util.compat import Directive
23from sphinx.util.nodes import nested_parse_with_titles
24from sphinx.util.docstrings import prepare_docstring
25from sphinx.pycode import ModuleAnalyzer
27from sphinxcontrib import httpdomain
28from sphinxcontrib.autohttp.common import http_directive, import_object
31def translate_werkzeug_rule(rule):
32 from werkzeug.routing
import parse_rule
34 for conv, arg, var
in parse_rule(rule):
49 for rule
in app.url_map.iter_rules(endpoint):
51 six.text_type(next(app.url_map.iter_rules(rule.endpoint))),
54 if url_with_endpoint
not in endpoints:
55 endpoints.append(url_with_endpoint)
58 endpoints = [e
for _, e
in endpoints]
59 for endpoint
in endpoints:
61 for rule
in app.url_map.iter_rules(endpoint):
62 methods = rule.methods.difference([
'OPTIONS',
'HEAD'])
64 for method
in methods:
65 if method
in methodrules:
66 methodrules[method].append(path)
68 methodrules[method] = [path]
69 for method, paths
in methodrules.items():
70 yield method, paths, endpoint
74 rcomp = re.compile(
"^\s*.. :quickref:\s*(?P<quick>.*)$")
75 method = method.lower().strip()
76 if isinstance(content, six.string_types):
77 content = content.splitlines()
82 qref = rcomp.match(line)
84 quickref = qref.group(
"quick")
85 parts = quickref.split(
";",1)
95 row[
'operation'] =
' - `%s %s <#%s-%s>`_' % (method.upper(), path, method.lower(), ref)
96 row[
'description'] = description
100class AutoflaskBase(Directive):
103 required_arguments = 1
104 option_spec = {
'endpoints': directives.unchanged,
105 'blueprints': directives.unchanged,
106 'modules': directives.unchanged,
107 'order': directives.unchanged,
108 'undoc-endpoints': directives.unchanged,
109 'undoc-blueprints': directives.unchanged,
110 'undoc-modules': directives.unchanged,
111 'undoc-static': directives.unchanged,
112 'include-empty-docstring': directives.unchanged}
116 endpoints = self.options.
get(
'endpoints',
None)
119 return re.split(
r'\s*,\s*', endpoints)
123 undoc_endpoints = self.options.
get(
'undoc-endpoints',
None)
124 if not undoc_endpoints:
126 return frozenset(re.split(
r'\s*,\s*', undoc_endpoints))
130 blueprints = self.options.
get(
'blueprints',
None)
133 return frozenset(re.split(
r'\s*,\s*', blueprints))
137 undoc_blueprints = self.options.
get(
'undoc-blueprints',
None)
138 if not undoc_blueprints:
140 return frozenset(re.split(
r'\s*,\s*', undoc_blueprints))
144 modules = self.options.
get(
'modules',
None)
147 return frozenset(re.split(
r'\s*,\s*', modules))
151 undoc_modules = self.options.
get(
'undoc-modules',
None)
152 if not undoc_modules:
154 return frozenset(re.split(
r'\s*,\s*', undoc_modules))
158 order = self.options.
get(
'order',
None)
159 if order
not in (
None,
'path'):
160 raise ValueError(
'Invalid value for :order:')
170 for method, paths, endpoint
in routes:
172 blueprint, _, endpoint_internal = endpoint.rpartition(
'.')
183 static_url_path = app.static_url_path
184 except AttributeError:
185 static_url_path = app.static_path
186 if (
'undoc-static' in self.options
and endpoint ==
'static' and
187 static_url_path +
'/(path:filename)' in paths):
189 view = app.view_functions[endpoint]
197 docstring = view.__doc__
or ''
198 if hasattr(view,
'view_class'):
199 meth_func = getattr(view.view_class, method.lower(),
None)
200 if meth_func
and meth_func.__doc__:
201 docstring = meth_func.__doc__
202 if not isinstance(docstring, six.text_type):
203 analyzer = ModuleAnalyzer.for_module(view.__module__)
204 docstring = force_decode(docstring, analyzer.encoding)
206 if not docstring
and 'include-empty-docstring' not in self.options:
208 docstring = prepare_docstring(docstring)
def undoc_endpoints(self)
def undoc_blueprints(self)
def make_rst(self, qref=False)
static int replace
Replace DID Document Flag.
static int get
Get DID Documement for DID Flag.
def http_directive(method, path, content)
def import_object(import_name)
def translate_werkzeug_rule(rule)
def get_routes(app, endpoint=None, order=None)
def quickref_directive(method, path, content)