summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2018-10-16 17:13:23 +0200
committerAnthon van der Neut <anthon@mnt.org>2018-10-16 17:13:23 +0200
commitf2f0bb9bc12d0fd236e32101b31f5b67c10aa97b (patch)
treee1dc158a0f28a9ff77f2e35819c04d24eaeb9e6f
parent5cdadf2799206bddf2b0370db0849feb827f8a0c (diff)
downloadruamel.yaml-f2f0bb9bc12d0fd236e32101b31f5b67c10aa97b.tar.gz
add .compact() to set non-compacting for sequence/mapping within sequence0.15.73
-rw-r--r--CHANGES5
-rw-r--r--README.rst9
-rw-r--r--__init__.py4
-rw-r--r--_doc/_static/pypi.svg2
-rw-r--r--_doc/example.ryd136
-rw-r--r--emitter.py20
-rw-r--r--main.py10
7 files changed, 139 insertions, 47 deletions
diff --git a/CHANGES b/CHANGES
index cb6ee69..be3d466 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,3 +1,8 @@
+[0, 15, 73]: 2018-10-16
+ - fix irregular output on pre-comment in sequence within sequence (reported
+ by `Thorsten Kampe <https://bitbucket.org/thorstenkampe/>`__)
+ - allow non-compact (i.e. next line) dumping sequence/mapping within sequence.
+
[0, 15, 72]: 2018-10-06
- fix regression on explicit 1.1 loading with the C based scanner/parser
(reported by `Tomas Vavra <https://bitbucket.org/xtomik/>`__)
diff --git a/README.rst b/README.rst
index f466c7f..b40e3b4 100644
--- a/README.rst
+++ b/README.rst
@@ -4,8 +4,8 @@ ruamel.yaml
``ruamel.yaml`` is a YAML 1.2 loader/dumper package for Python.
-:version: 0.15.72
-:updated: 2018-10-06
+:version: 0.15.73
+:updated: 2018-10-16
: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.73 (2018-10-16):
+ - fix irregular output on pre-comment in sequence within sequence (reported
+ by `Thorsten Kampe <https://bitbucket.org/thorstenkampe/>`__)
+ - allow non-compact (i.e. next line) dumping sequence/mapping within sequence.
+
0.15.72 (2018-10-06):
- fix regression on explicit 1.1 loading with the C based scanner/parser
(reported by `Tomas Vavra <https://bitbucket.org/xtomik/>`__)
diff --git a/__init__.py b/__init__.py
index b9a3a44..e68a11f 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, 72),
- __version__='0.15.72',
+ version_info=(0, 15, 73),
+ __version__='0.15.73',
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 19b9de5..4b0bd1b 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.72</text><text x="585" y="140" transform="scale(.1)" textLength="430">0.15.72</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.73</text><text x="585" y="140" transform="scale(.1)" textLength="430">0.15.73</text></g> </svg>
diff --git a/_doc/example.ryd b/_doc/example.ryd
index 5df1d5c..c8e1c5d 100644
--- a/_doc/example.ryd
+++ b/_doc/example.ryd
@@ -125,52 +125,114 @@ both mappings and sequences. For sequences the indent is counted to the
beginning of the scalar, with the dash taking the first position of the
indented "space".
-The following program with three dumps::
+You can change this default indentation by e.g. using ``yaml.indent()``:
+
+
--- !python |
- import sys
- from ruamel.yaml import YAML
- data = {1: {1: [{1: 1, 2: 2}, {1: 1, 2: 2}], 2: 2}, 2: 42}
+import sys
+from ruamel.yaml import YAML
+
+d = dict(a=dict(b=2),c=[3, 4])
+yaml = YAML()
+yaml.dump(d, sys.stdout)
+print('0123456789')
+yaml = YAML()
+yaml.indent(mapping=4, sequence=6, offset=3)
+yaml.dump(d, sys.stdout)
+print('0123456789')
- yaml = YAML()
- yaml.explicit_start = True
- yaml.dump(data, sys.stdout)
- yaml.indent(sequence=4, offset=2)
- yaml.dump(data, sys.stdout)
+--- !stdout |
+
+giving::
+
+
+--- |
+
+If a block sequence or block mapping is the element of a sequence, the
+are, by default, displayed `compact
+<http://yaml.org/spec/1.2/spec.html#id2797686>`__ notation. This means
+that the dash of the "parent" sequence is on the same line as the
+first element resp. first key/value pair of the child collection.
+
+If you want either or both of these (sequence within sequence, mapping
+within sequence) to begin on the next line use ``yaml.compact()``.
+
+
+--- !python |
+
+import sys
+from ruamel.yaml import YAML
+
+d = [dict(b=2), [3, 4]]
+yaml = YAML()
+yaml.dump(d, sys.stdout)
+print('='*15)
+yaml = YAML()
+yaml.compact(seq_seq=False, seq_map=False)
+yaml.dump(d, sys.stdout)
- def sequence_indent_four(s):
- # this will fail on direclty nested lists: {1; [[2, 3], 4]}
- levels = []
- ret_val = ''
- for line in s.splitlines(True):
- ls = line.lstrip()
- indent = len(line) - len(ls)
- if ls.startswith('- '):
- if not levels or indent > levels[-1]:
- levels.append(indent)
- elif levels:
- if indent < levels[-1]:
- levels = levels[:-1]
- # same -> do nothing
- else:
- if levels:
- if indent <= levels[-1]:
- while levels and indent <= levels[-1]:
- levels = levels[:-1]
- ret_val += ' ' * len(levels) + line
- return ret_val
- yaml = YAML()
- yaml.explicit_start = True
- yaml.dump(data, sys.stdout, transform=sequence_indent_four)
+--- !stdout |
+
+giving::
+
+
+--- |
+
+------
+
+The following program uses three dumps on the same data, resulting in a stream with
+three documents:
+
+--- !python |
+import sys
+from ruamel.yaml import YAML
+
+data = {1: {1: [{1: 1, 2: 2}, {1: 1, 2: 2}], 2: 2}, 2: 42}
+
+yaml = YAML()
+yaml.explicit_start = True
+yaml.dump(data, sys.stdout)
+yaml.indent(sequence=4, offset=2)
+yaml.dump(data, sys.stdout)
+
+
+def sequence_indent_four(s):
+ # this will fail on direclty nested lists: {1; [[2, 3], 4]}
+ levels = []
+ ret_val = ''
+ for line in s.splitlines(True):
+ ls = line.lstrip()
+ indent = len(line) - len(ls)
+ if ls.startswith('- '):
+ if not levels or indent > levels[-1]:
+ levels.append(indent)
+ elif levels:
+ if indent < levels[-1]:
+ levels = levels[:-1]
+ # same -> do nothing
+ else:
+ if levels:
+ if indent <= levels[-1]:
+ while levels and indent <= levels[-1]:
+ levels = levels[:-1]
+ ret_val += ' ' * len(levels) + line
+ return ret_val
+
+yaml = YAML()
+yaml.explicit_start = True
+yaml.dump(data, sys.stdout, transform=sequence_indent_four)
+
--- !stdout |
gives as output::
---- |
-The transform example was inspired by a `question posted by *nowox*
-<https://stackoverflow.com/q/44388701/1307905>`_ on
-StackOverflow.
+--- |
+
+The transform example, in the last document, was inspired by a
+`question posted by *nowox*
+<https://stackoverflow.com/q/44388701/1307905>`_ on StackOverflow.
-----
diff --git a/emitter.py b/emitter.py
index d1e9dee..05311f0 100644
--- a/emitter.py
+++ b/emitter.py
@@ -16,7 +16,7 @@ from ruamel.yaml.events import * # NOQA
# fmt: off
from ruamel.yaml.compat import utf8, text_type, PY2, nprint, dbg, DBG_EVENT, \
- check_anchorname_char, nprintf # NOQA
+ check_anchorname_char
# fmt: on
if False: # MYPY
@@ -156,6 +156,9 @@ class Emitter(object):
self.column = 0
self.whitespace = True
self.indention = True
+ self.compact_seq_seq = True # dash after dash
+ self.compact_seq_map = True # key after dash
+ # self.compact_ms = False # dash after key, only when excplicit key with ?
self.no_newline = None # type: Optional[bool] # set if directly after `- `
# Whether the document requires an explicit document indicator
@@ -404,8 +407,8 @@ class Emitter(object):
if isinstance(self.event, ScalarEvent):
self.expect_scalar()
elif isinstance(self.event, SequenceStartEvent):
- nprintf('@', self.indention, self.no_newline, self.column)
- i2, n2 = self.indention, self.no_newline
+ # nprintf('@', self.indention, self.no_newline, self.column)
+ i2, n2 = self.indention, self.no_newline # NOQA
if self.event.comment:
if self.event.flow_style is False and self.event.comment:
if self.write_post_comment(self.event):
@@ -596,7 +599,12 @@ class Emitter(object):
def expect_block_sequence(self):
# type: () -> None
- indentless = self.mapping_context and not self.indention
+ if self.mapping_context:
+ indentless = not self.indention
+ else:
+ indentless = False
+ if not self.compact_seq_seq and self.column != 0:
+ self.write_line_break()
self.increase_indent(flow=False, sequence=True, indentless=indentless)
self.state = self.expect_first_block_sequence_item
@@ -629,6 +637,8 @@ class Emitter(object):
def expect_block_mapping(self):
# type: () -> None
+ if not self.mapping_context and not (self.compact_seq_map or self.column == 0):
+ self.write_line_break()
self.increase_indent(flow=False, sequence=False)
self.state = self.expect_first_block_mapping_key
@@ -1580,7 +1590,7 @@ class Emitter(object):
def write_comment(self, comment, pre=False):
# type: (Any) -> None
value = comment.value
- nprintf('{:02d} {:02d} {!r}'.format(self.column, comment.start_mark.column, value))
+ # nprintf('{:02d} {:02d} {!r}'.format(self.column, comment.start_mark.column, value))
if not pre and value[-1] == '\n':
value = value[:-1]
try:
diff --git a/main.py b/main.py
index 140e696..93c688e 100644
--- a/main.py
+++ b/main.py
@@ -146,6 +146,8 @@ class YAML(object):
self.map_indent = None
self.sequence_indent = None
self.sequence_dash_offset = 0
+ self.compact_seq_seq = None
+ self.compact_seq_map = None
self.top_level_colon_align = None
self.prefix_colon = None
@@ -252,6 +254,10 @@ class YAML(object):
if self.sequence_dash_offset is not None:
_emitter.sequence_dash_offset = self.sequence_dash_offset
# _emitter.block_seq_indent = self.sequence_dash_offset
+ if self.compact_seq_seq is not None:
+ _emitter.compact_seq_seq = self.compact_seq_seq
+ if self.compact_seq_map is not None:
+ _emitter.compact_seq_map = self.compact_seq_map
else:
if getattr(self, '_stream', None) is None:
# wait for the stream
@@ -709,6 +715,10 @@ class YAML(object):
# type: (Any) -> None
self.sequence_dash_offset = val
+ def compact(self, seq_seq=None, seq_map=None):
+ self.compact_seq_seq = seq_seq
+ self.compact_seq_map = seq_map
+
class YAMLContextManager(object):
def __init__(self, yaml, transform=None):