diff options
author | Anthon van der Neut <anthon@mnt.org> | 2015-08-28 08:03:59 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2015-08-28 08:03:59 +0200 |
commit | 386029ed647ca7fd0209506285f02365c347eef9 (patch) | |
tree | 78263b913d9dbe9bf7c8a3c04379f508bff56a23 /configobjwalker.py | |
parent | 04e651b6b062edbdf66271847d3dde06550adbb4 (diff) | |
download | ruamel.yaml-386029ed647ca7fd0209506285f02365c347eef9.tar.gz |
- main problem in moving stuff from yaml/py to yaml was that
parser.py clashes with built-in parser module (CPython, C-module)
which is inlucded from pkg_resources/__init__.py
- no C compile yet
Diffstat (limited to 'configobjwalker.py')
-rw-r--r-- | configobjwalker.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/configobjwalker.py b/configobjwalker.py new file mode 100644 index 0000000..576adcd --- /dev/null +++ b/configobjwalker.py @@ -0,0 +1,65 @@ + + +def configobj_walker(cfg): + """ + walks over a ConfigObj (INI file with comments) generating + corresponding YAML output (including comments + """ + from configobj import ConfigObj + assert isinstance(cfg, ConfigObj) + for c in cfg.initial_comment: + if c.strip(): + yield c + for s in _walk_section(cfg): + if s.strip(): + yield s + for c in cfg.final_comment: + if c.strip(): + yield c + + +def _walk_section(s, level=0): + from configobj import Section + assert isinstance(s, Section) + indent = u' ' * level + for name in s.scalars: + for c in s.comments[name]: + yield indent + c.strip() + x = s[name] + if u'\n' in x: + i = indent + u' ' + x = u'|\n' + i + x.strip().replace(u'\n', u'\n' + i) + elif ':' in x: + x = u"'" + x.replace(u"'", u"''") + u"'" + line = u'{0}{1}: {2}'.format(indent, name, x) + c = s.inline_comments[name] + if c: + line += u' ' + c + yield line + for name in s.sections: + for c in s.comments[name]: + yield indent + c.strip() + line = u'{0}{1}:'.format(indent, name) + c = s.inline_comments[name] + if c: + line += u' ' + c + yield line + for val in _walk_section(s[name], level=level+1): + yield val + +##def config_obj_2_rt_yaml(cfg): +## from .comments import CommentedMap, CommentedSeq +## from configobj import ConfigObj +## assert isinstance(cfg, ConfigObj) +## #for c in cfg.initial_comment: +## # if c.strip(): +## # pass +## cm = CommentedMap() +## for name in s.sections: +## cm[name] = d = CommentedMap() +## +## +## #for c in cfg.final_comment: +## # if c.strip(): +## # yield c +## return cm |