summaryrefslogtreecommitdiff
path: root/_exts/httpdomain/autohttp
diff options
context:
space:
mode:
Diffstat (limited to '_exts/httpdomain/autohttp')
-rw-r--r--_exts/httpdomain/autohttp/__init__.py11
-rw-r--r--_exts/httpdomain/autohttp/bottle.py114
-rw-r--r--_exts/httpdomain/autohttp/common.py36
-rw-r--r--_exts/httpdomain/autohttp/flask.py48
-rw-r--r--_exts/httpdomain/autohttp/flask_base.py215
-rw-r--r--_exts/httpdomain/autohttp/flaskqref.py80
-rw-r--r--_exts/httpdomain/autohttp/tornado.py128
7 files changed, 632 insertions, 0 deletions
diff --git a/_exts/httpdomain/autohttp/__init__.py b/_exts/httpdomain/autohttp/__init__.py
new file mode 100644
index 00000000..95372d4c
--- /dev/null
+++ b/_exts/httpdomain/autohttp/__init__.py
@@ -0,0 +1,11 @@
+"""
+ sphinxcontrib.autohttp
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ The sphinx.ext.autodoc-style HTTP API reference builder
+ for sphinxcontrib.httpdomain.
+
+ :copyright: Copyright 2011 by Hong Minhee
+ :license: BSD, see LICENSE for details.
+
+"""
diff --git a/_exts/httpdomain/autohttp/bottle.py b/_exts/httpdomain/autohttp/bottle.py
new file mode 100644
index 00000000..d8c18597
--- /dev/null
+++ b/_exts/httpdomain/autohttp/bottle.py
@@ -0,0 +1,114 @@
+"""
+ sphinxcontrib.autohttp.bottle
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ The sphinx.ext.autodoc-style HTTP API reference builder (from Bottle)
+ for sphinxcontrib.httpdomain.
+
+ :copyright: Copyright 2012 by Jameel Al-Aziz
+ :license: BSD, see LICENSE for details.
+
+"""
+
+import re
+import six
+
+from docutils import nodes
+from docutils.parsers.rst import directives
+from docutils.statemachine import ViewList
+
+from sphinx.util import force_decode
+from sphinx.util.compat import Directive
+from sphinx.util.nodes import nested_parse_with_titles
+from sphinx.util.docstrings import prepare_docstring
+from sphinx.pycode import ModuleAnalyzer
+
+from sphinxcontrib import httpdomain
+from sphinxcontrib.autohttp.common import http_directive, import_object
+
+
+def translate_bottle_rule(app, rule):
+ buf = six.StringIO()
+ if hasattr(app.router, "parse_rule"):
+ iterator = app.router.parse_rule(rule) # bottle 0.11
+ else:
+ iterator = app.router._itertokens(rule) # bottle 0.12
+ for name, filter, conf in iterator:
+ if filter:
+ buf.write('(')
+ buf.write(name)
+ if (filter != app.router.default_filter and filter != 'default')\
+ or conf:
+ buf.write(':')
+ buf.write(filter)
+ if conf:
+ buf.write(':')
+ buf.write(conf)
+ buf.write(')')
+ else:
+ buf.write(name)
+ return buf.getvalue()
+
+
+def get_routes(app):
+ for route in app.routes:
+ path = translate_bottle_rule(app, route.rule)
+ yield route.method, path, route
+
+
+class AutobottleDirective(Directive):
+
+ has_content = True
+ required_arguments = 1
+ option_spec = {'endpoints': directives.unchanged,
+ 'undoc-endpoints': directives.unchanged,
+ 'include-empty-docstring': directives.unchanged}
+
+ @property
+ def endpoints(self):
+ endpoints = self.options.get('endpoints', None)
+ if not endpoints:
+ return None
+ return frozenset(re.split(r'\s*,\s*', endpoints))
+
+ @property
+ def undoc_endpoints(self):
+ undoc_endpoints = self.options.get('undoc-endpoints', None)
+ if not undoc_endpoints:
+ return frozenset()
+ return frozenset(re.split(r'\s*,\s*', undoc_endpoints))
+
+ def make_rst(self):
+ app = import_object(self.arguments[0])
+ for method, path, target in get_routes(app):
+ endpoint = target.name or target.callback.__name__
+ if self.endpoints and endpoint not in self.endpoints:
+ continue
+ if endpoint in self.undoc_endpoints:
+ continue
+ view = target.callback
+ docstring = view.__doc__ or ''
+ 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
+
+ def run(self):
+ node = nodes.section()
+ node.document = self.state.document
+ result = ViewList()
+ for line in self.make_rst():
+ result.append(line, '<autobottle>')
+ nested_parse_with_titles(self.state, result, node)
+ return node.children
+
+
+def setup(app):
+ if 'http' not in app.domains:
+ httpdomain.setup(app)
+ app.add_directive('autobottle', AutobottleDirective)
+
diff --git a/_exts/httpdomain/autohttp/common.py b/_exts/httpdomain/autohttp/common.py
new file mode 100644
index 00000000..199e2972
--- /dev/null
+++ b/_exts/httpdomain/autohttp/common.py
@@ -0,0 +1,36 @@
+"""
+ sphinxcontrib.autohttp.common
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ The common functions for web framework reflection.
+
+ :copyright: Copyright 2011 by Hong Minhee
+ :license: BSD, see LICENSE for details.
+
+"""
+import six
+from six.moves import builtins
+from six.moves import reduce
+
+def import_object(import_name):
+ module_name, expr = import_name.split(':', 1)
+ mod = __import__(module_name)
+ mod = reduce(getattr, module_name.split('.')[1:], mod)
+ globals = builtins
+ if not isinstance(globals, dict):
+ globals = globals.__dict__
+ return eval(expr, globals, mod.__dict__)
+
+
+def http_directive(method, path, content):
+ method = method.lower().strip()
+ if isinstance(content, six.string_types):
+ content = content.splitlines()
+ yield ''
+ paths = [path] if isinstance(path, six.string_types) else path
+ for path in paths:
+ yield '.. http:{method}:: {path}'.format(**locals())
+ yield ''
+ for line in content:
+ yield ' ' + line
+ yield ''
diff --git a/_exts/httpdomain/autohttp/flask.py b/_exts/httpdomain/autohttp/flask.py
new file mode 100644
index 00000000..4bd5232e
--- /dev/null
+++ b/_exts/httpdomain/autohttp/flask.py
@@ -0,0 +1,48 @@
+"""
+ sphinxcontrib.autohttp.flask
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ The sphinx.ext.autodoc-style HTTP API reference builder (from Flask)
+ for sphinxcontrib.httpdomain.
+
+ :copyright: Copyright 2011 by Hong Minhee
+ :license: BSD, see LICENSE for details.
+
+"""
+from __future__ import absolute_import
+
+import re
+import itertools
+import six
+
+from docutils import nodes
+from docutils.parsers.rst import directives
+from docutils.statemachine import ViewList
+
+from sphinx.util import force_decode
+from sphinx.util.compat import Directive
+from sphinx.util.nodes import nested_parse_with_titles
+from sphinx.util.docstrings import prepare_docstring
+from sphinx.pycode import ModuleAnalyzer
+
+from sphinxcontrib import httpdomain
+from sphinxcontrib.autohttp.common import http_directive, import_object
+
+from .flask_base import AutoflaskBase
+
+class AutoflaskDirective(AutoflaskBase):
+
+ def run(self):
+ node = nodes.section()
+ node.document = self.state.document
+ result = ViewList()
+ for line in self.make_rst():
+ result.append(line, '<autoflask>')
+ nested_parse_with_titles(self.state, result, node)
+ return node.children
+
+
+def setup(app):
+ if 'http' not in app.domains:
+ httpdomain.setup(app)
+ app.add_directive('autoflask', AutoflaskDirective)
diff --git a/_exts/httpdomain/autohttp/flask_base.py b/_exts/httpdomain/autohttp/flask_base.py
new file mode 100644
index 00000000..50454fe2
--- /dev/null
+++ b/_exts/httpdomain/autohttp/flask_base.py
@@ -0,0 +1,215 @@
+"""
+ sphinxcontrib.autohttp.flask
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ The sphinx.ext.autodoc-style HTTP API reference builder (from Flask)
+ for sphinxcontrib.httpdomain.
+
+ :copyright: Copyright 2011 by Hong Minhee
+ :license: BSD, see LICENSE for details.
+
+"""
+
+import re
+import itertools
+import six
+
+from docutils import nodes
+from docutils.parsers.rst import directives
+from docutils.statemachine import ViewList
+
+from sphinx.util import force_decode
+from sphinx.util.compat import Directive
+from sphinx.util.nodes import nested_parse_with_titles
+from sphinx.util.docstrings import prepare_docstring
+from sphinx.pycode import ModuleAnalyzer
+
+from sphinxcontrib import httpdomain
+from sphinxcontrib.autohttp.common import http_directive, import_object
+
+
+def translate_werkzeug_rule(rule):
+ from werkzeug.routing import parse_rule
+ buf = six.StringIO()
+ for conv, arg, var in parse_rule(rule):
+ if conv:
+ buf.write('(')
+ if conv != 'default':
+ buf.write(conv)
+ buf.write(':')
+ buf.write(var)
+ buf.write(')')
+ else:
+ buf.write(var)
+ return buf.getvalue()
+
+
+def get_routes(app, endpoint=None, order=None):
+ endpoints = []
+ for rule in app.url_map.iter_rules(endpoint):
+ url_with_endpoint = (
+ six.text_type(next(app.url_map.iter_rules(rule.endpoint))),
+ rule.endpoint
+ )
+ if url_with_endpoint not in endpoints:
+ endpoints.append(url_with_endpoint)
+ if order == 'path':
+ endpoints.sort()
+ endpoints = [e for _, e in endpoints]
+ for endpoint in endpoints:
+ methodrules = {}
+ for rule in app.url_map.iter_rules(endpoint):
+ methods = rule.methods.difference(['OPTIONS', 'HEAD'])
+ path = translate_werkzeug_rule(rule.rule)
+ for method in methods:
+ if method in methodrules:
+ methodrules[method].append(path)
+ else:
+ methodrules[method] = [path]
+ for method, paths in methodrules.items():
+ yield method, paths, endpoint
+
+
+def quickref_directive(method, path, content):
+ rcomp = re.compile("^\s*.. :quickref:\s*(?P<quick>.*)$")
+ method = method.lower().strip()
+ if isinstance(content, six.string_types):
+ content = content.splitlines()
+ description=""
+ name=""
+ ref = path.replace("<","(").replace(">",")").replace("/","-").replace(":","-")
+ for line in content:
+ qref = rcomp.match(line)
+ if qref:
+ quickref = qref.group("quick")
+ parts = quickref.split(";",1)
+ if len(parts)>1:
+ name = parts[0]
+ description= parts[1]
+ else:
+ description= quickref
+ break
+
+ row ={}
+ row['name'] = name
+ row['operation'] = ' - `%s %s <#%s-%s>`_' % (method.upper(), path, method.lower(), ref)
+ row['description'] = description
+
+ return row
+
+class AutoflaskBase(Directive):
+
+ has_content = True
+ required_arguments = 1
+ option_spec = {'endpoints': directives.unchanged,
+ 'blueprints': directives.unchanged,
+ 'modules': directives.unchanged,
+ 'order': directives.unchanged,
+ 'undoc-endpoints': directives.unchanged,
+ 'undoc-blueprints': directives.unchanged,
+ 'undoc-modules': directives.unchanged,
+ 'undoc-static': directives.unchanged,
+ 'include-empty-docstring': directives.unchanged}
+
+ @property
+ def endpoints(self):
+ endpoints = self.options.get('endpoints', None)
+ if not endpoints:
+ return None
+ return re.split(r'\s*,\s*', endpoints)
+
+ @property
+ def undoc_endpoints(self):
+ undoc_endpoints = self.options.get('undoc-endpoints', None)
+ if not undoc_endpoints:
+ return frozenset()
+ return frozenset(re.split(r'\s*,\s*', undoc_endpoints))
+
+ @property
+ def blueprints(self):
+ blueprints = self.options.get('blueprints', None)
+ if not blueprints:
+ return None
+ return frozenset(re.split(r'\s*,\s*', blueprints))
+
+ @property
+ def undoc_blueprints(self):
+ undoc_blueprints = self.options.get('undoc-blueprints', None)
+ if not undoc_blueprints:
+ return frozenset()
+ return frozenset(re.split(r'\s*,\s*', undoc_blueprints))
+
+ @property
+ def modules(self):
+ modules = self.options.get('modules', None)
+ if not modules:
+ return frozenset()
+ return frozenset(re.split(r'\s*,\s*', modules))
+
+ @property
+ def undoc_modules(self):
+ undoc_modules = self.options.get('undoc-modules', None)
+ if not undoc_modules:
+ return frozenset()
+ return frozenset(re.split(r'\s*,\s*', undoc_modules))
+
+ @property
+ def order(self):
+ order = self.options.get('order', None)
+ if order not in (None, 'path'):
+ raise ValueError('Invalid value for :order:')
+ return order
+
+ def make_rst(self, qref=False):
+ app = import_object(self.arguments[0])
+ if self.endpoints:
+ routes = itertools.chain(*[get_routes(app, endpoint, self.order)
+ for endpoint in self.endpoints])
+ else:
+ routes = get_routes(app, order=self.order)
+ for method, paths, endpoint in routes:
+ try:
+ blueprint, _, endpoint_internal = endpoint.rpartition('.')
+ if self.blueprints and blueprint not in self.blueprints:
+ continue
+ if blueprint in self.undoc_blueprints:
+ continue
+ except ValueError:
+ pass # endpoint is not within a blueprint
+
+ if endpoint in self.undoc_endpoints:
+ continue
+ try:
+ static_url_path = app.static_url_path # Flask 0.7 or higher
+ except AttributeError:
+ static_url_path = app.static_path # Flask 0.6 or under
+ if ('undoc-static' in self.options and endpoint == 'static' and
+ static_url_path + '/(path:filename)' in paths):
+ continue
+ view = app.view_functions[endpoint]
+
+ if self.modules and view.__module__ not in self.modules:
+ continue
+
+ if self.undoc_modules and view.__module__ in self.modules:
+ continue
+
+ 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)
+ if qref == True:
+ for path in paths:
+ row = quickref_directive(method, path, docstring)
+ yield row
+ else:
+ for line in http_directive(method, paths, docstring):
+ yield line
diff --git a/_exts/httpdomain/autohttp/flaskqref.py b/_exts/httpdomain/autohttp/flaskqref.py
new file mode 100644
index 00000000..c28bb153
--- /dev/null
+++ b/_exts/httpdomain/autohttp/flaskqref.py
@@ -0,0 +1,80 @@
+"""
+ sphinxcontrib.autohttp.flaskqref
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ The sphinx.ext.autodoc-style HTTP API quick reference
+ builder (from Flask)
+ for sphinxcontrib.httpdomain.
+
+ :copyright: Copyright 2011 by Hong Minhee
+ :license: BSD, see LICENSE for details.
+
+"""
+
+from docutils import nodes
+from docutils.statemachine import ViewList
+
+from sphinxcontrib import httpdomain
+from sphinx.util.nodes import nested_parse_with_titles
+
+from .flask import AutoflaskBase
+
+
+class QuickReferenceFlaskDirective(AutoflaskBase):
+
+
+ header = [ '',
+ '.. list-table::',
+ ' :widths: 20 45 35',
+ ' :header-rows: 1',
+ '',
+ ' * - Resource',
+ ' - Operation',
+ ' - Description'
+ ]
+
+ def run(self):
+ node = nodes.section()
+ node.document = self.state.document
+ result = ViewList()
+ for line in QuickReferenceFlaskDirective.header:
+ result.append(line, '<qrefflask>')
+ table={}
+ table_sorted_names=[]
+
+ for table_row in self.make_rst(qref=True):
+ name = table_row['name']
+ if table.get(name) is None:
+ table[name]=[]
+ table[name].append(table_row)
+ if name not in table_sorted_names:
+ table_sorted_names.append(name)
+
+ table_sorted_names.sort()
+
+ for name in table_sorted_names:
+ # Keep table display clean by not repeating duplicate
+ # resource names and descriptions
+ display_name = name
+ previous_description=None
+ for row in table[name]:
+ result.append(' * - %s' % display_name, '<qrefflask>')
+ display_name =""
+ result.append(row['operation'], '<qrefflask>')
+ description = row['description']
+ if previous_description is not None and previous_description == description:
+ description =""
+ else:
+ previous_description = description
+
+ result.append(' - %s' % description, '<qrefflask>')
+
+ result.append('', '<qrefflask>')
+ nested_parse_with_titles(self.state, result, node)
+ return node.children
+
+def setup(app):
+ if 'http' not in app.domains:
+ httpdomain.setup(app)
+ app.add_directive('qrefflask', QuickReferenceFlaskDirective)
+
diff --git a/_exts/httpdomain/autohttp/tornado.py b/_exts/httpdomain/autohttp/tornado.py
new file mode 100644
index 00000000..9a514fee
--- /dev/null
+++ b/_exts/httpdomain/autohttp/tornado.py
@@ -0,0 +1,128 @@
+"""
+ sphinxcontrib.autohttp.tornado
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+ The sphinx.ext.autodoc-style HTTP API reference builder (from Tornado)
+ for sphinxcontrib.httpdomain.
+
+ :copyright: Copyright 2013 by Rodrigo Machado
+ :license: BSD, see LICENSE for details.
+
+"""
+
+import inspect
+import re
+import six
+
+from docutils import nodes
+from docutils.parsers.rst import directives
+from docutils.statemachine import ViewList
+
+from sphinx.util import force_decode
+from sphinx.util.compat import Directive
+from sphinx.util.nodes import nested_parse_with_titles
+from sphinx.util.docstrings import prepare_docstring
+from sphinx.pycode import ModuleAnalyzer
+
+from sphinxcontrib import httpdomain
+from sphinxcontrib.autohttp.common import http_directive, import_object
+
+
+def translate_tornado_rule(app, rule):
+ buf = six.StringIO()
+ for name, filter, conf in app.router.parse_rule(rule):
+ if filter:
+ buf.write('(')
+ buf.write(name)
+ if filter != app.router.default_filter or conf:
+ buf.write(':')
+ buf.write(filter)
+ if conf:
+ buf.write(':')
+ buf.write(conf)
+ buf.write(')')
+ else:
+ buf.write(name)
+ return buf.getvalue()
+
+
+def get_routes(app):
+ for spec in app.handlers[0][1]:
+ handler = spec.handler_class
+ doc_methods = list(handler.SUPPORTED_METHODS)
+ if 'HEAD' in doc_methods:
+ doc_methods.remove('HEAD')
+ if 'OPTIONS' in doc_methods:
+ doc_methods.remove('OPTIONS')
+
+ for method in doc_methods:
+ maybe_method = getattr(handler, method.lower(), None)
+ if (inspect.isfunction(maybe_method) or
+ inspect.ismethod(maybe_method)):
+ yield method.lower(), spec.regex.pattern, handler
+
+
+def normalize_path(path):
+ if path.endswith('$'):
+ path = path[:-1]
+ return path
+
+
+class AutoTornadoDirective(Directive):
+
+ has_content = True
+ required_arguments = 1
+ option_spec = {'endpoints': directives.unchanged,
+ 'undoc-endpoints': directives.unchanged,
+ 'include-empty-docstring': directives.unchanged}
+
+ @property
+ def endpoints(self):
+ endpoints = self.options.get('endpoints', None)
+ if not endpoints:
+ return None
+ return frozenset(re.split(r'\s*,\s*', endpoints))
+
+ @property
+ def undoc_endpoints(self):
+ undoc_endpoints = self.options.get('undoc-endpoints', None)
+ if not undoc_endpoints:
+ return frozenset()
+ return frozenset(re.split(r'\s*,\s*', undoc_endpoints))
+
+ def make_rst(self):
+ app = import_object(self.arguments[0])
+ for method, path, handler in get_routes(app):
+ class_name = handler.__name__
+ method_name = getattr(handler, method).__name__
+ endpoint = '.'.join((class_name, method_name))
+
+ if self.endpoints and endpoint not in self.endpoints:
+ continue
+ if endpoint in self.undoc_endpoints:
+ continue
+
+ docstring = getattr(handler, method).__doc__ or ''
+ #if not isinstance(docstring, unicode):
+ # 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, normalize_path(path), docstring):
+ yield line
+
+ def run(self):
+ node = nodes.section()
+ node.document = self.state.document
+ result = ViewList()
+ for line in self.make_rst():
+ result.append(line, '<autotornado>')
+ nested_parse_with_titles(self.state, result, node)
+ return node.children
+
+
+def setup(app):
+ if 'http' not in app.domains:
+ httpdomain.setup(app)
+ app.add_directive('autotornado', AutoTornadoDirective)