summaryrefslogtreecommitdiff
path: root/representer.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-10-17 09:55:06 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-10-17 09:55:06 +0200
commitd24b5a2651d1e367e2f50e4c00fe96bea5a706a6 (patch)
tree6da171cf60a4d155d063ebe256319583b27efa29 /representer.py
parent793b5fc54e94a3c40b7db0d1141ddea9a1d52c81 (diff)
downloadruamel.yaml-d24b5a2651d1e367e2f50e4c00fe96bea5a706a6.tar.gz
fix issue #250 dropping of comment when seq within seq0.15.74
*When this change indeed resolves your problem, please **Close** this issue*. *(You can do so using the WorkFlow pull-down (close to the top right of this page))*
Diffstat (limited to 'representer.py')
-rw-r--r--representer.py19
1 files changed, 18 insertions, 1 deletions
diff --git a/representer.py b/representer.py
index 9f7179c..28ec9a2 100644
--- a/representer.py
+++ b/representer.py
@@ -6,6 +6,7 @@ from __future__ import print_function, absolute_import, division
from ruamel.yaml.error import * # NOQA
from ruamel.yaml.nodes import * # NOQA
from ruamel.yaml.compat import text_type, binary_type, to_unicode, PY2, PY3, ordereddict
+from ruamel.yaml.compat import nprint, nprintf # NOQA
from ruamel.yaml.scalarstring import (
LiteralScalarString,
FoldedScalarString,
@@ -889,7 +890,7 @@ class RoundTripRepresenter(SafeRepresenter):
item_comments = {}
for idx, item in enumerate(sequence):
node_item = self.represent_data(item)
- node_item.comment = item_comments.get(idx)
+ self.merge_comments(node_item, item_comments.get(idx))
if not (isinstance(node_item, ScalarNode) and not node_item.style):
best_style = False
value.append(node_item)
@@ -900,6 +901,22 @@ class RoundTripRepresenter(SafeRepresenter):
node.flow_style = best_style
return node
+ def merge_comments(self, node, comments):
+ # type: (Any, Any) -> Any
+ if comments is None:
+ assert hasattr(node, 'comment')
+ return node
+ if getattr(node, 'comment', None) is not None:
+ for idx, val in enumerate(comments):
+ if idx >= len(node.comment):
+ continue
+ nc = node.comment[idx]
+ if nc is not None:
+ assert val is None or val == nc
+ comments[idx] = nc
+ node.comment = comments
+ return node
+
def represent_key(self, data):
# type: (Any) -> Any
if isinstance(data, CommentedKeySeq):