| 1 |
# coding: utf-8 |
| 2 |
|
| 3 |
from __future__ import print_function, absolute_import, division, unicode_literals |
| 4 |
|
| 5 |
from strictyaml.ruamel.compat import text_type |
| 6 |
from strictyaml.ruamel.anchor import Anchor |
| 7 |
|
| 8 |
if False: # MYPY |
| 9 |
from typing import Text, Any, Dict, List # NOQA |
| 10 |
|
| 11 |
__all__ = [ |
| 12 |
"ScalarString", |
| 13 |
"LiteralScalarString", |
| 14 |
"FoldedScalarString", |
| 15 |
"SingleQuotedScalarString", |
| 16 |
"DoubleQuotedScalarString", |
| 17 |
"PlainScalarString", |
| 18 |
# PreservedScalarString is the old name, as it was the first to be preserved on rt, |
| 19 |
# use LiteralScalarString instead |
| 20 |
"PreservedScalarString", |
| 21 |
] |
| 22 |
|
| 23 |
|
| 24 |
class ScalarString(text_type): |
| 25 |
__slots__ = Anchor.attrib |
| 26 |
|
| 27 |
def __new__(cls, *args, **kw): |
| 28 |
# type: (Any, Any) -> Any |
| 29 |
anchor = kw.pop("anchor", None) # type: ignore |
| 30 |
ret_val = text_type.__new__(cls, *args, **kw) # type: ignore |
| 31 |
if anchor is not None: |
| 32 |
ret_val.yaml_set_anchor(anchor, always_dump=True) |
| 33 |
return ret_val |
| 34 |
|
| 35 |
def replace(self, old, new, maxreplace=-1): |
| 36 |
# type: (Any, Any, int) -> Any |
| 37 |
return type(self)((text_type.replace(self, old, new, maxreplace))) |
| 38 |
|
| 39 |
@property |
| 40 |
def anchor(self): |
| 41 |
# type: () -> Any |
| 42 |
if not hasattr(self, Anchor.attrib): |
| 43 |
setattr(self, Anchor.attrib, Anchor()) |
| 44 |
return getattr(self, Anchor.attrib) |
| 45 |
|
| 46 |
def yaml_anchor(self, any=False): |
| 47 |
# type: (bool) -> Any |
| 48 |
if not hasattr(self, Anchor.attrib): |
| 49 |
return None |
| 50 |
if any or self.anchor.always_dump: |
| 51 |
return self.anchor |
| 52 |
return None |
| 53 |
|
| 54 |
def yaml_set_anchor(self, value, always_dump=False): |
| 55 |
# type: (Any, bool) -> None |
| 56 |
self.anchor.value = value |
| 57 |
self.anchor.always_dump = always_dump |
| 58 |
|
| 59 |
|
| 60 |
class LiteralScalarString(ScalarString): |
| 61 |
__slots__ = "comment" # the comment after the | on the first line |
| 62 |
|
| 63 |
style = "|" |
| 64 |
|
| 65 |
def __new__(cls, value, anchor=None): |
| 66 |
# type: (Text, Any) -> Any |
| 67 |
return ScalarString.__new__(cls, value, anchor=anchor) |
| 68 |
|
| 69 |
|
| 70 |
PreservedScalarString = LiteralScalarString |
| 71 |
|
| 72 |
|
| 73 |
class FoldedScalarString(ScalarString): |
| 74 |
__slots__ = ("fold_pos", "comment") # the comment after the > on the first line |
| 75 |
|
| 76 |
style = ">" |
| 77 |
|
| 78 |
def __new__(cls, value, anchor=None): |
| 79 |
# type: (Text, Any) -> Any |
| 80 |
return ScalarString.__new__(cls, value, anchor=anchor) |
| 81 |
|
| 82 |
|
| 83 |
class SingleQuotedScalarString(ScalarString): |
| 84 |
__slots__ = () |
| 85 |
|
| 86 |
style = "'" |
| 87 |
|
| 88 |
def __new__(cls, value, anchor=None): |
| 89 |
# type: (Text, Any) -> Any |
| 90 |
return ScalarString.__new__(cls, value, anchor=anchor) |
| 91 |
|
| 92 |
|
| 93 |
class DoubleQuotedScalarString(ScalarString): |
| 94 |
__slots__ = () |
| 95 |
|
| 96 |
style = '"' |
| 97 |
|
| 98 |
def __new__(cls, value, anchor=None): |
| 99 |
# type: (Text, Any) -> Any |
| 100 |
return ScalarString.__new__(cls, value, anchor=anchor) |
| 101 |
|
| 102 |
|
| 103 |
class PlainScalarString(ScalarString): |
| 104 |
__slots__ = () |
| 105 |
|
| 106 |
style = "" |
| 107 |
|
| 108 |
def __new__(cls, value, anchor=None): |
| 109 |
# type: (Text, Any) -> Any |
| 110 |
return ScalarString.__new__(cls, value, anchor=anchor) |
| 111 |
|
| 112 |
|
| 113 |
def preserve_literal(s): |
| 114 |
# type: (Text) -> Text |
| 115 |
return LiteralScalarString(s.replace("\r\n", "\n").replace("\r", "\n")) |
| 116 |
|
| 117 |
|
| 118 |
def walk_tree(base, map=None): |
| 119 |
# type: (Any, Any) -> None |
| 120 |
""" |
| 121 |
the routine here walks over a simple yaml tree (recursing in |
| 122 |
dict values and list items) and converts strings that |
| 123 |
have multiple lines to literal scalars |
| 124 |
|
| 125 |
You can also provide an explicit (ordered) mapping for multiple transforms |
| 126 |
(first of which is executed): |
| 127 |
map = strictyaml.ruamel.compat.ordereddict |
| 128 |
map['\n'] = preserve_literal |
| 129 |
map[':'] = SingleQuotedScalarString |
| 130 |
walk_tree(data, map=map) |
| 131 |
""" |
| 132 |
from strictyaml.ruamel.compat import string_types |
| 133 |
from strictyaml.ruamel.compat import MutableMapping, MutableSequence # type: ignore |
| 134 |
|
| 135 |
if map is None: |
| 136 |
map = {"\n": preserve_literal} |
| 137 |
|
| 138 |
if isinstance(base, MutableMapping): |
| 139 |
for k in base: |
| 140 |
v = base[k] # type: Text |
| 141 |
if isinstance(v, string_types): |
| 142 |
for ch in map: |
| 143 |
if ch in v: |
| 144 |
base[k] = map[ch](v) |
| 145 |
break |
| 146 |
else: |
| 147 |
walk_tree(v, map=map) |
| 148 |
elif isinstance(base, MutableSequence): |
| 149 |
for idx, elem in enumerate(base): |
| 150 |
if isinstance(elem, string_types): |
| 151 |
for ch in map: |
| 152 |
if ch in elem: # type: ignore |
| 153 |
base[idx] = map[ch](elem) |
| 154 |
break |
| 155 |
else: |
| 156 |
walk_tree(elem, map=map) |