summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnthon van der Neut <anthon@mnt.org>2014-11-24 10:14:03 +0100
committerAnthon van der Neut <anthon@mnt.org>2014-11-24 10:14:03 +0100
commit229163818f6668e78f888d5e976156206f23e346 (patch)
tree7e3aeeb631eeb398345cd7827595c55a7e8e3d50
parentccdbd3484aad7c22a43dcbc68d97ae87407b0c0c (diff)
downloadruamel.yaml-229163818f6668e78f888d5e976156206f23e346.tar.gz
- fix value comment occuring as on previous line (looking like eol comment)
- parse ini in yaml tests ok
-rw-r--r--py/emitter.py2
-rw-r--r--py/scanner.py9
-rw-r--r--py/yaml.py77
-rw-r--r--test/test_comments.py1
-rw-r--r--test/test_util.py124
-rw-r--r--test/test_z_data.py4
6 files changed, 128 insertions, 89 deletions
diff --git a/py/emitter.py b/py/emitter.py
index 8408b04..85d93b2 100644
--- a/py/emitter.py
+++ b/py/emitter.py
@@ -267,6 +267,8 @@ class Emitter(object):
if self.event.comment:
self.write_post_comment(self.event)
if self.event.comment and self.event.comment[1]:
+ if self.column != 0:
+ self.write_line_break()
self.write_pre_comment(self.event)
if self.flow_level or self.canonical or self.event.flow_style \
or self.check_empty_mapping():
diff --git a/py/scanner.py b/py/scanner.py
index d231cf5..94d4c49 100644
--- a/py/scanner.py
+++ b/py/scanner.py
@@ -1,4 +1,5 @@
from __future__ import absolute_import
+from __future__ import print_function
# Scanner produces tokens of the following types:
# STREAM-START
@@ -1529,7 +1530,7 @@ class RoundTripScanner(Scanner):
if isinstance(self.tokens[0], CommentToken):
comment = self.tokens.pop(0)
self.tokens_taken += 1
- # print 'dropping', comment
+ # print('################ dropping', comment)
comments.append(comment)
while self.need_more_tokens():
self.fetch_more_tokens()
@@ -1541,9 +1542,9 @@ class RoundTripScanner(Scanner):
# print 'dropping2', comment
comments.append(comment)
if len(comments) >= 1:
- # print ' len', len(comments), comments
- # print ' com', comments[0], comments[0].start_mark.line
- # print ' tok', self.tokens[0].end_mark.line
+ # print(' len', len(comments), comments)
+ # print(' com', comments[0], comments[0].start_mark.line)
+ # print(' tok', self.tokens[0].end_mark.line)
self.tokens[0].add_pre_comments(comments)
# pull in post comment on e.g. ':'
if not self.done and len(self.tokens) < 2:
diff --git a/py/yaml.py b/py/yaml.py
index 818bbe4..b379e8f 100644
--- a/py/yaml.py
+++ b/py/yaml.py
@@ -20,7 +20,6 @@ import ruamel.yaml
from ruamel.yaml.compat import ordereddict
-
class YAML:
def __init__(self, args, config):
self._args = args
@@ -28,54 +27,23 @@ class YAML:
def from_ini(self):
from configobj import ConfigObj
- x = dedent("""
- # initial comment
- keyword1 = value1
- keyword2 = value2 # eol comment kw2
-
- [section 1]
- keyword1 = value1 #
- # comment s1kw2
- keyword2 = value2 # eol comment s1kw2
-
- [[sub-section]] # eol on section
- # this is in section 1
- keyword1 = value1
- keyword2 = value2
-
- [[[nested section]]] #
- # this is in sub section
- keyword1 = value1
- keyword2 = value2
-
- [[sub-section2]] #
- # this is in section 1 again
- keyword1 = value1
- keyword2 = value2
-
- [[sub-section3]] #
- # this is also in section 1, indentation is misleading here
- keyword1 = value1
- keyword2 = value2
-
- # final comment
- """)
- cfg = ConfigObj(x.splitlines())
- print(cfg)
+ errors = 0
doc = []
+ cfg = ConfigObj(open(self._args.file))
+ # print(cfg)
for line in self.walk_configobj(cfg):
if not line.strip():
continue
- print(line)
+ # print(line)
doc.append(line)
- print('--------------')
- joined = '\n'.join(doc) + '\n'
+ # print('--------------')
+ joined = '\n'.join(doc)
rto = self.round_trip_single(joined)
- print(rto)
- print()
- if rto != joined:
- self.diff(joined, rto, "test.ini")
- return 0
+ print(rto, end='') # already has eol at eof
+ # print()
+ # if rto != joined:
+ # self.diff(joined, rto, "test.ini")
+ return 1 if errors else 0
def test(self):
def print_input(input):
@@ -86,8 +54,8 @@ class YAML:
print('Tokens ' + '#' * 60)
tokens = ruamel.yaml.scan(input, ruamel.yaml.RoundTripLoader)
for idx, token in enumerate(tokens):
- #print(token.start_mark)
- #print(token.end_mark)
+ # print(token.start_mark)
+ # print(token.end_mark)
print("{0:2} {1}".format(idx, token))
def rt_events(input):
@@ -107,14 +75,6 @@ class YAML:
events = ruamel.yaml.parse(input, ruamel.yaml.RoundTripLoader)
for idx, event in enumerate(events):
print("{0:2} {1}".format(idx, event))
- #for event in events:
- #number += 1
- # cls = event.__class__
- #print(cls)
- #if (cls, -1) in substitutions:
- # markers.append([event.start_mark.index, +1, number, substitutions[cls, -1]])
- #if (cls, +1) in substitutions:
- # markers.append([event.end_mark.index, -1, number, substitutions[cls, +1]])
def print_nodes(input):
print('Nodes ' + '#' * 60)
@@ -131,9 +91,9 @@ class YAML:
print_input(input)
print_tokens(input)
print_events(input)
- #rt_events(input)
+ # rt_events(input)
print_nodes(input)
- #rt_nodes(input)
+ # rt_nodes(input)
data = ruamel.yaml.load(input, ruamel.yaml.RoundTripLoader)
print('data', data)
@@ -148,10 +108,10 @@ class YAML:
stream = io.StringIO()
dumper = ruamel.yaml.RoundTripDumper
print('>>>>>>>>>>')
- #print ruamel.yaml.dump(data, default_flow_style=False, Dumper=dumper), '==========='
+ # print(ruamel.yaml.dump(data, default_flow_style=False,
+ # Dumper=dumper), '===========')
print(ruamel.yaml.dump(data, Dumper=dumper)+'===========')
-
# test end
@staticmethod
@@ -249,7 +209,6 @@ class YAML:
if strip_trailing_space and line[:4] in ['--- ', '+++ ']:
line = line.rstrip() + '\n'
sys.stdout.write(line)
- #sys.stdout.writelines(diff)
def to_stdout(*args):
@@ -315,7 +274,7 @@ class YAML_Cmd(ProgramBase):
help='convert .ini/config to block YAML',
description='convert .ini/config to block YAML',
)
- @option('file', nargs='+')
+ @option('file')
def ini(self, yaml):
return yaml.from_ini()
diff --git a/test/test_comments.py b/test/test_comments.py
index 4a19db1..d72da63 100644
--- a/test/test_comments.py
+++ b/test/test_comments.py
@@ -144,7 +144,6 @@ class TestComments:
key1: val1
""")
- @pytest.mark.skipif(True, reason="debugging")
def test_map_in_map_1(self):
# comment is moved from value to key
round_trip("""
diff --git a/test/test_util.py b/test/test_util.py
index 0137ea8..612d2d8 100644
--- a/test/test_util.py
+++ b/test/test_util.py
@@ -6,6 +6,7 @@ import sys
import subprocess
try:
_ = subprocess.check_output
+
def check_output(*args, **kw):
try:
res = subprocess.check_output(*args, **kw)
@@ -16,22 +17,12 @@ try:
res = res.decode('utf-8')
return res
except AttributeError:
- # https://gist.github.com/edufelipe/1027906
def check_output(*args, **kw):
process = subprocess.Popen(stdout=subprocess.PIPE, *args, **kw)
output, unused_err = process.communicate()
if PY3:
output = output.decode('utf-8')
return output
- # retcode = process.poll()
- # if retcode:
- # cmd = kw.get("args")
- # if cmd is None:
- # cmd = args[0]
- # error = subprocess.CalledProcessError(retcode, cmd)
- # error.output = output
- # raise error
- # return output
import pytest
@@ -49,9 +40,11 @@ def call_util(s, file_name, cmd, mp, td):
res = check_output(cmd, stderr=subprocess.STDOUT)
return res
+
def rt_test(s, file_name, mp, td):
return call_util(s, file_name, ['yaml', 'rt', "-v", file_name], mp, td)
+
class TestUtil:
def test_version(self, capsys):
@@ -75,7 +68,6 @@ class TestUtil:
- ghi # some comment
- klm
""", file_name, mp=monkeypatch, td=tmpdir)
- #print(res)
assert res == dedent("""
{file_name}:
stabelizes on second round trip, ok without comments
@@ -88,12 +80,12 @@ class TestUtil:
- klm
""").format(**dict(file_name=file_name))
- @pytest.mark.skipif(not ruamel.yaml.__with_libyaml__,
- reason="better walker needed that keeps parent list")
def test_from_configobj(self, tmpdir, monkeypatch):
- from configobj import ConfigObj
- x = dedent("""
+ file_name = "02_from_ini.yaml"
+ res = call_util(
+ u"""
# initial comment
+
keyword1 = value1
keyword2 = value2
@@ -122,12 +114,98 @@ class TestUtil:
keyword2 = value2
# final comment
+ """, file_name, ['yaml', 'from-ini', file_name],
+ mp=monkeypatch, td=tmpdir)
+ print(res)
+ assert res == dedent("""
+ # initial comment
+ keyword1: value1
+ keyword2: value2
+ section 1:
+ keyword1: value1
+ keyword2: value2
+ sub-section:
+ # this is in section 1
+ keyword1: value1
+ keyword2: value2
+ nested section:
+ # this is in sub section
+ keyword1: value1
+ keyword2: value2
+ sub-section2:
+ # this is in section 1 again
+ keyword1: value1
+ keyword2: value2
+ sub-section3:
+ # this is also in section 1, indentation is misleading here
+ keyword1: value1
+ keyword2: value2
+ # final comment
+ """)
+
+ def test_from_configobj_extra_comments(self, tmpdir, monkeypatch):
+ file_name = "02_from_ini.yaml"
+ res = call_util(
+ u"""
+ # initial comment
+ keyword1 = value1
+ keyword2 = value2 # eol comment kw2
+
+ [section 1]
+ keyword1 = value1 # and here more comment
+ # comment s1kw2
+ keyword2 = value2 # eol comment s1kw2
+
+ [[sub-section]] # eol on section
+ # this is in section 1
+ keyword1 = value1
+ keyword2 = value2
+
+ [[[nested section]]]
+ # this is in sub section
+ keyword1 = value1
+ keyword2 = value2
+ # after nested
+
+ [[sub-section2]]
+ # this is in section 1 again
+ keyword1 = value1
+ keyword2 = value2
+
+ [[sub-section3]] # comment on section key
+ # this is also in section 1, indentation is misleading here
+ keyword1 = value1
+ keyword2 = value2
+
+ # final comment
+ """,
+ file_name, ['yaml', 'from-ini', file_name],
+ mp=monkeypatch, td=tmpdir)
+ print(res)
+ assert res == dedent("""
+ # initial comment
+ keyword1: value1
+ keyword2: value2 # eol comment kw2
+ section 1:
+ keyword1: value1 # and here more comment
+ # comment s1kw2
+ keyword2: value2 # eol comment s1kw2
+ sub-section: # eol on section
+ # this is in section 1
+ keyword1: value1
+ keyword2: value2
+ nested section:
+ # this is in sub section
+ keyword1: value1
+ keyword2: value2
+ # after nested
+ sub-section2:
+ # this is in section 1 again
+ keyword1: value1
+ keyword2: value2
+ sub-section3: # comment on section key
+ # this is also in section 1, indentation is misleading here
+ keyword1: value1
+ keyword2: value2
+ # final comment
""")
- cfg = ConfigObj(x.splitlines())
- print(cfg)
- def doit(section, key):
- print('section {0}, key {1}, value {}'.format(
- section.name, key, section[key]))
- for z in cfg.walk(doit):
- pass
- assert False \ No newline at end of file
diff --git a/test/test_z_data.py b/test/test_z_data.py
index cd8c8f7..1f785ba 100644
--- a/test/test_z_data.py
+++ b/test/test_z_data.py
@@ -20,8 +20,8 @@ def test_data():
test_appliance.run(collections, args)
-#@pytest.mark.skipif(not ruamel.yaml.__with_libyaml__,
-# reason="no libyaml")
+# @pytest.mark.skipif(not ruamel.yaml.__with_libyaml__,
+# reason="no libyaml")
def test_data_ext():
collections = []
if ruamel.yaml.__with_libyaml__: