diff options
-rw-r--r-- | README.rst | 42 | ||||
-rw-r--r-- | __init__.py | 2 | ||||
-rw-r--r-- | _test/roundtrip.py | 11 | ||||
-rw-r--r-- | _test/test_indentation.py | 53 | ||||
-rw-r--r-- | dumper.py | 20 | ||||
-rw-r--r-- | emitter.py | 7 | ||||
-rw-r--r-- | main.py | 13 |
7 files changed, 126 insertions, 22 deletions
@@ -28,6 +28,7 @@ Major differences with PyYAML 3.11: on Python 3, and ``!!omap`` is generated for these types. - some `YAML 1.2 <http://yaml.org/spec/1.2/spec.html>`_ enhancements (``0o`` octal prefix, ``\/`` escape) +- scalars in lists are indented - pep8 compliance - tox and py.test based testing - Tests whether the C yaml library is installed as well as the header @@ -52,6 +53,47 @@ Major differences with PyYAML 3.11: (``lc.key('a')``, ``lc.value('a')`` resp. ``lc.item(3)``) - preservation of whitelines after block scalars. Contributed by Sam Thursfield. +Indentation of block sequences +============================== + +Although ruamel.yaml doesn't preserve individual indentations of block sequence +items, it does properly dump:: + + x: + - b: 1 + - 2 + +back to:: + + x: + - b: 1 + - 2 + +if you specify ``indent=4``. + +PyYAML (and older versions of ruamel.yaml) gives you indented scalars:: + + x: + - b: 1 + - 2 + +The dump routine also has an additional ``block_seq_indent`` parameter that +can be used to push the dash inwards, *within the space defined by* ``indent``. + +The above example with the often seen ``indent=4, block_seq_indent=2`` +indentation:: + + x: + - b: 1 + - 2 + + +If the ``block_seq_indent`` is only one less than the indent, there is +not enough room to put the space that has to follow the dash. In that +case the element is pushed to the next line. If you specify ``block_seq_indent>=indent``, then the emitter adjusts the ``indent`` value to equal +``block_seq_indent + 1``. + + Document version support. ========================= diff --git a/__init__.py b/__init__.py index d6bf32d..a273f61 100644 --- a/__init__.py +++ b/__init__.py @@ -9,7 +9,7 @@ from __future__ import absolute_import _package_data = dict( full_package_name="ruamel.yaml", - version_info=(0, 11, 3, "dev"), + version_info=(0, 11, 3), 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/roundtrip.py b/_test/roundtrip.py index bd65c5a..4534a16 100644 --- a/_test/roundtrip.py +++ b/_test/roundtrip.py @@ -27,14 +27,15 @@ def round_trip_load(inp): return ruamel.yaml.load(dinp, ruamel.yaml.RoundTripLoader) -def round_trip_dump(data, indent=None): +def round_trip_dump(data, indent=None, block_seq_indent=None): dumper = ruamel.yaml.RoundTripDumper return ruamel.yaml.dump(data, default_flow_style=False, Dumper=dumper, allow_unicode=True, - indent=indent) + indent=indent, block_seq_indent=block_seq_indent) -def round_trip(inp, outp=None, extra=None, intermediate=None, indent=None): +def round_trip(inp, outp=None, extra=None, intermediate=None, indent=None, + block_seq_indent=None): if outp is None: outp = inp doutp = dedent(outp) @@ -47,9 +48,9 @@ def round_trip(inp, outp=None, extra=None, intermediate=None, indent=None): if data[k] != v: print('{0!r} <> {1!r}'.format(data[k], v)) raise ValueError - res = round_trip_dump(data, indent=indent) + res = round_trip_dump(data, indent=indent, block_seq_indent=block_seq_indent) print('roundtrip data:\n', res, sep='') assert res == doutp - res = round_trip_dump(data, indent=indent) + res = round_trip_dump(data, indent=indent, block_seq_indent=block_seq_indent) print('roundtrip second round data:\n', res, sep='') assert res == doutp diff --git a/_test/test_indentation.py b/_test/test_indentation.py index 4d2ca4f..72ef8dc 100644 --- a/_test/test_indentation.py +++ b/_test/test_indentation.py @@ -95,5 +95,58 @@ class TestIndent: - b """, indent=4) + def test_set_indent_5_block_list_indent_1(self): + round_trip(""" + a: + - b: c + - 1 + - d: + - 2 + """, indent=5, block_seq_indent=1) + + def test_set_indent_4_block_list_indent_2(self): + round_trip(""" + a: + - b: c + - 1 + - d: + - 2 + """, indent=4, block_seq_indent=2) + + def test_set_indent_3_block_list_indent_0(self): + round_trip(""" + a: + - b: c + - 1 + - d: + - 2 + """, indent=3, block_seq_indent=0) + + def test_set_indent_3_block_list_indent_2(self): + round_trip(""" + a: + - + b: c + - + 1 + - + d: + - + 2 + """, indent=3, block_seq_indent=2) + + def test_set_indent_2_block_list_indent_2(self): + round_trip(""" + a: + - + b: c + - + 1 + - + d: + - + 2 + """, indent=2, block_seq_indent=2) + # ############ indentation @@ -22,10 +22,11 @@ class BaseDumper(Emitter, Serializer, BaseRepresenter, BaseResolver): canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, - version=None, tags=None): + version=None, tags=None, block_seq_indent=None): Emitter.__init__(self, stream, canonical=canonical, indent=indent, width=width, - allow_unicode=allow_unicode, line_break=line_break) + allow_unicode=allow_unicode, line_break=line_break, + block_seq_indent=block_seq_indent) Serializer.__init__(self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, @@ -41,10 +42,11 @@ class SafeDumper(Emitter, Serializer, SafeRepresenter, Resolver): canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, - version=None, tags=None): + version=None, tags=None, block_seq_indent=None): Emitter.__init__(self, stream, canonical=canonical, indent=indent, width=width, - allow_unicode=allow_unicode, line_break=line_break) + allow_unicode=allow_unicode, line_break=line_break, + block_seq_indent=block_seq_indent) Serializer.__init__(self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, @@ -60,10 +62,11 @@ class Dumper(Emitter, Serializer, Representer, Resolver): canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, - version=None, tags=None): + version=None, tags=None, block_seq_indent=None): Emitter.__init__(self, stream, canonical=canonical, indent=indent, width=width, - allow_unicode=allow_unicode, line_break=line_break) + allow_unicode=allow_unicode, line_break=line_break, + block_seq_indent=block_seq_indent) Serializer.__init__(self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, @@ -79,10 +82,11 @@ class RoundTripDumper(Emitter, Serializer, RoundTripRepresenter, Resolver): canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=None, explicit_start=None, explicit_end=None, - version=None, tags=None): + version=None, tags=None, block_seq_indent=None): Emitter.__init__(self, stream, canonical=canonical, indent=indent, width=width, - allow_unicode=allow_unicode, line_break=line_break) + allow_unicode=allow_unicode, line_break=line_break, + block_seq_indent=block_seq_indent) Serializer.__init__(self, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, @@ -50,7 +50,7 @@ class Emitter(object): MAX_SIMPLE_KEY_LENGTH = 128 def __init__(self, stream, canonical=None, indent=None, width=None, - allow_unicode=None, line_break=None): + allow_unicode=None, line_break=None, block_seq_indent=None): # The stream should have the methods `write` and possibly `flush`. self.stream = stream @@ -96,9 +96,12 @@ class Emitter(object): # Formatting details. self.canonical = canonical self.allow_unicode = allow_unicode + self.block_seq_indent = block_seq_indent if block_seq_indent else 0 self.best_indent = 2 if indent and 1 < indent < 10: self.best_indent = indent + if self.best_indent < self.block_seq_indent + 1: + self.best_indent = self.block_seq_indent + 1 self.best_width = 80 if width and width > self.best_indent*2: self.best_width = width @@ -430,7 +433,7 @@ class Emitter(object): if self.event.comment and self.event.comment[1]: self.write_pre_comment(self.event) self.write_indent() - self.write_indicator(u'-', True, indention=True) + self.write_indicator((u' ' * self.block_seq_indent) + u'-', True, indention=True) self.states.append(self.expect_block_sequence_item) self.expect_node(sequence=True) @@ -194,7 +194,7 @@ def dump_all(documents, stream=None, Dumper=Dumper, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=enc, explicit_start=None, explicit_end=None, - version=None, tags=None): + version=None, tags=None, block_seq_indent=None): """ Serialize a sequence of Python objects into a YAML stream. If stream is None, return the produced string instead. @@ -210,8 +210,10 @@ def dump_all(documents, stream=None, Dumper=Dumper, default_flow_style=default_flow_style, canonical=canonical, indent=indent, width=width, allow_unicode=allow_unicode, line_break=line_break, - encoding=encoding, version=version, tags=tags, - explicit_start=explicit_start, explicit_end=explicit_end) + encoding=encoding, explicit_start=explicit_start, + explicit_end=explicit_end, version=version, + tags=tags, block_seq_indent=block_seq_indent + ) try: dumper.open() for data in documents: @@ -228,8 +230,7 @@ def dump(data, stream=None, Dumper=Dumper, canonical=None, indent=None, width=None, allow_unicode=None, line_break=None, encoding=enc, explicit_start=None, explicit_end=None, - version=None, tags=None): - + version=None, tags=None, block_seq_indent=None): """ Serialize a Python object into a YAML stream. If stream is None, return the produced string instead. @@ -246,7 +247,7 @@ def dump(data, stream=None, Dumper=Dumper, line_break=line_break, encoding=encoding, explicit_start=explicit_start, explicit_end=explicit_end, - version=version, tags=tags) + version=version, tags=tags, block_seq_indent=block_seq_indent) def safe_dump_all(documents, stream=None, **kwds): |