summaryrefslogtreecommitdiff
path: root/constructor.py
diff options
context:
space:
mode:
Diffstat (limited to 'constructor.py')
-rw-r--r--constructor.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/constructor.py b/constructor.py
index 03c88ae..682e95a 100644
--- a/constructor.py
+++ b/constructor.py
@@ -934,6 +934,9 @@ class RoundTripConstructor(SafeConstructor):
# if merge:
# node.value = merge + node.value
+ def _sentinel(self):
+ pass
+
def construct_mapping(self, node, maptyp, deep=False):
if not isinstance(node, MappingNode):
raise ConstructorError(
@@ -950,6 +953,7 @@ class RoundTripConstructor(SafeConstructor):
from ruamel.yaml.serializer import templated_id
if not templated_id(node.anchor):
maptyp.yaml_set_anchor(node.anchor)
+ last_key, last_value = None, self._sentinel
for key_node, value_node in node.value:
# keys can be list -> deep
key = self.construct_object(key_node, deep=True)
@@ -976,6 +980,15 @@ class RoundTripConstructor(SafeConstructor):
"while constructing a mapping", node.start_mark,
"found unhashable key", key_node.start_mark)
value = self.construct_object(value_node, deep=deep)
+ if key_node.comment and len(key_node.comment) > 4 and \
+ key_node.comment[4]:
+ if last_value is None:
+ key_node.comment[0] = key_node.comment.pop(4)
+ maptyp._yaml_add_comment(key_node.comment, value=last_key)
+ else:
+ key_node.comment[2] = key_node.comment.pop(4)
+ maptyp._yaml_add_comment(key_node.comment, key=key)
+ key_node.comment = None
if key_node.comment:
maptyp._yaml_add_comment(key_node.comment, key=key)
if value_node.comment:
@@ -984,6 +997,7 @@ class RoundTripConstructor(SafeConstructor):
key, [key_node.start_mark.line, key_node.start_mark.column,
value_node.start_mark.line, value_node.start_mark.column])
maptyp[key] = value
+ last_key, last_value = key, value # could use indexing
# do this last, or <<: before a key will prevent insertion in instances
# of collections.OrderedDict (as they have no __contains__
if merge_map: