diff options
author | Anthon van der Neut <anthon@mnt.org> | 2015-03-26 10:34:12 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2015-03-26 10:34:12 +0100 |
commit | ab80df5141fa3f981286a52d46131ab5bb36a64b (patch) | |
tree | 88be8bd0f7e07aa7929ea6b2579cbdd3a1cac66f | |
parent | b391d63624636795e5fbb29e53d5530a8c11801a (diff) | |
download | ruamel.yaml-ab80df5141fa3f981286a52d46131ab5bb36a64b.tar.gz |
preserve flow style on sequences for roundtrip handling 'a: b, c, d'
-rw-r--r-- | py/comments.py | 4 | ||||
-rw-r--r-- | py/constructor.py | 4 | ||||
-rw-r--r-- | py/events.py | 3 | ||||
-rw-r--r-- | py/representer.py | 2 | ||||
-rw-r--r-- | py/yaml.py | 2 | ||||
-rw-r--r-- | test/test_indentation.py | 4 |
6 files changed, 16 insertions, 3 deletions
diff --git a/py/comments.py b/py/comments.py index 21bbf7e..c26ad1d 100644 --- a/py/comments.py +++ b/py/comments.py @@ -80,6 +80,10 @@ class Format(object): self._flow_style = False def flow_style(self, default): + """if default (the flow_style) is None, the flow style tacked on to + the object explicitly will be taken. If that is None as well the + default flow style rules the format down the line, or the type + of the constituent values (simple -> flow, map/list -> block)""" if self._flow_style is None: return default return self._flow_style diff --git a/py/constructor.py b/py/constructor.py index 6e3a7ac..ae1046b 100644 --- a/py/constructor.py +++ b/py/constructor.py @@ -909,6 +909,10 @@ class RoundTripConstructor(SafeConstructor): def construct_yaml_seq(self, node): data = CommentedSeq() + if node.flow_style is True: + data.fa.set_flow_style() + elif node.flow_style is False: + data.fa.set_block_style() if node.comment: data._yaml_add_comment(node.comment) yield data diff --git a/py/events.py b/py/events.py index 3dcab45..e1b9c62 100644 --- a/py/events.py +++ b/py/events.py @@ -16,7 +16,8 @@ class Event(object): self.comment = comment def __repr__(self): - attributes = [key for key in ['anchor', 'tag', 'implicit', 'value'] + attributes = [key for key in ['anchor', 'tag', 'implicit', 'value', + 'flow_style', 'style'] if hasattr(self, key)] arguments = ', '.join(['%s=%r' % (key, getattr(self, key)) for key in attributes]) diff --git a/py/representer.py b/py/representer.py index 1492eb1..f4ca75f 100644 --- a/py/representer.py +++ b/py/representer.py @@ -670,6 +670,7 @@ class RoundTripRepresenter(SafeRepresenter): def represent_omap(self, tag, omap, flow_style=None): value = [] + flow_style = omap.fa.flow_style(flow_style) node = SequenceNode(tag, value, flow_style=flow_style) if self.alias_key is not None: self.represented_objects[self.alias_key] = node @@ -716,6 +717,7 @@ class RoundTripRepresenter(SafeRepresenter): tag = u'tag:yaml.org,2002:set' # return self.represent_mapping(tag, value) value = [] + flow_style = setting.fa.flow_style(flow_style) node = MappingNode(tag, value, flow_style=flow_style) if self.alias_key is not None: self.represented_objects[self.alias_key] = node @@ -142,6 +142,8 @@ class YAML: secure: optional """) + input = "a: [b, c, d]" + print_input(input) print_tokens(input) print_events(input) diff --git a/test/test_indentation.py b/test/test_indentation.py index cdecf7c..3b4e939 100644 --- a/test/test_indentation.py +++ b/test/test_indentation.py @@ -17,7 +17,7 @@ def rt(s): ).strip() + '\n' -@pytest.mark.xfail +# @pytest.mark.xfail def test_roundtrip_inline_list(): s = 'a: [a, b, c]\n' output = rt(s) @@ -34,7 +34,7 @@ def test_added_inline_list(): data = ruamel.yaml.load(s1, Loader=ruamel.yaml.RoundTripLoader) val = data['a'] val.fa.set_flow_style() - print(type(val), '_yaml_format' in dir(val)) + # print(type(val), '_yaml_format' in dir(val)) output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper) assert s == output |