diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-12-28 17:14:28 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-12-28 17:14:28 +0100 |
commit | 3ac28c509c0759a881ebfd06d300507e8db2c9d2 (patch) | |
tree | 1e12c26ddc454f101496c91729b60deeaa5c47a9 /representer.py | |
parent | 96cf5fdbbfec1902fcaeeb3ba6c75ccf92f1f2f7 (diff) | |
download | ruamel.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 'representer.py')
-rw-r--r-- | representer.py | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/representer.py b/representer.py index 923b2a2..ced41a0 100644 --- a/representer.py +++ b/representer.py @@ -16,6 +16,7 @@ from ruamel.yaml.scalarstring import ( ) 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 import datetime @@ -289,8 +290,8 @@ class SafeRepresenter(BaseRepresenter): # type: (Any) -> Any return self.represent_scalar(u'tag:yaml.org,2002:str', data) - def represent_bool(self, data): - # type: (Any) -> Any + def represent_bool(self, data, anchor=None): + # type: (Any, Optional[Any]) -> Any try: value = self.dumper.boolean_representation[bool(data)] except AttributeError: @@ -298,7 +299,7 @@ class SafeRepresenter(BaseRepresenter): value = u'true' else: value = u'false' - return self.represent_scalar(u'tag:yaml.org,2002:bool', value) + return self.represent_scalar(u'tag:yaml.org,2002:bool', value, anchor=anchor) def represent_int(self, data): # type: (Any) -> Any @@ -1200,7 +1201,19 @@ class RoundTripRepresenter(SafeRepresenter): tag = data.tag.value except AttributeError: tag = None - return self.represent_scalar(tag, data.value, style=data.style) + try: + anchor = data.yaml_anchor() + except AttributeError: + anchor = None + return self.represent_scalar(tag, data.value, style=data.style, anchor=anchor) + + def represent_scalar_bool(self, data): + # type: (Any) -> Any + try: + anchor = data.yaml_anchor() + except AttributeError: + anchor = None + return SafeRepresenter.represent_bool(self, data, anchor=anchor) RoundTripRepresenter.add_representer(type(None), RoundTripRepresenter.represent_none) @@ -1237,6 +1250,8 @@ RoundTripRepresenter.add_representer(HexCapsInt, RoundTripRepresenter.represent_ RoundTripRepresenter.add_representer(ScalarFloat, RoundTripRepresenter.represent_scalar_float) +RoundTripRepresenter.add_representer(ScalarBoolean, RoundTripRepresenter.represent_scalar_bool) + RoundTripRepresenter.add_representer(CommentedSeq, RoundTripRepresenter.represent_list) RoundTripRepresenter.add_representer(CommentedMap, RoundTripRepresenter.represent_dict) |