Mercurial > repos > shellac > guppy_basecaller
comparison env/lib/python3.7/site-packages/ruamel/yaml/scalarstring.py @ 2:6af9afd405e9 draft
"planemo upload commit 0a63dd5f4d38a1f6944587f52a8cd79874177fc1"
author | shellac |
---|---|
date | Thu, 14 May 2020 14:56:58 -0400 |
parents | 26e78fe6e8c4 |
children |
comparison
equal
deleted
inserted
replaced
1:75ca89e9b81c | 2:6af9afd405e9 |
---|---|
1 # coding: utf-8 | |
2 | |
3 from __future__ import print_function, absolute_import, division, unicode_literals | |
4 | |
5 from ruamel.yaml.compat import text_type | |
6 from ruamel.yaml.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 = ruamel.yaml.compat.ordereddict | |
128 map['\n'] = preserve_literal | |
129 map[':'] = SingleQuotedScalarString | |
130 walk_tree(data, map=map) | |
131 """ | |
132 from ruamel.yaml.compat import string_types, MutableMapping, MutableSequence | |
133 | |
134 if map is None: | |
135 map = {'\n': preserve_literal} | |
136 | |
137 if isinstance(base, MutableMapping): | |
138 for k in base: | |
139 v = base[k] # type: Text | |
140 if isinstance(v, string_types): | |
141 for ch in map: | |
142 if ch in v: | |
143 base[k] = map[ch](v) | |
144 break | |
145 else: | |
146 walk_tree(v) | |
147 elif isinstance(base, MutableSequence): | |
148 for idx, elem in enumerate(base): | |
149 if isinstance(elem, string_types): | |
150 for ch in map: | |
151 if ch in elem: # type: ignore | |
152 base[idx] = map[ch](elem) | |
153 break | |
154 else: | |
155 walk_tree(elem) |