diff options
author | Anthon van der Neut <anthon@mnt.org> | 2015-04-18 20:08:03 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2015-04-18 20:08:03 +0200 |
commit | b667c482b096ab424c98faaadfe060b51919f133 (patch) | |
tree | f88cbcb69bddb1a4127e082695ccfa7885498c6f | |
parent | 0a2f20fd94daed9abd86470c5c3c5b526f47cdd7 (diff) | |
download | ruamel.yaml-b667c482b096ab424c98faaadfe060b51919f133.tar.gz |
added line and column to collection types
-rw-r--r-- | example/so_13517753.py | 54 | ||||
-rw-r--r-- | py/__init__.py | 2 | ||||
-rw-r--r-- | py/comments.py | 22 | ||||
-rw-r--r-- | py/composer.py | 3 | ||||
-rw-r--r-- | py/constructor.py | 4 | ||||
-rw-r--r-- | py/emitter.py | 6 | ||||
-rw-r--r-- | py/parser.py | 8 |
7 files changed, 86 insertions, 13 deletions
diff --git a/example/so_13517753.py b/example/so_13517753.py new file mode 100644 index 0000000..b83af86 --- /dev/null +++ b/example/so_13517753.py @@ -0,0 +1,54 @@ +from __future__ import print_function + +# import sys +import ruamel.yaml +from ruamel.yaml.comments import CommentedMap + +##class MyObj(): +## name = "boby" +## age = 34 +## +##print(ruamel.yaml.dump(MyObj())) # , Dumper=ruamel.yaml.RoundTripDumper), end='') +## +##inp = """\ +##boby: # this is the name +## age: 34 # in years +##""" +## +##print('====', ruamel.yaml.load(inp)) +## +## +##data1 = ruamel.yaml.load(inp, Loader=ruamel.yaml.RoundTripLoader) +##print('<<<', data1.ca.items) +##print(ruamel.yaml.dump(data1, Dumper=ruamel.yaml.RoundTripDumper), end='') +## +##print('----------------') + +class MyObj(): + name = "boby" + age = 34 + + def convert_to_yaml_struct(self): + x = CommentedMap() + a = CommentedMap() + x[data.name] = a + x.yaml_add_eol_comment('this is the name', 'boby', 11) + a['age'] = data.age + a.yaml_add_eol_comment('in years', 'age', 11) + print('>>>', x.ca.items) + return x + + @staticmethod + def yaml_representer(dumper, data, flow_style=False): + assert isinstance(dumper, ruamel.yaml.RoundTripDumper) + return dumper.represent_dict(data.convert_to_yaml_struct()) + + +ruamel.yaml.RoundTripDumper.add_representer(MyObj, MyObj.yaml_representer) + +data = MyObj() + + +print(ruamel.yaml.dump(data, Dumper=ruamel.yaml.RoundTripDumper), end='') + + diff --git a/py/__init__.py b/py/__init__.py index 2eb14d2..6f51d2e 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -21,7 +21,7 @@ def _convert_version(tup): return ret_val -version_info = (0, 8) +version_info = (0, 9) __version__ = _convert_version(version_info) del _convert_version diff --git a/py/comments.py b/py/comments.py index d39fd87..0d83d14 100644 --- a/py/comments.py +++ b/py/comments.py @@ -18,6 +18,8 @@ from .compat import ordereddict comment_attrib = '_yaml_comment' format_attrib = '_yaml_format' +line_col_attrib = '_yaml_line_col' + class Comment(object): # sys.getsize tested the Comment objects, __slots__ make them bigger @@ -88,6 +90,15 @@ class Format(object): return default return self._flow_style + +class LineCol(object): + attrib = line_col_attrib + + def __init__(self): + self.line = None + self.col = None + + class CommentedBase(object): @property def ca(self): @@ -126,7 +137,6 @@ class CommentedBase(object): setattr(self, Format.attrib, Format()) return getattr(self, Format.attrib) - def yaml_add_eol_comment(self, comment, key=NoComment, column=None): """ there is a problem as eol comments should start with ' #' @@ -147,6 +157,16 @@ class CommentedBase(object): ct = [CommentToken(comment, start_mark, None), None] self._yaml_add_eol_comment(ct, key=key) + @property + def lc(self): + if not hasattr(self, LineCol.attrib): + setattr(self, LineCol.attrib, LineCol()) + return getattr(self, LineCol.attrib) + + def _yaml_set_line_col(self, line, col): + self.lc.line = line + self.lc.col = col + class CommentedSeq(list, CommentedBase): __slots__ = [Comment.attrib, ] diff --git a/py/composer.py b/py/composer.py index 981c5ef..dd739dc 100644 --- a/py/composer.py +++ b/py/composer.py @@ -113,13 +113,10 @@ class Composer(object): start_event.start_mark, None, flow_style=start_event.flow_style, comment=start_event.comment) - #print('.................', node.comment) if anchor is not None: self.anchors[anchor] = node index = 0 - #print('...............0.', node.comment) while not self.check_event(SequenceEndEvent): - #print('...............1.', node.comment) node.value.append(self.compose_node(node, index)) index += 1 end_event = self.get_event() diff --git a/py/constructor.py b/py/constructor.py index 61fa8a7..4835e14 100644 --- a/py/constructor.py +++ b/py/constructor.py @@ -909,6 +909,7 @@ class RoundTripConstructor(SafeConstructor): def construct_yaml_seq(self, node): data = CommentedSeq() + data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) if node.flow_style is True: data.fa.set_flow_style() elif node.flow_style is False: @@ -920,6 +921,7 @@ class RoundTripConstructor(SafeConstructor): def construct_yaml_map(self, node): data = CommentedMap() + data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) if node.flow_style is True: data.fa.set_flow_style() elif node.flow_style is False: @@ -930,6 +932,7 @@ class RoundTripConstructor(SafeConstructor): def construct_yaml_omap(self, node): # Note: we do now check for duplicate keys omap = CommentedOrderedMap() + omap._yaml_set_line_col(node.start_mark.line, node.start_mark.column) if node.flow_style is True: omap.fa.set_flow_style() elif node.flow_style is False: @@ -970,6 +973,7 @@ class RoundTripConstructor(SafeConstructor): def construct_yaml_set(self, node): data = CommentedSet() + data._yaml_set_line_col(node.start_mark.line, node.start_mark.column) yield data self.construct_setting(node, data) diff --git a/py/emitter.py b/py/emitter.py index 54edae1..c5c0286 100644 --- a/py/emitter.py +++ b/py/emitter.py @@ -344,9 +344,9 @@ class Emitter(object): self.indent = self.indents.pop() self.flow_level -= 1 self.write_indicator(u'}', False) - #if self.event.comment and self.event.comment[0]: - # # eol comment on flow sequence - # self.write_post_comment(self.event) + # if self.event.comment and self.event.comment[0]: + # # eol comment on flow sequence + # self.write_post_comment(self.event) self.state = self.states.pop() else: if self.canonical or self.column > self.best_width: diff --git a/py/parser.py b/py/parser.py index bb90f2a..0afb596 100644 --- a/py/parser.py +++ b/py/parser.py @@ -377,10 +377,10 @@ class Parser(object): # indentation? pt = self.peek_token() comment = pt.comment - #print('pt0', type(pt)) + # print('pt0', type(pt)) if comment is None or comment[1] is None: comment = pt.split_comment() - #print('pt1', comment) + # print('pt1', comment) event = SequenceStartEvent( anchor, tag, implicit, start_mark, end_mark, flow_style=False, @@ -418,7 +418,7 @@ class Parser(object): def parse_block_sequence_first_entry(self): token = self.get_token() # move any comment from start token - #token.move_comment(self.peek_token()) + # token.move_comment(self.peek_token()) self.marks.append(token.start_mark) return self.parse_block_sequence_entry() @@ -426,8 +426,6 @@ class Parser(object): if self.check_token(BlockEntryToken): token = self.get_token() token.move_comment(self.peek_token()) - #print('================== here be dragons', repr(token)) - #print(token.comment) if not self.check_token(BlockEntryToken, BlockEndToken): self.states.append(self.parse_block_sequence_entry) return self.parse_block_node() |