summaryrefslogtreecommitdiff
path: root/constructor.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-12-28 17:14:28 +0100
committerAnthon van der Neut <anthon@mnt.org>2018-12-28 17:14:28 +0100
commit3ac28c509c0759a881ebfd06d300507e8db2c9d2 (patch)
tree1e12c26ddc454f101496c91729b60deeaa5c47a9 /constructor.py
parent96cf5fdbbfec1902fcaeeb3ba6c75ccf92f1f2f7 (diff)
downloadruamel.yaml-3ac28c509c0759a881ebfd06d300507e8db2c9d2.tar.gz
preserve anchors on scalars, on tagged objects0.15.82
fixes issue #63 fixes issue #266 *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 'constructor.py')
-rw-r--r--constructor.py22
1 files changed, 16 insertions, 6 deletions
diff --git a/constructor.py b/constructor.py
index 61ac4e9..65d6829 100644
--- a/constructor.py
+++ b/constructor.py
@@ -27,6 +27,7 @@ from ruamel.yaml.scalarstring import (SingleQuotedScalarString, DoubleQuotedScal
PlainScalarString, ScalarString,)
from ruamel.yaml.scalarint import ScalarInt, BinaryInt, OctalInt, HexInt, HexCapsInt
from ruamel.yaml.scalarfloat import ScalarFloat
+from ruamel.yaml.scalarbool import ScalarBoolean
from ruamel.yaml.timestamp import TimeStamp
from ruamel.yaml.util import RegExp
@@ -1163,9 +1164,7 @@ class RoundTripConstructor(SafeConstructor):
if underscore is not None:
# cannot have a leading underscore
underscore[2] = len(value_su) > 1 and value_su[-1] == '_'
- return ScalarInt(
- sign * int(value_s), width=len(value_s), underscore=underscore
- )
+ return ScalarInt(sign * int(value_s), width=len(value_s), underscore=underscore)
elif underscore:
# cannot have a leading underscore
underscore[2] = len(value_su) > 1 and value_su[-1] == '_'
@@ -1173,9 +1172,7 @@ class RoundTripConstructor(SafeConstructor):
sign * int(value_s), width=None, underscore=underscore, anchor=node.anchor
)
elif node.anchor:
- return ScalarInt(
- sign * int(value_s), width=None, anchor=node.anchor
- )
+ return ScalarInt(sign * int(value_s), width=None, anchor=node.anchor)
else:
return sign * int(value_s)
@@ -1623,6 +1620,8 @@ class RoundTripConstructor(SafeConstructor):
data.fa.set_block_style()
data.yaml_set_tag(node.tag)
yield data
+ if node.anchor:
+ data.yaml_set_anchor(node.anchor)
self.construct_mapping(node, data)
return
elif isinstance(node, ScalarNode):
@@ -1631,6 +1630,8 @@ class RoundTripConstructor(SafeConstructor):
data2.style = node.style
data2.yaml_set_tag(node.tag)
yield data2
+ if node.anchor:
+ data2.yaml_set_anchor(node.anchor, always_dump=True)
return
elif isinstance(node, SequenceNode):
data3 = CommentedSeq()
@@ -1641,6 +1642,8 @@ class RoundTripConstructor(SafeConstructor):
data3.fa.set_block_style()
data3.yaml_set_tag(node.tag)
yield data3
+ if node.anchor:
+ data3.yaml_set_anchor(node.anchor)
data3.extend(self.construct_sequence(node))
return
except: # NOQA
@@ -1713,6 +1716,13 @@ class RoundTripConstructor(SafeConstructor):
data._yaml['t'] = True
return data
+ def construct_yaml_bool(self, node):
+ # type: (Any) -> Any
+ b = SafeConstructor.construct_yaml_bool(self, node)
+ if node.anchor:
+ return ScalarBoolean(b, anchor=node.anchor)
+ return b
+
RoundTripConstructor.add_constructor(
u'tag:yaml.org,2002:null', RoundTripConstructor.construct_yaml_null