summaryrefslogtreecommitdiff
path: root/py/comments.py
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2015-06-23 12:22:18 +0200
committerAnthon van der Neut <anthon@mnt.org>2015-06-23 12:22:18 +0200
commit591b1005706b0db167d5c55eefee7a0b0f0442e6 (patch)
treea38bb7988820299673f8e5217c1197b8aa78f211 /py/comments.py
parent74c68f87e5644229fadee9ff6ef6c71696e6cd5d (diff)
downloadruamel.yaml-591b1005706b0db167d5c55eefee7a0b0f0442e6.tar.gz
- set anchor explicitly (yaml_set_anchor)
- add before item comment (yaml_set_start_comment)
Diffstat (limited to 'py/comments.py')
-rw-r--r--py/comments.py37
1 files changed, 33 insertions, 4 deletions
diff --git a/py/comments.py b/py/comments.py
index 577b984..0db9457 100644
--- a/py/comments.py
+++ b/py/comments.py
@@ -104,6 +104,7 @@ class Anchor(object):
def __init__(self):
self.value = None
+ self.always_dump = False
class CommentedBase(object):
@property
@@ -137,6 +138,19 @@ class CommentedBase(object):
l[3].extend(comment[0])
l[2] = comment[0]
+ def yaml_set_start_comment(self, comment, indent=0):
+ """overwrites any preceding comment lines on an object
+ expects comment to be without `#` and possible have mutlple lines
+ """
+ from .error import Mark
+ from .tokens import CommentToken
+ pre_comments = self._yaml_get_pre_comment()
+ if comment[-1] == '\n':
+ comment = comment[:-1] # strip final newline if there
+ start_mark = Mark(None, None, None, indent, None, None)
+ for com in comment.split('\n'):
+ pre_comments.append(CommentToken('# ' + com + '\n', start_mark, None))
+
@property
def fa(self):
if not hasattr(self, Format.attrib):
@@ -182,12 +196,11 @@ class CommentedBase(object):
def yaml_anchor(self):
if not hasattr(self, Anchor.attrib):
return None
- return self.anchor.value
+ return self.anchor
- def set_yaml_anchor(self, value):
+ def yaml_set_anchor(self, value, always_dump=False):
self.anchor.value = value
-
-
+ self.anchor.always_dump = always_dump
class CommentedSeq(list, CommentedBase):
__slots__ = [Comment.attrib, ]
@@ -224,6 +237,14 @@ class CommentedSeq(list, CommentedBase):
column = self._yaml_get_columnX(sel_idx)
return column
+ def _yaml_get_pre_comment(self):
+ if self.ca.comment is None:
+ pre_comments = []
+ self.ca.comment = [None, pre_comments]
+ else:
+ pre_comments = self.ca.comment[1] = []
+ return pre_comments
+
class CommentedMap(ordereddict, CommentedBase):
__slots__ = [Comment.attrib, ]
@@ -272,6 +293,14 @@ class CommentedMap(ordereddict, CommentedBase):
column = self._yaml_get_columnX(sel_idx)
return column
+ def _yaml_get_pre_comment(self):
+ if self.ca.comment is None:
+ pre_comments = []
+ self.ca.comment = [None, pre_comments]
+ else:
+ pre_comments = self.ca.comment[1] = []
+ return pre_comments
+
def update(self, vals):
try:
ordereddict.update(self, vals)