summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2016-02-17 01:10:00 +0100
committerAnthon van der Neut <anthon@mnt.org>2016-02-17 01:10:00 +0100
commit7ae833545063f1ddad8468f4dc92cc0240b4d97c (patch)
tree323328c4c49a0e24d3564042b3a8b15e7837a287
parentaf7395581fada1750f45855fb8420e0dd8d5ee19 (diff)
downloadruamel.yaml-7ae833545063f1ddad8468f4dc92cc0240b4d97c.tar.gz
- added CommentedSeq insert and pop with moving comments0.10.23
(issue brought up by Michael Sarahan)
-rw-r--r--__init__.py2
-rw-r--r--_test/test_comments.py134
-rw-r--r--comments.py17
3 files changed, 152 insertions, 1 deletions
diff --git a/__init__.py b/__init__.py
index f889139..f9193ef 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, 10, 22),
+ version_info=(0, 10, 23),
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_comments.py b/_test/test_comments.py
index 727243b..501f06d 100644
--- a/_test/test_comments.py
+++ b/_test/test_comments.py
@@ -280,3 +280,137 @@ class TestMultiLevelGet:
assert d.mlget(['a', 1, 'd', 'f'], list_ok=True) == 196
with pytest.raises(AssertionError):
d.mlget(['a', 1, 'd', 'f']) == 196
+
+class TestInsertPopList:
+ """list insertion is more complex than dict insertion, as you
+ need to move the values to subsequent keys on insert"""
+
+ @property
+ def ins(self):
+ return dedent("""\
+ ab:
+ - a # a
+ - b # b
+ - c
+ - d # d
+
+ de:
+ - 1
+ - 2
+ """)
+
+ def test_insert_0(self):
+ d = round_trip_load(self.ins)
+ d['ab'].insert(0, 'xyz')
+ y = round_trip_dump(d, indent=2)
+ assert y == dedent("""\
+ ab:
+ - xyz
+ - a # a
+ - b # b
+ - c
+ - d # d
+
+ de:
+ - 1
+ - 2
+ """)
+
+
+ def test_insert_1(self):
+ d = round_trip_load(self.ins)
+ d['ab'].insert(4, 'xyz')
+ y = round_trip_dump(d, indent=2)
+ assert y == dedent("""\
+ ab:
+ - a # a
+ - b # b
+ - c
+ - d # d
+
+ - xyz
+ de:
+ - 1
+ - 2
+ """)
+
+ def test_insert_1(self):
+ d = round_trip_load(self.ins)
+ d['ab'].insert(1, 'xyz')
+ y = round_trip_dump(d, indent=2)
+ assert y == dedent("""\
+ ab:
+ - a # a
+ - xyz
+ - b # b
+ - c
+ - d # d
+
+ de:
+ - 1
+ - 2
+ """)
+
+
+ def test_pop_0(self):
+ d = round_trip_load(self.ins)
+ d['ab'].pop(0)
+ y = round_trip_dump(d, indent=2)
+ print(y)
+ assert y == dedent("""\
+ ab:
+ - b # b
+ - c
+ - d # d
+
+ de:
+ - 1
+ - 2
+ """)
+
+ def test_pop_1(self):
+ d = round_trip_load(self.ins)
+ d['ab'].pop(1)
+ y = round_trip_dump(d, indent=2)
+ print(y)
+ assert y == dedent("""\
+ ab:
+ - a # a
+ - c
+ - d # d
+
+ de:
+ - 1
+ - 2
+ """)
+
+ def test_pop_2(self):
+ d = round_trip_load(self.ins)
+ d['ab'].pop(2)
+ y = round_trip_dump(d, indent=2)
+ print(y)
+ assert y == dedent("""\
+ ab:
+ - a # a
+ - b # b
+ - d # d
+
+ de:
+ - 1
+ - 2
+ """)
+
+ def test_pop_3(self):
+ d = round_trip_load(self.ins)
+ d['ab'].pop(3)
+ y = round_trip_dump(d, indent=2)
+ print(y)
+ assert y == dedent("""\
+ ab:
+ - a # a
+ - b # b
+ - c
+ de:
+ - 1
+ - 2
+ """)
diff --git a/comments.py b/comments.py
index f82e1c4..7cac4f6 100644
--- a/comments.py
+++ b/comments.py
@@ -261,6 +261,23 @@ class CommentedSeq(list, CommentedBase):
def _yaml_get_columnX(self, key):
return self.ca.items[key][0].start_mark.column
+ def insert(self, idx, val):
+ """the comments after the insertion have to move forward"""
+ list.insert(self, idx, val)
+ for list_index in sorted(self.ca.items, reverse=True):
+ if list_index < idx:
+ break
+ self.ca.items[list_index+1] = self.ca.items.pop(list_index)
+
+ def pop(self, idx):
+ res = list.pop(self, idx)
+ self.ca.items.pop(idx, None) # might not be there -> default value
+ for list_index in sorted(self.ca.items):
+ if list_index < idx:
+ continue
+ self.ca.items[list_index-1] = self.ca.items.pop(list_index)
+ return res
+
def _yaml_get_column(self, key):
column = None
sel_idx = None