Debian Codegraph @

strictyaml/exceptions.py

raw
1 from strictyaml.ruamel.error import MarkedYAMLError
2 from strictyaml.ruamel.dumper import RoundTripDumper
3 from strictyaml.ruamel import dump
4
5 try:
6 from strictyaml.ruamel.error import Mark as StringMark
7 except ImportError:
8 from strictyaml.ruamel.error import StringMark
9
10
11 class StrictYAMLError(MarkedYAMLError):
12 pass
13
14
15 class InvalidValidatorError(StrictYAMLError):
16 pass
17
18
19 class CannotBuildDocumentFromInvalidData(StrictYAMLError):
20 pass
21
22
23 class CannotBuildDocumentsFromEmptyDictOrList(StrictYAMLError):
24 pass
25
26
27 class YAMLSerializationError(StrictYAMLError):
28 pass
29
30
31 class InvalidOptionalDefault(YAMLSerializationError):
32 pass
33
34
35 class YAMLValidationError(StrictYAMLError):
36 def __init__(self, context, problem, chunk):
37 self.context = context
38 self.problem = problem
39 self._chunk = chunk
40 self.note = None
41
42 @property
43 def context_mark(self):
44 context_line = self._chunk.start_line() - 1
45 str_document = dump(self._chunk.whole_document, Dumper=RoundTripDumper)
46 context_index = len("\n".join(str_document.split("\n")[:context_line]))
47 return StringMark(
48 self._chunk.label,
49 context_index,
50 context_line,
51 0,
52 str_document,
53 context_index + 1,
54 )
55
56 @property
57 def problem_mark(self):
58 problem_line = self._chunk.end_line() - 1
59 str_document = dump(self._chunk.whole_document, Dumper=RoundTripDumper)
60 problem_index = len("\n".join(str_document.split("\n")[:problem_line]))
61 return StringMark(
62 self._chunk.label,
63 problem_index,
64 problem_line,
65 0,
66 str_document,
67 problem_index + 1,
68 )
69
70
71 class DisallowedToken(StrictYAMLError):
72 MESSAGE = "Disallowed token"
73
74
75 class TagTokenDisallowed(DisallowedToken):
76 MESSAGE = "Tag tokens not allowed"
77
78
79 class FlowMappingDisallowed(DisallowedToken):
80 MESSAGE = "Flow mapping tokens not allowed"
81
82
83 class AnchorTokenDisallowed(DisallowedToken):
84 MESSAGE = "Anchor tokens not allowed"
85
86
87 class DuplicateKeysDisallowed(DisallowedToken):
88 MESSAGE = "Duplicate keys not allowed"
89
90
91 class InconsistentIndentationDisallowed(DisallowedToken):
92 MESSAGE = "Inconsistent indentation not allowed"
93
94
95 def raise_type_error(yaml_object, to_type, alternatives):
96 raise TypeError(
97 ("Cannot cast {0} to {1}.\n" "Use {2} instead.").format(
98 repr(yaml_object), to_type, alternatives
99 )
100 )