diff options
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | README.rst | 5 | ||||
-rw-r--r-- | __init__.py | 4 | ||||
-rw-r--r-- | _test/test_add_xxx.py | 42 | ||||
-rw-r--r-- | emitter.py | 5 | ||||
-rw-r--r-- | scalarstring.py | 6 | ||||
-rw-r--r-- | serializer.py | 4 |
8 files changed, 67 insertions, 7 deletions
@@ -1,3 +1,8 @@ +[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/>`_) + [0, 15, 8]: 2017-06-15 - allow plug-in install via ``install ruamel.yaml[jinja2]`` @@ -2,7 +2,8 @@ UTILNAME:=yaml PKGNAME:=ruamel.yaml INSTPKGNAME:=--pkg ruamel.yaml -VERSION:=0.15.8.dev +VERSION:=0.15.9.dev +VERSIOND:=0.15.9.dev0 include ~/.config/develop/Makefile.inc @@ -32,6 +32,11 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key +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/>`_) + 0.15.8 (2017-06-15): - allow plug-in install via ``install ruamel.yaml[jinja2]`` diff --git a/__init__.py b/__init__.py index 48f5fbe..38b8257 100644 --- a/__init__.py +++ b/__init__.py @@ -11,8 +11,8 @@ if False: # MYPY _package_data = dict( full_package_name='ruamel.yaml', - version_info=(0, 15, 8), - __version__='0.15.8', + version_info=(0, 15, 9), + __version__='0.15.9', 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/_test/test_add_xxx.py b/_test/test_add_xxx.py index c283b44..5b064f7 100644 --- a/_test/test_add_xxx.py +++ b/_test/test_add_xxx.py @@ -40,8 +40,9 @@ def test_dice_constructor_with_loader(): def test_dice_representer(): ruamel.yaml.add_representer(Dice, dice_representer) + # ruamel.yaml 0.15.8+ no longer forces quotes tagged scalars assert ruamel.yaml.dump(dict(gold=Dice(10, 6)), default_flow_style=False) == \ - "gold: !dice '10d6'\n" + "gold: !dice 10d6\n" def test_dice_implicit_resolver(): @@ -105,3 +106,42 @@ def test_yaml_obj_with_loader_and_dumper(): # ToDo use nullege to search add_multi_representer and add_path_resolver # and add some test code + +# Issue 127 reported by Tommy Wang + +class Ref(ruamel.yaml.YAMLObject): + yaml_constructor = ruamel.yaml.RoundTripConstructor + yaml_representer = ruamel.yaml.RoundTripRepresenter + yaml_tag = u'!Ref' + + def __init__(self, logical_id): + self.logical_id = logical_id + + @classmethod + def from_yaml(cls, loader, node): + return cls(loader.construct_scalar(node)) + + @classmethod + def to_yaml(cls, dumper, data): + if isinstance(data.logical_id, ruamel.yaml.scalarstring.ScalarString): + style = data.logical_id.style # ruamel.yaml>0.15.8 + else: + style = None + return dumper.represent_scalar(cls.yaml_tag, data.logical_id, style=style) + + +def test_issue_127(): + document = dedent('''\ + AList: + - !Ref One + - !Ref 'Two' + - !Ref + Two and a half + BList: [!Ref Three, !Ref "Four"] + CList: + - Five Six + - 'Seven Eight' + ''') + data = ruamel.yaml.round_trip_load(document, preserve_quotes=True) + assert ruamel.yaml.round_trip_dump(data, indent=4, block_seq_indent=2) == \ + document.replace('\n Two and', ' Two and') @@ -629,6 +629,9 @@ class Emitter(object): self.prepared_tag = self.prepare_tag(tag) if self.prepared_tag: self.write_indicator(self.prepared_tag, True) + if self.sequence_context and not self.flow_level and \ + isinstance(self.event, ScalarEvent): + self.no_newline = True self.prepared_tag = None def choose_scalar_style(self): @@ -638,7 +641,7 @@ class Emitter(object): if self.event.style == '"' or self.canonical: return '"' if (not self.event.style or self.event.style == '?') and \ - self.event.implicit[0]: + (self.event.implicit[0] or not self.event.implicit[2]): if (not (self.simple_key_context and (self.analysis.empty or self.analysis.multiline)) and (self.flow_level and self.analysis.allow_flow_plain or diff --git a/scalarstring.py b/scalarstring.py index 570240b..571230f 100644 --- a/scalarstring.py +++ b/scalarstring.py @@ -26,6 +26,8 @@ class ScalarString(text_type): class PreservedScalarString(ScalarString): __slots__ = () + style = "|" + def __new__(cls, value): # type: (Text) -> Any return ScalarString.__new__(cls, value) @@ -34,6 +36,8 @@ class PreservedScalarString(ScalarString): class SingleQuotedScalarString(ScalarString): __slots__ = () + style = "'" + def __new__(cls, value): # type: (Text) -> Any return ScalarString.__new__(cls, value) @@ -42,6 +46,8 @@ class SingleQuotedScalarString(ScalarString): class DoubleQuotedScalarString(ScalarString): __slots__ = () + style = '"' + def __new__(cls, value): # type: (Text) -> Any return ScalarString.__new__(cls, value) diff --git a/serializer.py b/serializer.py index c949c79..46884b5 100644 --- a/serializer.py +++ b/serializer.py @@ -152,13 +152,13 @@ class Serializer(object): # if not equal quoting is necessary for strings detected_tag = self.resolver.resolve(ScalarNode, node.value, (True, False)) default_tag = self.resolver.resolve(ScalarNode, node.value, (False, True)) - implicit = (node.tag == detected_tag), (node.tag == default_tag) + implicit = ((node.tag == detected_tag), (node.tag == default_tag), + node.tag.startswith('tag:yaml.org,2002:')) self.emitter.emit(ScalarEvent(alias, node.tag, implicit, node.value, style=node.style, comment=node.comment)) elif isinstance(node, SequenceNode): implicit = (node.tag == self.resolver.resolve(SequenceNode, node.value, True)) comment = node.comment - # print('comment >>>>>>>>>>>>>.', comment, node.flow_style) end_comment = None seq_comment = None if node.flow_style is True: |