Made passing of options to templates more sane

pull/199/head
Ashley Whetter 4 years ago
parent 265b4ae562
commit c5d5fb5346

@ -54,7 +54,7 @@ class PythonMapperBase(object):
top_level_object = False
_RENDER_LOG_LEVEL = "VERBOSE"
def __init__(self, obj, jinja_env, app=None, options=None):
def __init__(self, obj, jinja_env, app, options=None):
self.app = app
self.obj = obj
self.options = options
@ -85,19 +85,15 @@ class PythonMapperBase(object):
@property
def rendered(self):
"""Shortcut to render an object in templates."""
return self.render(
include_private_inheritance=(
"private-members" in self.app.config.autoapi_options
),
include_summaries=self.app.config.autoapi_include_summaries,
show_inheritance=("show-inheritance" in self.app.config.autoapi_options),
show_inheritance_diagram=(
"show-inheritance-diagram" in self.app.config.autoapi_options
),
)
return self.render()
def get_context_data(self):
return {"obj": self, "sphinx_version": sphinx.version_info}
return {
"autoapi_options": self.app.config.autoapi_options,
"include_summaries": self.app.config.autoapi_include_summaries,
"obj": self,
"sphinx_version": sphinx.version_info,
}
def __lt__(self, other):
"""Object sorting comparison"""
@ -299,18 +295,7 @@ class SphinxMapperBase(object):
verbosity="INFO",
stringify_func=(lambda x: x[0]),
):
rst = obj.render(
include_private_inheritance=(
"private-members" in self.app.config.autoapi_options
),
include_summaries=self.app.config.autoapi_include_summaries,
show_inheritance=(
"show-inheritance" in self.app.config.autoapi_options
),
show_inheritance_diagram=(
"show-inheritance-diagram" in self.app.config.autoapi_options
),
)
rst = obj.render()
if not rst:
continue

@ -172,7 +172,9 @@ class DotNetSphinxMapper(SphinxMapperBase):
except KeyError:
LOGGER.warning("Unknown type: %s" % data)
else:
obj = cls(data, jinja_env=self.jinja_env, options=options, **kwargs)
obj = cls(
data, jinja_env=self.jinja_env, app=self.app, options=options, **kwargs
)
obj.url_root = self.url_root
# Append child objects
@ -338,7 +340,9 @@ class DotNetPythonMapper(PythonMapperBase):
# Inheritance
# TODO Support more than just a class type here, should support enum/etc
self.inheritance = [
DotNetClass({"uid": name, "name": name}, jinja_env=self.jinja_env)
DotNetClass(
{"uid": name, "name": name}, jinja_env=self.jinja_env, app=self.app
)
for name in obj.get("inheritance", [])
]

@ -95,7 +95,7 @@ class GoSphinxMapper(SphinxMapperBase):
yield obj
else:
# Recurse for children
obj = cls(data, jinja_env=self.jinja_env)
obj = cls(data, jinja_env=self.jinja_env, app=self.app)
for child_type in ["consts", "types", "vars", "funcs", "methods"]:
for child_data in data.get(child_type, []):
obj.children += list(

@ -74,7 +74,7 @@ class JavaScriptSphinxMapper(SphinxMapperBase):
LOGGER.warning("Unknown Type: %s" % data)
else:
# Recurse for children
obj = cls(data, jinja_env=self.jinja_env)
obj = cls(data, jinja_env=self.jinja_env, app=self.app)
if "children" in data:
for child_data in data["children"]:
for child_obj in self.create_class(child_data, options=options):

@ -3,15 +3,15 @@
{% if obj.bases %}
{% if show_inheritance %}
{% if "show-inheritance" in autoapi_options %}
Bases: {% for base in obj.bases %}:class:`{{ base }}`{% if not loop.last %}, {% endif %}{% endfor %}
{% endif %}
{% if show_inheritance_diagram and obj.bases != ["object"] %}
{% if "show-inheritance-diagram" in autoapi_options and obj.bases != ["object"] %}
.. autoapi-inheritance-diagram:: {{ obj.obj["full_name"] }}
:parts: 1
{% if include_private_inheritance %}:private-bases:{% endif %}
{% if "private-members" in autoapi_options %}:private-bases:{% endif %}
{% endif %}
{% endif %}

@ -28,7 +28,10 @@ Context
Every template is given a set context that can be accessed in the templates.
This contains:
* ``autoapi_options``: The value of the :confval:`autoapi_options`
configuration option.
* ``obj``: A Python object derived from :class:`PythonMapperBase`.
* ``sphinx_version``: The contents of :attr:`sphinx.version_info`.
This object has a number of standard attributes you can reliably access per language.

@ -2,6 +2,7 @@
import unittest
import mock
from mock import patch
from autoapi.mappers import dotnet
@ -185,7 +186,7 @@ class DotNetPythonMapperTests(unittest.TestCase):
},
},
}
mapped = dotnet.DotNetPythonMapper(obj, jinja_env=None)
mapped = dotnet.DotNetPythonMapper(obj, app=mock.MagicMock(), jinja_env=None)
self.assertEqual(
mapped.parameters[0],
{"name": "a", "type": "{TUser}", "desc": "Test :any:`TUser`"},

@ -5,6 +5,7 @@
import os
import unittest
from collections import namedtuple
import mock
from jinja2 import Environment, FileSystemLoader
@ -16,118 +17,128 @@ from autoapi.settings import TEMPLATE_DIR
class DotNetObjectTests(unittest.TestCase):
def test_type(self):
"""Test types of some of the objects"""
obj = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "namespace")
self.assertEqual(obj.ref_type, "namespace")
self.assertEqual(obj.ref_directive, "ns")
obj = dotnet.DotNetMethod({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetMethod({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "method")
self.assertEqual(obj.ref_type, "method")
self.assertEqual(obj.ref_directive, "meth")
obj = dotnet.DotNetProperty({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetProperty({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "property")
self.assertEqual(obj.ref_type, "property")
self.assertEqual(obj.ref_directive, "prop")
obj = dotnet.DotNetEnum({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetEnum({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "enum")
self.assertEqual(obj.ref_type, "enumeration")
self.assertEqual(obj.ref_directive, "enum")
obj = dotnet.DotNetStruct({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetStruct({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "struct")
self.assertEqual(obj.ref_type, "structure")
self.assertEqual(obj.ref_directive, "struct")
obj = dotnet.DotNetConstructor({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetConstructor({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "constructor")
self.assertEqual(obj.ref_type, "constructor")
self.assertEqual(obj.ref_directive, "ctor")
obj = dotnet.DotNetInterface({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetInterface({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "interface")
self.assertEqual(obj.ref_type, "interface")
self.assertEqual(obj.ref_directive, "iface")
obj = dotnet.DotNetDelegate({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetDelegate({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "delegate")
self.assertEqual(obj.ref_type, "delegate")
self.assertEqual(obj.ref_directive, "del")
obj = dotnet.DotNetClass({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetClass({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "class")
self.assertEqual(obj.ref_type, "class")
self.assertEqual(obj.ref_directive, "cls")
obj = dotnet.DotNetField({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetField({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "field")
self.assertEqual(obj.ref_type, "field")
self.assertEqual(obj.ref_directive, "field")
obj = dotnet.DotNetEvent({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetEvent({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.type, "event")
self.assertEqual(obj.ref_type, "event")
self.assertEqual(obj.ref_directive, "event")
def test_names(self):
"""Test names of objects"""
obj = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None)
obj = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(obj.name, "Foo.Bar")
self.assertEqual(obj.short_name, "Bar")
obj = dotnet.DotNetNamespace({"id": "Foo.Bar.Something`1"}, jinja_env=None)
obj = dotnet.DotNetNamespace(
{"id": "Foo.Bar.Something`1"}, jinja_env=None, app=None
)
self.assertEqual(obj.name, "Foo.Bar.Something`1")
self.assertEqual(obj.short_name, "Something`1")
def test_namespace_namespace(self):
"""Namespace parent resolution"""
ns = dotnet.DotNetNamespace({"id": "Foo.Bar.Widgets"}, jinja_env=None)
ns = dotnet.DotNetNamespace({"id": "Foo.Bar.Widgets"}, jinja_env=None, app=None)
self.assertEqual(ns.namespace, "Foo.Bar")
ns = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None)
ns = dotnet.DotNetNamespace({"id": "Foo.Bar"}, jinja_env=None, app=None)
self.assertEqual(ns.namespace, "Foo")
ns = dotnet.DotNetNamespace({"id": "Foo"}, jinja_env=None)
ns = dotnet.DotNetNamespace({"id": "Foo"}, jinja_env=None, app=None)
self.assertIsNone(ns.namespace)
def test_class_namespace(self):
"""Class parent resolution"""
cls = dotnet.DotNetClass(
dict(id="Foo.Bar.Widget", type="class"), jinja_env=None
dict(id="Foo.Bar.Widget", type="class"), jinja_env=None, app=None,
)
self.assertEqual(cls.namespace, "Foo.Bar")
cls = dotnet.DotNetClass(dict(id="Foo.Bar", type="class"), jinja_env=None)
cls = dotnet.DotNetClass(
dict(id="Foo.Bar", type="class"), jinja_env=None, app=None
)
self.assertEqual(cls.namespace, "Foo")
cls = dotnet.DotNetClass(dict(id="Foo", type="class"), jinja_env=None)
cls = dotnet.DotNetClass(dict(id="Foo", type="class"), jinja_env=None, app=None)
self.assertIsNone(cls.namespace)
def test_filename(self):
"""Object file name"""
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None)
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None, app=None)
self.assertEqual(cls.pathname, os.path.join("Foo", "Bar", "Widget"))
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget<T>"}, jinja_env=None)
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget<T>"}, jinja_env=None, app=None)
self.assertEqual(cls.pathname, os.path.join("Foo", "Bar", "Widget-T"))
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget<T>(TFoo)"}, jinja_env=None)
cls = dotnet.DotNetClass(
{"id": "Foo.Bar.Widget<T>(TFoo)"}, jinja_env=None, app=None
)
self.assertEqual(cls.pathname, os.path.join("Foo", "Bar", "Widget-T"))
cls = dotnet.DotNetClass({"id": "Foo.Foo-Bar.Widget<T>(TFoo)"}, jinja_env=None)
cls = dotnet.DotNetClass(
{"id": "Foo.Foo-Bar.Widget<T>(TFoo)"}, jinja_env=None, app=None
)
self.assertEqual(cls.pathname, os.path.join("Foo", "FooBar", "Widget-T"))
cls = dotnet.DotNetClass({"id": u"Foo.Bär"}, jinja_env=None)
cls = dotnet.DotNetClass({"id": u"Foo.Bär"}, jinja_env=None, app=None)
self.assertEqual(cls.pathname, os.path.join("Foo", "Bar"))
cls = dotnet.DotNetClass({"id": u"Ащщ.юИфк"}, jinja_env=None)
cls = dotnet.DotNetClass({"id": u"Ащщ.юИфк"}, jinja_env=None, app=None)
self.assertEqual(cls.pathname, os.path.join("Ashchshch", "iuIfk"))
def test_rendered_class_escaping(self):
"""Rendered class escaping"""
jinja_env = Environment(loader=FileSystemLoader([TEMPLATE_DIR]))
cls = dotnet.DotNetClass(
{"id": "Foo.Bar`1", "inheritance": ["Foo.Baz`1"]}, jinja_env=jinja_env
{"id": "Foo.Bar`1", "inheritance": ["Foo.Baz`1"]},
jinja_env=jinja_env,
app=mock.MagicMock(),
)
self.assertIn("* :dn:cls:`Foo.Baz\\`1`\n", cls.render())
def test_include_path(self):
"""Include path"""
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None)
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None, app=None)
self.assertEqual(cls.include_path, "/autoapi/Foo/Bar/Widget/index")
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None)
cls = dotnet.DotNetClass({"id": "Foo.Bar.Widget"}, jinja_env=None, app=None)
cls.url_root = "/autofoo"
self.assertEqual(cls.include_path, "/autofoo/Foo/Bar/Widget/index")

Loading…
Cancel
Save