diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-08-14 16:30:15 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-08-14 16:30:15 +0200 |
commit | 0923c2a691f57f9e095f5c4b9a8f06fa78bb46d2 (patch) | |
tree | ba2dc411b705bc0290711a591480a66225634352 /comments.py | |
parent | 65d95151526615f21aa9fbecb92831e17da697bc (diff) | |
download | ruamel.yaml-0923c2a691f57f9e095f5c4b9a8f06fa78bb46d2.tar.gz |
reimplement CommentedSeq to subclass MutableSequence instead of list
re issue #176
Diffstat (limited to 'comments.py')
-rw-r--r-- | comments.py | 87 |
1 files changed, 53 insertions, 34 deletions
diff --git a/comments.py b/comments.py index c98baf8..2063775 100644 --- a/comments.py +++ b/comments.py @@ -12,7 +12,7 @@ import sys import copy -from ruamel.yaml.compat import ordereddict, PY2, string_types +from ruamel.yaml.compat import ordereddict, PY2, string_types, MutableSequence from ruamel.yaml.scalarstring import ScalarString if PY2: @@ -385,42 +385,73 @@ class CommentedBase(object): raise NotImplementedError -class CommentedSeq(list, CommentedBase): - __slots__ = (Comment.attrib,) +class CommentedSeq(MutableSequence, CommentedBase): + __slots__ = (Comment.attrib, '_lst') - def _yaml_add_comment(self, comment, key=NoComment): - # type: (Any, Optional[Any]) -> None - if key is not NoComment: - self.yaml_key_comment_extend(key, comment) - else: - self.ca.comment = comment + def __init__(self, *args, **kw): + # type: (Any, Any) -> None + self._lst = list(*args, **kw) - def _yaml_add_eol_comment(self, comment, key): + def __getitem__(self, idx): + # type: (Any) -> Any + return self._lst[idx] + + def __setitem__(self, idx, value): # type: (Any, Any) -> None - self._yaml_add_comment(comment, key=key) + # try to preserve the scalarstring type if setting an existing key to a new value + if idx < len(self): + if ( + isinstance(value, string_types) + and not isinstance(value, ScalarString) + and isinstance(self[idx], ScalarString) + ): + value = type(self[idx])(value) + self._lst.__setitem__(idx, value) - def _yaml_get_columnX(self, key): + def __delitem__(self, idx=None): # type: (Any) -> Any - return self.ca.items[key][0].start_mark.column + del self._lst[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) + + def __len__(self): + # type: () -> int + return len(self._lst) def insert(self, idx, val): # type: (Any, Any) -> None """the comments after the insertion have to move forward""" - list.insert(self, idx, val) + self._lst.insert(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=None): + def extend(self, val): + # type: (Any) -> None + self._lst.extend(val) + + def __eq__(self, other): + # type: (Any) -> bool + return bool(self._lst == other) + + def _yaml_add_comment(self, comment, key=NoComment): + # type: (Any, Optional[Any]) -> None + if key is not NoComment: + self.yaml_key_comment_extend(key, comment) + else: + self.ca.comment = comment + + def _yaml_add_eol_comment(self, comment, key): + # type: (Any, Any) -> None + self._yaml_add_comment(comment, key=key) + + def _yaml_get_columnX(self, key): # type: (Any) -> Any - 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 + return self.ca.items[key][0].start_mark.column def _yaml_get_column(self, key): # type: (Any) -> Any @@ -461,18 +492,6 @@ class CommentedSeq(list, CommentedBase): self.copy_attributes(res, deep=True) return res - def __setitem__(self, idx, value): - # type: (Any, Any) -> None - # try to preserve the scalarstring type if setting an existing key to a new value - if idx < len(self): - if ( - isinstance(value, string_types) - and not isinstance(value, ScalarString) - and isinstance(self[idx], ScalarString) - ): - value = type(self[idx])(value) - list.__setitem__(self, idx, value) - class CommentedKeySeq(tuple, CommentedBase): """This primarily exists to be able to roundtrip keys that are sequences""" |