diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-11-01 15:41:15 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-11-01 15:41:15 +0100 |
commit | e63c1e186f88812d705de8f572373d2abe5f195b (patch) | |
tree | d2ca9500cab4cadd16d8b75852a23f12a359966e | |
parent | 82a72a27c6ba866878fdebe7a70bd4a86f54e81d (diff) | |
download | ruamel.yaml-e63c1e186f88812d705de8f572373d2abe5f195b.tar.gz |
fix issue #255 and #256 empty collection defaulting to flow-style0.15.76
an empty collection now defaults to best style. I.e. if elements are inserted
their style is not forced to flow-style
*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))*
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | README.rst | 9 | ||||
-rw-r--r-- | __init__.py | 4 | ||||
-rw-r--r-- | _doc/_static/pypi.svg | 2 | ||||
-rw-r--r-- | _test/data/duplicate-merge-key.former-loader-error.data | 4 | ||||
-rw-r--r-- | anchor.py | 3 | ||||
-rw-r--r-- | constructor.py | 19 | ||||
-rw-r--r-- | representer.py | 7 |
8 files changed, 32 insertions, 21 deletions
@@ -1,3 +1,8 @@ +[0, 15, 76]: 2018-11-01 + - fix issue with empty mapping and sequence loaded as flow-style + (mapping reported by `Min RK <https://bitbucket.org/minrk/>`__, sequence + by `Maged Ahmed <https://bitbucket.org/maged2/>`__) + [0, 15, 75]: 2018-10-27 - fix issue with single '?' scalar (reported by `Terrance <https://bitbucket.org/OllieTerrance/>`__) @@ -4,8 +4,8 @@ ruamel.yaml ``ruamel.yaml`` is a YAML 1.2 loader/dumper package for Python. -:version: 0.15.75 -:updated: 2018-10-27 +:version: 0.15.76 +:updated: 2018-11-01 :documentation: http://yaml.readthedocs.io :repository: https://bitbucket.org/ruamel/ :pypi: https://pypi.org/project/ruamel.yaml/ @@ -54,6 +54,11 @@ ChangeLog .. should insert NEXT: at the beginning of line for next key (with empty line) +0.15.76 (2018-11-01): + - fix issue with empty mapping and sequence loaded as flow-style + (mapping reported by `Min RK <https://bitbucket.org/minrk/>`__, sequence + by `Maged Ahmed <https://bitbucket.org/maged2/>`__) + 0.15.75 (2018-10-27): - fix issue with single '?' scalar (reported by `Terrance <https://bitbucket.org/OllieTerrance/>`__) diff --git a/__init__.py b/__init__.py index 764755b..9b28251 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, 75), - __version__='0.15.75', + version_info=(0, 15, 76), + __version__='0.15.76', 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/_static/pypi.svg b/_doc/_static/pypi.svg index 2aa33a3..ef9b8ff 100644 --- a/_doc/_static/pypi.svg +++ b/_doc/_static/pypi.svg @@ -1 +1 @@ -<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="86" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="86" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h33v20H0z"/><path fill="#007ec6" d="M33 0h53v20H33z"/><path fill="url(#b)" d="M0 0h86v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="175" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="230">pypi</text><text x="175" y="140" transform="scale(.1)" textLength="230">pypi</text><text x="585" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">0.15.75</text><text x="585" y="140" transform="scale(.1)" textLength="430">0.15.75</text></g> </svg> +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="86" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"/><stop offset="1" stop-opacity=".1"/></linearGradient><clipPath id="a"><rect width="86" height="20" rx="3" fill="#fff"/></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h33v20H0z"/><path fill="#007ec6" d="M33 0h53v20H33z"/><path fill="url(#b)" d="M0 0h86v20H0z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="110"> <text x="175" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="230">pypi</text><text x="175" y="140" transform="scale(.1)" textLength="230">pypi</text><text x="585" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="430">0.15.76</text><text x="585" y="140" transform="scale(.1)" textLength="430">0.15.76</text></g> </svg> diff --git a/_test/data/duplicate-merge-key.former-loader-error.data b/_test/data/duplicate-merge-key.former-loader-error.data deleted file mode 100644 index cebc3a1..0000000 --- a/_test/data/duplicate-merge-key.former-loader-error.data +++ /dev/null @@ -1,4 +0,0 @@ ---- -<<: {x: 1, y: 2} -foo: bar -<<: {z: 3, t: 4} @@ -1,8 +1,8 @@ - anchor_attrib = '_yaml_anchor' + class Anchor(object): __slots__ = 'value', 'always_dump' attrib = anchor_attrib @@ -11,4 +11,3 @@ class Anchor(object): # type: () -> None self.value = None self.always_dump = False - diff --git a/constructor.py b/constructor.py index 05fbb34..bf484e1 100644 --- a/constructor.py +++ b/constructor.py @@ -846,7 +846,7 @@ class Constructor(SafeConstructor): lobject_name = [name] try: __import__(module_name) - except ImportError: + except ImportError as exc: raise ConstructorError( 'while constructing a Python object', mark, @@ -1289,7 +1289,7 @@ class RoundTripConstructor(SafeConstructor): return value # merge = [] - merge_map_list = [] + merge_map_list = [] # type: List[Any] index = 0 while index < len(node.value): key_node, value_node = node.value[index] @@ -1494,25 +1494,28 @@ class RoundTripConstructor(SafeConstructor): # type: (Any) -> Any data = CommentedSeq() 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() if node.comment: data._yaml_add_comment(node.comment) yield data data.extend(self.construct_rt_sequence(node, data)) + self.set_collection_style(data, node) def construct_yaml_map(self, node): # type: (Any) -> Any data = CommentedMap() data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) + yield data + self.construct_mapping(node, data) + self.set_collection_style(data, node) + + def set_collection_style(self, data, node): + # type: (Any, Any) -> None + if len(data) == 0: + return if node.flow_style is True: data.fa.set_flow_style() elif node.flow_style is False: data.fa.set_block_style() - yield data - self.construct_mapping(node, data) def construct_yaml_object(self, node, cls): # type: (Any, Any) -> Any diff --git a/representer.py b/representer.py index 28ec9a2..e0ac31d 100644 --- a/representer.py +++ b/representer.py @@ -895,7 +895,7 @@ class RoundTripRepresenter(SafeRepresenter): best_style = False value.append(node_item) if flow_style is None: - if self.default_flow_style is not None: + if len(sequence) != 0 and self.default_flow_style is not None: node.flow_style = self.default_flow_style else: node.flow_style = best_style @@ -961,11 +961,13 @@ class RoundTripRepresenter(SafeRepresenter): except AttributeError: item_comments = {} merge_list = [m[1] for m in getattr(mapping, merge_attrib, [])] + item_count = 0 if bool(merge_list): items = mapping.non_merged_items() else: items = mapping.items() for item_key, item_value in items: + item_count += 1 node_key = self.represent_key(item_key) node_value = self.represent_data(item_value) item_comment = item_comments.get(item_key) @@ -984,7 +986,8 @@ class RoundTripRepresenter(SafeRepresenter): best_style = False value.append((node_key, node_value)) if flow_style is None: - if self.default_flow_style is not None: + if ((item_count != 0) or bool(merge_list)) \ + and self.default_flow_style is not None: node.flow_style = self.default_flow_style else: node.flow_style = best_style |