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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# coding: utf-8
from __future__ import print_function, absolute_import, division, unicode_literals
from ruamel.yaml.compat import text_type
if False: # MYPY
from typing import Text, Any, Dict, List # NOQA
__all__ = [
'ScalarString',
'PreservedScalarString',
'SingleQuotedScalarString',
'DoubleQuotedScalarString',
]
class ScalarString(text_type):
__slots__ = ()
def __new__(cls, *args, **kw):
# type: (Any, Any) -> Any
return text_type.__new__(cls, *args, **kw) # type: ignore
def replace(self, old, new, maxreplace=-1):
# type: (Any, Any, int) -> Any
return type(self)((text_type.replace(self, old, new, maxreplace)))
class PreservedScalarString(ScalarString):
__slots__ = 'comment' # the comment after the | on the first line
style = '|'
def __new__(cls, value):
# type: (Text) -> Any
return ScalarString.__new__(cls, value)
class SingleQuotedScalarString(ScalarString):
__slots__ = ()
style = "'"
def __new__(cls, value):
# type: (Text) -> Any
return ScalarString.__new__(cls, value)
class DoubleQuotedScalarString(ScalarString):
__slots__ = ()
style = '"'
def __new__(cls, value):
# type: (Text) -> Any
return ScalarString.__new__(cls, value)
def preserve_literal(s):
# type: (Text) -> Text
return PreservedScalarString(s.replace('\r\n', '\n').replace('\r', '\n'))
def walk_tree(base):
# type: (Any) -> None
"""
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] # type: Text
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: # type: ignore
base[idx] = preserve_literal(elem) # type: ignore
else:
walk_tree(elem)
|