diff options
author | Anthon van der Neut <anthon@mnt.org> | 2017-08-31 08:17:35 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2017-08-31 08:17:35 +0200 |
commit | 9d09d17dd47a286ce89b741a98f16f936c836cba (patch) | |
tree | e19849c91a506c768db76c4a7f9ca3e76e910756 /constructor.py | |
parent | 10d88a29cf50dc4679a3b079353cd593113d3e41 (diff) | |
download | ruamel.yaml-9d09d17dd47a286ce89b741a98f16f936c836cba.tar.gz |
round-trip support for tagged scalars0.15.33
Inspired by Matthew Patton's question on StackOverflow:
https://stackoverflow.com/a/45967047/1307905
Diffstat (limited to 'constructor.py')
-rw-r--r-- | constructor.py | 40 |
1 files changed, 25 insertions, 15 deletions
diff --git a/constructor.py b/constructor.py index c77d11c..9706bbe 100644 --- a/constructor.py +++ b/constructor.py @@ -19,7 +19,7 @@ from ruamel.yaml.compat import (utf8, builtins_module, to_str, PY2, PY3, # NOQA ordereddict, text_type, nprint, version_tnf) from ruamel.yaml.comments import * # NOQA from ruamel.yaml.comments import (CommentedMap, CommentedOrderedMap, CommentedSet, - CommentedKeySeq, CommentedSeq) + CommentedKeySeq, CommentedSeq, TaggedScalar) from ruamel.yaml.scalarstring import * # NOQA from ruamel.yaml.scalarstring import (PreservedScalarString, SingleQuotedScalarString, DoubleQuotedScalarString, ScalarString) @@ -1419,21 +1419,31 @@ class RoundTripConstructor(SafeConstructor): def construct_undefined(self, node): # type: (Any) -> Any try: - data = CommentedMap() - data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) - if node.flow_style is True: - data.fa.set_flow_style() - elif node.flow_style is False: - data.fa.set_block_style() - data.yaml_set_tag(node.tag) - yield data - self.construct_mapping(node, data) + if isinstance(node, MappingNode): + data = CommentedMap() + data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) + if node.flow_style is True: + data.fa.set_flow_style() + elif node.flow_style is False: + data.fa.set_block_style() + data.yaml_set_tag(node.tag) + yield data + self.construct_mapping(node, data) + return + elif isinstance(node, ScalarNode): + data = TaggedScalar() + data.value = self.construct_scalar(node) + data.style = node.style + data.yaml_set_tag(node.tag) + yield data + return except: - raise ConstructorError( - None, None, - "could not determine a constructor for the tag %r" % - utf8(node.tag), - node.start_mark) + pass + raise ConstructorError( + None, None, + "could not determine a constructor for the tag %r" % + utf8(node.tag), + node.start_mark) def construct_yaml_timestamp(self, node, values=None): # type: (Any, Any) -> Any |