summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2015-03-26 09:47:37 +0100
committerAnthon van der Neut <anthon@mnt.org>2015-03-26 09:47:37 +0100
commitb391d63624636795e5fbb29e53d5530a8c11801a (patch)
tree1ffdec346145a1d0b69dc7b1915a063d4937c865
parentf9c4937a476eb9dae9fa0f54120c34957175aeff (diff)
downloadruamel.yaml-b391d63624636795e5fbb29e53d5530a8c11801a.tar.gz
initial test and first step implementation to preserve (flow) style
-rw-r--r--.hgignore1
-rw-r--r--CHANGES14
-rw-r--r--README.rst1
-rw-r--r--py/__init__.py2
-rw-r--r--py/comments.py33
-rw-r--r--py/representer.py4
-rw-r--r--test/test_indentation.py18
7 files changed, 68 insertions, 5 deletions
diff --git a/.hgignore b/.hgignore
index 085e77d..e53b7ea 100644
--- a/.hgignore
+++ b/.hgignore
@@ -10,4 +10,5 @@ venv
build
*.egg-info
.tox
+.cache
README.pdf
diff --git a/CHANGES b/CHANGES
index 5e69e70..fa1c014 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,17 @@
-0.6 2015-03-XX
+0.7 2015-03-26
+- tests (currently failing) for inline sequece and non-standard spacing between
+ block sequence dash and scaler (Anthony Sotille)
+- initial possibility (on list, i.e. CommentedSeq) to set the flow format
+ explicitly
+
+
+0.6.1 2015-03-15
+- setup.py changed so ruamel.ordereddict no longer is a dependency
+ if not on CPython 2.x (used to test only for 2.x, which breaks pypy 2.5.0
+ reported by Anthony Sotille)
+
+0.6 2015-03-11
- basic support for scalars with preserved newlines
- html option for yaml command
- check if yaml C library is available before trying to compile C extension
diff --git a/README.rst b/README.rst
index 343113d..0253c5b 100644
--- a/README.rst
+++ b/README.rst
@@ -77,6 +77,7 @@ eventuallly resulting in a different Python object (subclass or alternative),
that should behave like the original, but on the way from Python to YAML
generates the original (or at least something much closer).
+
Examples
========
diff --git a/py/__init__.py b/py/__init__.py
index 3f83ebb..1c6a93a 100644
--- a/py/__init__.py
+++ b/py/__init__.py
@@ -21,7 +21,7 @@ def _convert_version(tup):
return ret_val
-version_info = (0, 6, 1)
+version_info = (0, 7)
__version__ = _convert_version(version_info)
del _convert_version
diff --git a/py/comments.py b/py/comments.py
index 4c4050d..21bbf7e 100644
--- a/py/comments.py
+++ b/py/comments.py
@@ -1,3 +1,5 @@
+# coding: utf-8
+
from __future__ import absolute_import
from __future__ import print_function
@@ -5,7 +7,9 @@ __all__ = ["CommentedSeq", "CommentedMap", "CommentedOrderedMap",
"CommentedSet", 'comment_attrib']
"""
-stuff to deal with comments on dict/list/ordereddict/set
+stuff to deal with comments and formatting on dict/list/ordereddict/set
+these are not really related, formatting could be factored out as
+a separate base
"""
from collections import MutableSet
@@ -13,12 +17,12 @@ from collections import MutableSet
from .compat import ordereddict
comment_attrib = '_yaml_comment'
-
+format_attrib = '_yaml_format'
class Comment(object):
# sys.getsize tested the Comment objects, __slots__ make them bigger
# and adding self.end did not matter
- attrib = '_yaml_comment'
+ attrib = comment_attrib
def __init__(self):
self.comment = None # [post, [pre]]
@@ -63,6 +67,23 @@ def NoComment():
pass
+class Format(object):
+ attrib = format_attrib
+
+ def __init__(self):
+ self._flow_style = None
+
+ def set_flow_style(self):
+ self._flow_style = True
+
+ def set_block_style(self):
+ self._flow_style = False
+
+ def flow_style(self, default):
+ if self._flow_style is None:
+ return default
+ return self._flow_style
+
class CommentedBase(object):
@property
def ca(self):
@@ -95,6 +116,12 @@ class CommentedBase(object):
l[3].extend(comment[0])
l[2] = comment[0]
+ @property
+ def fa(self):
+ if not hasattr(self, Format.attrib):
+ setattr(self, Format.attrib, Format())
+ return getattr(self, Format.attrib)
+
class CommentedSeq(list, CommentedBase):
__slots__ = [Comment.attrib, ]
diff --git a/py/representer.py b/py/representer.py
index 74f1578..1492eb1 100644
--- a/py/representer.py
+++ b/py/representer.py
@@ -594,6 +594,10 @@ class RoundTripRepresenter(SafeRepresenter):
def represent_sequence(self, tag, sequence, flow_style=None):
value = []
+ # if the flow_style is None, the flow style tacked on to the object
+ # explicitly will be taken. If that is None as well the default flow
+ # style rules
+ flow_style = sequence.fa.flow_style(flow_style)
node = SequenceNode(tag, value, flow_style=flow_style)
if self.alias_key is not None:
self.represented_objects[self.alias_key] = node
diff --git a/test/test_indentation.py b/test/test_indentation.py
index ae3604e..cdecf7c 100644
--- a/test/test_indentation.py
+++ b/test/test_indentation.py
@@ -2,6 +2,9 @@ from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
+
+from textwrap import dedent
+
import pytest
import ruamel.yaml
@@ -20,6 +23,21 @@ def test_roundtrip_inline_list():
output = rt(s)
assert s == output
+def test_added_inline_list():
+ s1 = dedent("""
+ a:
+ - b
+ - c
+ - d
+ """)
+ s = 'a: [b, c, d]\n'
+ data = ruamel.yaml.load(s1, Loader=ruamel.yaml.RoundTripLoader)
+ val = data['a']
+ val.fa.set_flow_style()
+ print(type(val), '_yaml_format' in dir(val))
+ output = ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper)
+ assert s == output
+
@pytest.mark.xfail
def test_roundtrip_four_space_indents():