summaryrefslogtreecommitdiff
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
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
-rw-r--r--CHANGES7
-rw-r--r--README.rst34
-rw-r--r--__init__.py4
-rw-r--r--_doc/basicuse.ryd2
-rw-r--r--_test/test_tag.py10
-rw-r--r--comments.py6
-rw-r--r--constructor.py40
-rw-r--r--representer.py14
8 files changed, 84 insertions, 33 deletions
diff --git a/CHANGES b/CHANGES
index ff8f432..8d8d2f0 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,10 @@
+[0, 15, 33]: 2017-08-31
+ - support for "undefined" round-tripping tagged scalar objects (in addition to
+ tagged mapping object). Inspired by a use case presented by Matthew Patton
+ on `StackOverflow <https://stackoverflow.com/a/45967047/1307905>`__.
+ - fix issue 148: replace cryptic error message when using !!timestamp with an
+ incorrectly formatted or non- scalar. Reported by FichteFoll.
+
[0, 15, 32]: 2017-08-21
- allow setting ``yaml.default_flow_style = None`` (default: ``False``) for
for ``typ='rt'``.
diff --git a/README.rst b/README.rst
index 7cd8333..b1a0024 100644
--- a/README.rst
+++ b/README.rst
@@ -35,17 +35,25 @@ ChangeLog
.. should insert NEXT: at the beginning of line for next key
+0.15.33 (2017-08-31):
+ - support for "undefined" round-tripping tagged scalar objects (in addition to
+ tagged mapping object). Inspired by a use case presented by Matthew Patton
+ on `StackOverflow <https://stackoverflow.com/a/45967047/1307905>`__.
+ - fix issue 148: replace cryptic error message when using !!timestamp with an
+ incorrectly formatted or non- scalar. Reported by FichteFoll.
+
0.15.32 (2017-08-21):
- allow setting ``yaml.default_flow_style = None`` (default: ``False``) for
for ``typ='rt'``.
- fix for issue 149: multiplications on ``ScalarFloat`` now return ``float``
+ (reported by jan.brezina@tul.cz)
0.15.31 (2017-08-15):
- fix Comment dumping
0.15.30 (2017-08-14):
- fix for issue with "compact JSON" not parsing: ``{"in":{},"out":{}}``
- (reported on `StackOverflow <https://stackoverflow.com/q/45681626/1307905>`_ by
+ (reported on `StackOverflow <https://stackoverflow.com/q/45681626/1307905>`__ by
`mjalkio <https://stackoverflow.com/users/5130525/mjalkio>`_
0.15.29 (2017-08-14):
@@ -74,7 +82,7 @@ ChangeLog
42.00E+56, 0.0, -0.0 etc. while keeping the format. Underscores in mantissas
are not preserved/supported (yet, is anybody using that?).
- (finally) fixed longstanding issue 23 (reported by `Antony Sottile
- <https://bitbucket.org/asottile/>`_), now handling comment between block
+ <https://bitbucket.org/asottile/>`__), now handling comment between block
mapping key and value correctly
- warn on YAML 1.1 float input that is incorrect (triggered by invalid YAML
provided by Cecil Curry)
@@ -102,28 +110,28 @@ ChangeLog
0.15.18 (2017-07-04):
- missing ``pure`` attribute on ``YAML`` useful for implementing `!include` tag
constructor for `including YAML files in a YAML file
- <https://stackoverflow.com/a/44913652/1307905>`_
+ <https://stackoverflow.com/a/44913652/1307905>`__
- some documentation improvements
- trigger of doc build on new revision
0.15.17 (2017-07-03):
- support for Unicode supplementary Plane **output**
(input was already supported, triggered by
- `this <https://stackoverflow.com/a/44875714/1307905>`_ Stack Overflow Q&A)
+ `this <https://stackoverflow.com/a/44875714/1307905>`__ Stack Overflow Q&A)
0.15.16 (2017-07-01):
- minor typing issues (reported and fix provided by
- `Manvendra Singh <https://bitbucket.org/manu-chroma/>`_
+ `Manvendra Singh <https://bitbucket.org/manu-chroma/>`__
- small doc improvements
0.15.15 (2017-06-27):
- fix for issue 135, typ='safe' not dumping in Python 2.7
- (reported by Andrzej Ostrowski <https://bitbucket.org/aostr123/>`_)
+ (reported by Andrzej Ostrowski <https://bitbucket.org/aostr123/>`__)
0.15.14 (2017-06-25):
- fix for issue 133, in setup.py: change ModuleNotFoundError to
ImportError (reported and fix by
- `Asley Drake <https://github.com/aldraco>`_)
+ `Asley Drake <https://github.com/aldraco>`__)
0.15.13 (2017-06-24):
- suppress duplicate key warning on mappings with merge keys (reported by
@@ -135,7 +143,7 @@ ChangeLog
0.15.11 (2017-06-24):
- fix for issue 130, regression in nested merge keys (reported by
- `David Fee <https://bitbucket.org/dfee/>`_)
+ `David Fee <https://bitbucket.org/dfee/>`__)
0.15.10 (2017-06-23):
- top level PreservedScalarString not indented if not explicitly asked to
@@ -145,7 +153,7 @@ ChangeLog
0.15.9 (2017-06-16):
- fix for issue 127: tagged scalars were always quoted and seperated
by a newline when in a block sequence (reported and largely fixed by
- `Tommy Wang <https://bitbucket.org/twang817/>`_)
+ `Tommy Wang <https://bitbucket.org/twang817/>`__)
0.15.8 (2017-06-15):
- allow plug-in install via ``install ruamel.yaml[jinja2]``
@@ -175,7 +183,7 @@ ChangeLog
- update to conform to mypy 0.511: mypy --strict
0.15.1 (2017-06-07):
- - `duplicate keys <http://yaml.readthedocs.io/en/latest/api.html#duplicate-keys>`_
+ - `duplicate keys <http://yaml.readthedocs.io/en/latest/api.html#duplicate-keys>`__
in mappings generate an error (in the old API this change generates a warning until 0.16)
- dependecy on ruamel.ordereddict for 2.7 now via extras_require
@@ -206,7 +214,7 @@ ChangeLog
0.14.8 (2017-04-19):
- fix Text not available on 3.5.0 and 3.5.1, now proactively setting version guards
- on all files (reported by `João Paulo Magalhães <https://bitbucket.org/jpmag/>`_)
+ on all files (reported by `João Paulo Magalhães <https://bitbucket.org/jpmag/>`__)
0.14.7 (2017-04-18):
- round trip of integers (decimal, octal, hex, binary) now preserve
@@ -233,8 +241,8 @@ ChangeLog
0.14.3 (2017-03-31):
- fix for 0o52 not being a string in YAML 1.1 (reported on
- `StackOverflow Q&A 43138503 <http://stackoverflow.com/a/43138503/1307905>`_ by
- `Frank D <http://stackoverflow.com/users/7796630/frank-d>`_)
+ `StackOverflow Q&A 43138503 <http://stackoverflow.com/a/43138503/1307905>`__ by
+ `Frank D <http://stackoverflow.com/users/7796630/frank-d>`__)
0.14.2 (2017-03-23):
- fix for old default pip on Ubuntu 14.04 (reported by Sébastien Maccagnoni-Munch)
diff --git a/__init__.py b/__init__.py
index a90d9d2..0a8148e 100644
--- a/__init__.py
+++ b/__init__.py
@@ -7,8 +7,8 @@ if False: # MYPY
_package_data = dict(
full_package_name='ruamel.yaml',
- version_info=(0, 15, 32),
- __version__='0.15.32',
+ version_info=(0, 15, 33),
+ __version__='0.15.33',
author='Anthon van der Neut',
author_email='a.van.der.neut@ruamel.eu',
description='ruamel.yaml is a YAML parser/emitter that supports roundtrip preservation of comments, seq/map flow style, and map key order', # NOQA
diff --git a/_doc/basicuse.ryd b/_doc/basicuse.ryd
index 6488fe2..36da2d4 100644
--- a/_doc/basicuse.ryd
+++ b/_doc/basicuse.ryd
@@ -26,7 +26,7 @@ You load a YAML document using::
--- !python |
from ruamel.yaml import YAML
-yaml=YAML(typ='safe') # default if not specfied is round-trip
+yaml=YAML(typ='safe') # default, if not specfied, is 'rt' (round-trip)
yaml.load(doc)
--- |
diff --git a/_test/test_tag.py b/_test/test_tag.py
index fc8a4bd..2117ff4 100644
--- a/_test/test_tag.py
+++ b/_test/test_tag.py
@@ -94,3 +94,13 @@ class TestRoundTripCustom:
location: Germany
language: python
""")
+
+ def test_scalar_00(self):
+ # https://stackoverflow.com/a/45967047/1307905
+ round_trip("""\
+ Outputs:
+ Vpc:
+ Value: !Ref: vpc # first tag
+ Export:
+ Name: !Sub "${AWS::StackName}-Vpc" # second tag
+ """)
diff --git a/comments.py b/comments.py
index d77b981..49fd9f4 100644
--- a/comments.py
+++ b/comments.py
@@ -943,6 +943,12 @@ class CommentedSet(MutableSet, CommentedMap):
return 'set({0!r})'.format(self.odict.keys())
+class TaggedScalar(CommentedBase):
+ # the value and style attributes are set during roundtrip construction
+ def __str__(self):
+ return self.value
+
+
def dump_comments(d, name='', sep='.', out=sys.stdout):
# type: (Any, str, str, Any) -> None
"""
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
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)