summaryrefslogtreecommitdiff
path: root/scalarstring.py
blob: 38854250bd2b7c7effe40d6080210f88ca1bff8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# coding: utf-8

from __future__ import absolute_import
from __future__ import print_function

__all__ = ["ScalarString", "PreservedScalarString"]

try:
    from .compat import text_type
except (ImportError, ValueError):  # for Jython
    from ruamel.yaml.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)