Debian Codegraph @

strictyaml/ruamel/error.py

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