mirror of
https://github.com/saltstack/salt.git
synced 2025-04-17 10:10:20 +00:00
fix yaml output
In b9be2de
, OrderedDict was replaced with HashableOrderedDict.
Add logic to yamldumper to handle the new type.
This commit is contained in:
parent
9922b1a145
commit
caf62f97bd
5 changed files with 91 additions and 7 deletions
1
changelog/66783.fixed.md
Normal file
1
changelog/66783.fixed.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
fix yaml output
|
|
@ -51,7 +51,7 @@ from salt.exceptions import CommandExecutionError, SaltRenderError, SaltReqTimeo
|
||||||
from salt.serializers.msgpack import deserialize as msgpack_deserialize
|
from salt.serializers.msgpack import deserialize as msgpack_deserialize
|
||||||
from salt.serializers.msgpack import serialize as msgpack_serialize
|
from salt.serializers.msgpack import serialize as msgpack_serialize
|
||||||
from salt.template import compile_template, compile_template_str
|
from salt.template import compile_template, compile_template_str
|
||||||
from salt.utils.odict import DefaultOrderedDict, OrderedDict
|
from salt.utils.odict import DefaultOrderedDict, HashableOrderedDict
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
@ -127,11 +127,6 @@ STATE_INTERNAL_KEYWORDS = STATE_REQUISITE_KEYWORDS.union(
|
||||||
).union(STATE_RUNTIME_KEYWORDS)
|
).union(STATE_RUNTIME_KEYWORDS)
|
||||||
|
|
||||||
|
|
||||||
class HashableOrderedDict(OrderedDict):
|
|
||||||
def __hash__(self):
|
|
||||||
return id(self)
|
|
||||||
|
|
||||||
|
|
||||||
def split_low_tag(tag):
|
def split_low_tag(tag):
|
||||||
"""
|
"""
|
||||||
Take a low tag and split it back into the low dict that it came from
|
Take a low tag and split it back into the low dict that it came from
|
||||||
|
|
|
@ -62,3 +62,8 @@ class DefaultOrderedDict(OrderedDict):
|
||||||
return "DefaultOrderedDict({}, {})".format(
|
return "DefaultOrderedDict({}, {})".format(
|
||||||
self.default_factory, super().__repr__()
|
self.default_factory, super().__repr__()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class HashableOrderedDict(OrderedDict):
|
||||||
|
def __hash__(self):
|
||||||
|
return id(self)
|
||||||
|
|
|
@ -13,7 +13,7 @@ import collections
|
||||||
import yaml # pylint: disable=blacklisted-import
|
import yaml # pylint: disable=blacklisted-import
|
||||||
|
|
||||||
import salt.utils.context
|
import salt.utils.context
|
||||||
from salt.utils.odict import OrderedDict
|
from salt.utils.odict import HashableOrderedDict, OrderedDict
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from yaml import CDumper as Dumper
|
from yaml import CDumper as Dumper
|
||||||
|
@ -71,7 +71,9 @@ def represent_undefined(dumper, data):
|
||||||
|
|
||||||
|
|
||||||
OrderedDumper.add_representer(OrderedDict, represent_ordereddict)
|
OrderedDumper.add_representer(OrderedDict, represent_ordereddict)
|
||||||
|
OrderedDumper.add_representer(HashableOrderedDict, represent_ordereddict)
|
||||||
SafeOrderedDumper.add_representer(OrderedDict, represent_ordereddict)
|
SafeOrderedDumper.add_representer(OrderedDict, represent_ordereddict)
|
||||||
|
SafeOrderedDumper.add_representer(HashableOrderedDict, represent_ordereddict)
|
||||||
SafeOrderedDumper.add_representer(None, represent_undefined)
|
SafeOrderedDumper.add_representer(None, represent_undefined)
|
||||||
|
|
||||||
OrderedDumper.add_representer(
|
OrderedDumper.add_representer(
|
||||||
|
|
|
@ -2,7 +2,11 @@
|
||||||
Unit tests for salt.utils.yamldumper
|
Unit tests for salt.utils.yamldumper
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from collections import OrderedDict, defaultdict
|
||||||
|
|
||||||
import salt.utils.yamldumper
|
import salt.utils.yamldumper
|
||||||
|
from salt.utils.context import NamespacedDictWrapper
|
||||||
|
from salt.utils.odict import HashableOrderedDict
|
||||||
from tests.support.unit import TestCase
|
from tests.support.unit import TestCase
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,3 +39,80 @@ class YamlDumperTestCase(TestCase):
|
||||||
salt.utils.yamldumper.safe_dump(data, default_flow_style=False)
|
salt.utils.yamldumper.safe_dump(data, default_flow_style=False)
|
||||||
== "foo: bar\n"
|
== "foo: bar\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_yaml_ordered_dump(self):
|
||||||
|
"""
|
||||||
|
Test yaml.dump with OrderedDict
|
||||||
|
"""
|
||||||
|
data = OrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||||
|
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||||
|
assert (
|
||||||
|
salt.utils.yamldumper.dump(data, Dumper=salt.utils.yamldumper.OrderedDumper)
|
||||||
|
== exp_yaml
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_yaml_safe_ordered_dump(self):
|
||||||
|
"""
|
||||||
|
Test yaml.safe_dump with OrderedDict
|
||||||
|
"""
|
||||||
|
data = OrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||||
|
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||||
|
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||||
|
|
||||||
|
def test_yaml_indent_safe_ordered_dump(self):
|
||||||
|
"""
|
||||||
|
Test yaml.dump with IndentedSafeOrderedDumper
|
||||||
|
"""
|
||||||
|
data = OrderedDict([("foo", ["bar", "baz"]), ("qux", "quux")])
|
||||||
|
exp_yaml = "foo:\n- bar\n- baz\nqux: quux\n"
|
||||||
|
assert (
|
||||||
|
salt.utils.yamldumper.dump(
|
||||||
|
data,
|
||||||
|
Dumper=salt.utils.yamldumper.IndentedSafeOrderedDumper,
|
||||||
|
default_flow_style=False,
|
||||||
|
)
|
||||||
|
== exp_yaml
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_yaml_defaultdict_dump(self):
|
||||||
|
"""
|
||||||
|
Test yaml.dump with defaultdict
|
||||||
|
"""
|
||||||
|
data = defaultdict(list)
|
||||||
|
data["foo"].append("bar")
|
||||||
|
exp_yaml = "foo: [bar]\n"
|
||||||
|
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||||
|
|
||||||
|
def test_yaml_namespaced_dict_wrapper_dump(self):
|
||||||
|
"""
|
||||||
|
Test yaml.dump with NamespacedDictWrapper
|
||||||
|
"""
|
||||||
|
data = NamespacedDictWrapper({"test": {"foo": "bar"}}, "test")
|
||||||
|
exp_yaml = (
|
||||||
|
"!!python/object/new:salt.utils.context.NamespacedDictWrapper\n"
|
||||||
|
"dictitems: {foo: bar}\n"
|
||||||
|
"state:\n"
|
||||||
|
" _NamespacedDictWrapper__dict:\n"
|
||||||
|
" test: {foo: bar}\n"
|
||||||
|
" pre_keys: !!python/tuple [test]\n"
|
||||||
|
)
|
||||||
|
assert salt.utils.yamldumper.dump(data) == exp_yaml
|
||||||
|
|
||||||
|
def test_yaml_undefined_dump(self):
|
||||||
|
"""
|
||||||
|
Test yaml.safe_dump with None
|
||||||
|
"""
|
||||||
|
data = {"foo": None}
|
||||||
|
exp_yaml = "{foo: null}\n"
|
||||||
|
assert salt.utils.yamldumper.safe_dump(data) == exp_yaml
|
||||||
|
|
||||||
|
def test_yaml_hashable_ordered_dict_dump(self):
|
||||||
|
"""
|
||||||
|
Test yaml.dump with HashableOrderedDict
|
||||||
|
"""
|
||||||
|
data = HashableOrderedDict([("foo", "bar"), ("baz", "qux")])
|
||||||
|
exp_yaml = "{foo: bar, baz: qux}\n"
|
||||||
|
assert (
|
||||||
|
salt.utils.yamldumper.dump(data, Dumper=salt.utils.yamldumper.OrderedDumper)
|
||||||
|
== exp_yaml
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue