summaryrefslogtreecommitdiff
path: root/representer.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2017-08-31 08:17:35 +0200
committerAnthon van der Neut <anthon@mnt.org>2017-08-31 08:17:35 +0200
commit9d09d17dd47a286ce89b741a98f16f936c836cba (patch)
treee19849c91a506c768db76c4a7f9ca3e76e910756 /representer.py
parent10d88a29cf50dc4679a3b079353cd593113d3e41 (diff)
downloadruamel.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 'representer.py')
-rw-r--r--representer.py14
1 files changed, 12 insertions, 2 deletions
diff --git a/representer.py b/representer.py
index 70746bc..085308c 100644
--- a/representer.py
+++ b/representer.py
@@ -642,8 +642,8 @@ Representer.add_multi_representer(object,
Representer.represent_object)
-from ruamel.yaml.comments import CommentedMap, CommentedOrderedMap, CommentedSeq, \
- CommentedKeySeq, CommentedSet, comment_attrib, merge_attrib # NOQA
+from ruamel.yaml.comments import (CommentedMap, CommentedOrderedMap, CommentedSeq,
+ CommentedKeySeq, CommentedSet, comment_attrib, merge_attrib, TaggedScalar) # NOQA
class RoundTripRepresenter(SafeRepresenter):
@@ -1096,6 +1096,13 @@ class RoundTripRepresenter(SafeRepresenter):
value += _yaml['tz']
return self.represent_scalar(u'tag:yaml.org,2002:timestamp', to_unicode(value))
+ def represent_tagged_scalar(self, data):
+ try:
+ tag = data.tag.value
+ except AttributeError:
+ tag = None
+ return self.represent_scalar(tag, data.value, style=data.style)
+
RoundTripRepresenter.add_representer(type(None),
RoundTripRepresenter.represent_none)
@@ -1153,5 +1160,8 @@ if sys.version_info >= (2, 7):
RoundTripRepresenter.add_representer(CommentedSet,
RoundTripRepresenter.represent_set)
+RoundTripRepresenter.add_representer(TaggedScalar,
+ RoundTripRepresenter.represent_tagged_scalar)
+
RoundTripRepresenter.add_representer(TimeStamp,
RoundTripRepresenter.represent_datetime)