autoapisummary directive inherits from autosummary for future stability

pull/236/head
Ashley Whetter 4 years ago
parent 8d841664ed
commit 8640355f3d

@ -18,6 +18,10 @@ Bug Fixes
* Fixed incorrect formatting of properties on generated method directives.
* Fixed every toctree entry getting added as a new list.
Trivial/Internal Changes
^^^^^^^^^^^^^^^^^^^^^^^^
* autoapisummary directive inherits from autosummary for future stability.
v1.4.0 (2020-06-07)
-------------------

@ -1,117 +1,35 @@
"""AutoAPI directives"""
import posixpath
import re
from docutils.parsers.rst import Directive, directives
from docutils.statemachine import ViewList
from docutils.parsers.rst import Directive
from docutils import nodes
from sphinx import addnodes
import sphinx.ext.autosummary
from sphinx.ext.autosummary import Autosummary
from sphinx.util.nodes import nested_parse_with_titles
from sphinx.util.rst import escape
class AutoapiSummary(Directive): # pylint: disable=too-few-public-methods
"""A version of autosummary that uses static analysis."""
required_arguments = 0
optional_arguments = 0
final_argument_whitespace = False
has_content = True
option_spec = {
"toctree": directives.unchanged,
"nosignatures": directives.flag,
"template": directives.unchanged,
}
from .mappers.python.objects import PythonFunction
def _get_names(self):
"""Get the names of the objects to include in the table.
:returns: The names of the objects to include.
:rtype: generator(str)
"""
for line in self.content:
line = line.strip()
if line and re.search("^[a-zA-Z0-9]", line):
yield line
class AutoapiSummary(Autosummary): # pylint: disable=too-few-public-methods
"""A version of autosummary that uses static analysis."""
def run(self):
def get_items(self, names):
items = []
env = self.state.document.settings.env
mapper = env.autoapi_mapper
objects = [mapper.all_objects[name] for name in self._get_names()]
nodes_ = self._get_table(objects)
if "toctree" in self.options:
dirname = posixpath.dirname(env.docname)
tree_prefix = self.options["toctree"].strip()
docnames = []
for obj in objects:
docname = posixpath.join(tree_prefix, obj.name)
docname = posixpath.normpath(posixpath.join(dirname, docname))
if docname not in env.found_docs:
self.reporter.warning(
"toctree references unknown document {}".format(docname)
)
docnames.append(docname)
tocnode = addnodes.toctree()
tocnode["includefiles"] = docnames
tocnode["entries"] = [(None, docn) for docn in docnames]
tocnode["maxdepth"] = -1
tocnode["glob"] = None
tocnode = sphinx.ext.autosummary.autosummary_toc("", "", tocnode)
nodes_.append(tocnode)
return nodes_
def _get_row(self, obj):
template = ":{}:`{} <{}>`\\ {}"
if "nosignatures" in self.options:
template = ":{}:`{} <{}>`"
col1 = template.format(
"obj", obj.short_name, obj.name, escape("({})".format(obj.args))
)
col2 = obj.summary
row = nodes.row("")
for text in (col1, col2):
node = nodes.paragraph("")
view_list = ViewList()
view_list.append(text, "<autosummary>")
self.state.nested_parse(view_list, 0, node)
try:
if isinstance(node[0], nodes.paragraph):
node = node[0]
except IndexError:
pass
row.append(nodes.entry("", node))
return row
def _get_table(self, objects):
table_spec = addnodes.tabular_col_spec()
table_spec["spec"] = r"p{0.5\linewidth}p{0.5\linewidth}"
table = sphinx.ext.autosummary.autosummary_table("")
real_table = nodes.table("", classes=["longtable"])
table.append(real_table)
group = nodes.tgroup("", cols=2)
real_table.append(group)
group.append(nodes.colspec("", colwidth=10))
group.append(nodes.colspec("", colwidth=90))
body = nodes.tbody("")
group.append(body)
for name in names:
obj = mapper.all_objects[name]
if isinstance(obj, PythonFunction):
sig = "({})".format(obj.args)
if obj.return_annotation is not None:
sig += " -> {}".format(obj.return_annotation)
else:
sig = ""
for obj in objects:
body.append(self._get_row(obj))
item = (obj.short_name, sig, obj.summary, obj.id)
items.append(item)
return [table_spec, table]
return items
class NestedParse(Directive): # pylint: disable=too-few-public-methods

Loading…
Cancel
Save