comparison env/lib/python3.7/site-packages/ruamel/yaml/error.py @ 5:9b1c78e6ba9c draft default tip

"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author shellac
date Mon, 01 Jun 2020 08:59:25 -0400
parents 79f47841a781
children
comparison
equal deleted inserted replaced
4:79f47841a781 5:9b1c78e6ba9c
1 # coding: utf-8
2
3 from __future__ import absolute_import
4
5 import warnings
6 import textwrap
7
8 from ruamel.yaml.compat import utf8
9
10 if False: # MYPY
11 from typing import Any, Dict, Optional, List, Text # NOQA
12
13
14 __all__ = [
15 'FileMark',
16 'StringMark',
17 'CommentMark',
18 'YAMLError',
19 'MarkedYAMLError',
20 'ReusedAnchorWarning',
21 'UnsafeLoaderWarning',
22 'MarkedYAMLWarning',
23 'MarkedYAMLFutureWarning',
24 ]
25
26
27 class StreamMark(object):
28 __slots__ = 'name', 'index', 'line', 'column'
29
30 def __init__(self, name, index, line, column):
31 # type: (Any, int, int, int) -> None
32 self.name = name
33 self.index = index
34 self.line = line
35 self.column = column
36
37 def __str__(self):
38 # type: () -> Any
39 where = ' in "%s", line %d, column %d' % (self.name, self.line + 1, self.column + 1)
40 return where
41
42 def __eq__(self, other):
43 if self.line != other.line or self.column != other.column:
44 return False
45 if self.name != other.name or self.index != other.index:
46 return False
47 return True
48
49 def __ne__(self, other):
50 return not self.__eq__(other)
51
52
53 class FileMark(StreamMark):
54 __slots__ = ()
55
56
57 class StringMark(StreamMark):
58 __slots__ = 'name', 'index', 'line', 'column', 'buffer', 'pointer'
59
60 def __init__(self, name, index, line, column, buffer, pointer):
61 # type: (Any, int, int, int, Any, Any) -> None
62 StreamMark.__init__(self, name, index, line, column)
63 self.buffer = buffer
64 self.pointer = pointer
65
66 def get_snippet(self, indent=4, max_length=75):
67 # type: (int, int) -> Any
68 if self.buffer is None: # always False
69 return None
70 head = ""
71 start = self.pointer
72 while start > 0 and self.buffer[start - 1] not in u'\0\r\n\x85\u2028\u2029':
73 start -= 1
74 if self.pointer - start > max_length / 2 - 1:
75 head = ' ... '
76 start += 5
77 break
78 tail = ""
79 end = self.pointer
80 while end < len(self.buffer) and self.buffer[end] not in u'\0\r\n\x85\u2028\u2029':
81 end += 1
82 if end - self.pointer > max_length / 2 - 1:
83 tail = ' ... '
84 end -= 5
85 break
86 snippet = utf8(self.buffer[start:end])
87 caret = '^'
88 caret = '^ (line: {})'.format(self.line + 1)
89 return (
90 ' ' * indent
91 + head
92 + snippet
93 + tail
94 + '\n'
95 + ' ' * (indent + self.pointer - start + len(head))
96 + caret
97 )
98
99 def __str__(self):
100 # type: () -> Any
101 snippet = self.get_snippet()
102 where = ' in "%s", line %d, column %d' % (self.name, self.line + 1, self.column + 1)
103 if snippet is not None:
104 where += ':\n' + snippet
105 return where
106
107
108 class CommentMark(object):
109 __slots__ = ('column',)
110
111 def __init__(self, column):
112 # type: (Any) -> None
113 self.column = column
114
115
116 class YAMLError(Exception):
117 pass
118
119
120 class MarkedYAMLError(YAMLError):
121 def __init__(
122 self,
123 context=None,
124 context_mark=None,
125 problem=None,
126 problem_mark=None,
127 note=None,
128 warn=None,
129 ):
130 # type: (Any, Any, Any, Any, Any, Any) -> None
131 self.context = context
132 self.context_mark = context_mark
133 self.problem = problem
134 self.problem_mark = problem_mark
135 self.note = note
136 # warn is ignored
137
138 def __str__(self):
139 # type: () -> Any
140 lines = [] # type: List[str]
141 if self.context is not None:
142 lines.append(self.context)
143 if self.context_mark is not None and (
144 self.problem is None
145 or self.problem_mark is None
146 or self.context_mark.name != self.problem_mark.name
147 or self.context_mark.line != self.problem_mark.line
148 or self.context_mark.column != self.problem_mark.column
149 ):
150 lines.append(str(self.context_mark))
151 if self.problem is not None:
152 lines.append(self.problem)
153 if self.problem_mark is not None:
154 lines.append(str(self.problem_mark))
155 if self.note is not None and self.note:
156 note = textwrap.dedent(self.note)
157 lines.append(note)
158 return '\n'.join(lines)
159
160
161 class YAMLStreamError(Exception):
162 pass
163
164
165 class YAMLWarning(Warning):
166 pass
167
168
169 class MarkedYAMLWarning(YAMLWarning):
170 def __init__(
171 self,
172 context=None,
173 context_mark=None,
174 problem=None,
175 problem_mark=None,
176 note=None,
177 warn=None,
178 ):
179 # type: (Any, Any, Any, Any, Any, Any) -> None
180 self.context = context
181 self.context_mark = context_mark
182 self.problem = problem
183 self.problem_mark = problem_mark
184 self.note = note
185 self.warn = warn
186
187 def __str__(self):
188 # type: () -> Any
189 lines = [] # type: List[str]
190 if self.context is not None:
191 lines.append(self.context)
192 if self.context_mark is not None and (
193 self.problem is None
194 or self.problem_mark is None
195 or self.context_mark.name != self.problem_mark.name
196 or self.context_mark.line != self.problem_mark.line
197 or self.context_mark.column != self.problem_mark.column
198 ):
199 lines.append(str(self.context_mark))
200 if self.problem is not None:
201 lines.append(self.problem)
202 if self.problem_mark is not None:
203 lines.append(str(self.problem_mark))
204 if self.note is not None and self.note:
205 note = textwrap.dedent(self.note)
206 lines.append(note)
207 if self.warn is not None and self.warn:
208 warn = textwrap.dedent(self.warn)
209 lines.append(warn)
210 return '\n'.join(lines)
211
212
213 class ReusedAnchorWarning(YAMLWarning):
214 pass
215
216
217 class UnsafeLoaderWarning(YAMLWarning):
218 text = """
219 The default 'Loader' for 'load(stream)' without further arguments can be unsafe.
220 Use 'load(stream, Loader=ruamel.yaml.Loader)' explicitly if that is OK.
221 Alternatively include the following in your code:
222
223 import warnings
224 warnings.simplefilter('ignore', ruamel.yaml.error.UnsafeLoaderWarning)
225
226 In most other cases you should consider using 'safe_load(stream)'"""
227 pass
228
229
230 warnings.simplefilter('once', UnsafeLoaderWarning)
231
232
233 class MantissaNoDotYAML1_1Warning(YAMLWarning):
234 def __init__(self, node, flt_str):
235 # type: (Any, Any) -> None
236 self.node = node
237 self.flt = flt_str
238
239 def __str__(self):
240 # type: () -> Any
241 line = self.node.start_mark.line
242 col = self.node.start_mark.column
243 return """
244 In YAML 1.1 floating point values should have a dot ('.') in their mantissa.
245 See the Floating-Point Language-Independent Type for YAML™ Version 1.1 specification
246 ( http://yaml.org/type/float.html ). This dot is not required for JSON nor for YAML 1.2
247
248 Correct your float: "{}" on line: {}, column: {}
249
250 or alternatively include the following in your code:
251
252 import warnings
253 warnings.simplefilter('ignore', ruamel.yaml.error.MantissaNoDotYAML1_1Warning)
254
255 """.format(
256 self.flt, line, col
257 )
258
259
260 warnings.simplefilter('once', MantissaNoDotYAML1_1Warning)
261
262
263 class YAMLFutureWarning(Warning):
264 pass
265
266
267 class MarkedYAMLFutureWarning(YAMLFutureWarning):
268 def __init__(
269 self,
270 context=None,
271 context_mark=None,
272 problem=None,
273 problem_mark=None,
274 note=None,
275 warn=None,
276 ):
277 # type: (Any, Any, Any, Any, Any, Any) -> None
278 self.context = context
279 self.context_mark = context_mark
280 self.problem = problem
281 self.problem_mark = problem_mark
282 self.note = note
283 self.warn = warn
284
285 def __str__(self):
286 # type: () -> Any
287 lines = [] # type: List[str]
288 if self.context is not None:
289 lines.append(self.context)
290
291 if self.context_mark is not None and (
292 self.problem is None
293 or self.problem_mark is None
294 or self.context_mark.name != self.problem_mark.name
295 or self.context_mark.line != self.problem_mark.line
296 or self.context_mark.column != self.problem_mark.column
297 ):
298 lines.append(str(self.context_mark))
299 if self.problem is not None:
300 lines.append(self.problem)
301 if self.problem_mark is not None:
302 lines.append(str(self.problem_mark))
303 if self.note is not None and self.note:
304 note = textwrap.dedent(self.note)
305 lines.append(note)
306 if self.warn is not None and self.warn:
307 warn = textwrap.dedent(self.warn)
308 lines.append(warn)
309 return '\n'.join(lines)