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 /scalarstring.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 'scalarstring.py')
-rw-r--r-- | scalarstring.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/scalarstring.py b/scalarstring.py new file mode 100644 index 0000000..a628396 --- /dev/null +++ b/scalarstring.py @@ -0,0 +1,44 @@ +from __future__ import absolute_import +from __future__ import print_function + +__all__ = ["ScalarString", "PreservedScalarString"] + +from .compat import text_type + + +class ScalarString(text_type): + def __new__(cls, *args, **kw): + return text_type.__new__(cls, *args, **kw) + + +class PreservedScalarString(ScalarString): + def __new__(cls, value): + return ScalarString.__new__(cls, value) + +def preserve_literal(s): + return PreservedScalarString(s.replace('\r\n', '\n').replace('\r', '\n')) + + +def walk_tree(base): + """ + the routine here walks over a simple yaml tree (recursing in + dict values and list items) and converts strings that + have multiple lines to literal scalars + """ + from ruamel.yaml.compat import string_types + + + if isinstance(base, dict): + for k in base: + v = base[k] + if isinstance(v, string_types) and '\n' in v: + base[k] = preserve_literal(v) + else: + walk_tree(v) + elif isinstance(base, list): + for idx, elem in enumerate(base): + if isinstance(elem, string_types) and '\n' in elem: + print(elem) + base[idx] = preserve_literal(elem) + else: + walk_tree(elem) |