fix jinja rendering of nested structures

This commit is contained in:
Xavier Barbosa 2013-10-24 11:10:13 +02:00
parent c59b31888b
commit 1309ef9887
2 changed files with 31 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import yaml
import salt
import salt.fileclient
from salt.utils.odict import OrderedDict
from salt._compat import string_types
log = logging.getLogger(__name__)
@ -129,7 +130,12 @@ class PrintableDict(OrderedDict):
def __str__(self):
output = []
for key, value in self.items():
output.append('{0!r}: {1!r}'.format(str(key), str(value)))
if isinstance(value, string_types):
# keeps quotes around strings
output.append('{0!r}: {1!r}'.format(key, value))
else:
# let default output
output.append('{0!r}: {1!s}'.format(key, value))
return '{' + ', '.join(output) + '}'
def __repr__(self): # pylint: disable=W0221

View File

@ -22,6 +22,7 @@ from salt.utils.templates import (
render_jinja_tmpl,
get_template_context
)
from salt.utils.odict import OrderedDict
# Import 3rd party libs
import yaml
@ -351,7 +352,6 @@ class TestCustomExtensions(TestCase):
with self.assertRaises(exceptions.TemplateRuntimeError):
env.from_string('{% load_json as document %}{foo, bar: it works}{% endload %}').render()
def test_load_json(self):
env = Environment(extensions=[SerializerExtension])
rendered = env.from_string('{% set document = \'{"foo": "it works"}\'|load_json %}'
@ -437,6 +437,29 @@ class TestCustomExtensions(TestCase):
rendered = env.get_template('main6').render().strip()
self.assertEqual(rendered, u"it works")
def test_nested_structures(self):
env = Environment(extensions=[SerializerExtension])
rendered = env.from_string('{{ data }}').render(data="foo")
self.assertEquals(rendered, u"foo")
rendered = env.from_string('{{ data }}').render(data=OrderedDict(
foo=OrderedDict(
bar='baz',
qux=42,
)
))
self.assertEquals(rendered, u"{'foo': {'bar': 'baz', 'qux': 42}}")
rendered = env.from_string('{{ data }}').render(data=[
OrderedDict(
foo='bar',
),
OrderedDict(
baz=42,
)
])
self.assertEquals(rendered, u"[{'foo': 'bar'}, {'baz': 2}]")
# def test_print(self):
# env = Environment(extensions=[SerializerExtension])
# source = '{% import_yaml "toto.foo" as docu %}'