Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tomlkit/exceptions.py: 86%
92 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:01 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:01 +0000
1from typing import Collection
2from typing import Optional
5class TOMLKitError(Exception):
7 pass
10class ParseError(ValueError, TOMLKitError):
11 """
12 This error occurs when the parser encounters a syntax error
13 in the TOML being parsed. The error references the line and
14 location within the line where the error was encountered.
15 """
17 def __init__(self, line: int, col: int, message: Optional[str] = None) -> None:
18 self._line = line
19 self._col = col
21 if message is None:
22 message = "TOML parse error"
24 super().__init__(f"{message} at line {self._line} col {self._col}")
26 @property
27 def line(self):
28 return self._line
30 @property
31 def col(self):
32 return self._col
35class MixedArrayTypesError(ParseError):
36 """
37 An array was found that had two or more element types.
38 """
40 def __init__(self, line: int, col: int) -> None:
41 message = "Mixed types found in array"
43 super().__init__(line, col, message=message)
46class InvalidNumberError(ParseError):
47 """
48 A numeric field was improperly specified.
49 """
51 def __init__(self, line: int, col: int) -> None:
52 message = "Invalid number"
54 super().__init__(line, col, message=message)
57class InvalidDateTimeError(ParseError):
58 """
59 A datetime field was improperly specified.
60 """
62 def __init__(self, line: int, col: int) -> None:
63 message = "Invalid datetime"
65 super().__init__(line, col, message=message)
68class InvalidDateError(ParseError):
69 """
70 A date field was improperly specified.
71 """
73 def __init__(self, line: int, col: int) -> None:
74 message = "Invalid date"
76 super().__init__(line, col, message=message)
79class InvalidTimeError(ParseError):
80 """
81 A date field was improperly specified.
82 """
84 def __init__(self, line: int, col: int) -> None:
85 message = "Invalid time"
87 super().__init__(line, col, message=message)
90class InvalidNumberOrDateError(ParseError):
91 """
92 A numeric or date field was improperly specified.
93 """
95 def __init__(self, line: int, col: int) -> None:
96 message = "Invalid number or date format"
98 super().__init__(line, col, message=message)
101class InvalidUnicodeValueError(ParseError):
102 """
103 A unicode code was improperly specified.
104 """
106 def __init__(self, line: int, col: int) -> None:
107 message = "Invalid unicode value"
109 super().__init__(line, col, message=message)
112class UnexpectedCharError(ParseError):
113 """
114 An unexpected character was found during parsing.
115 """
117 def __init__(self, line: int, col: int, char: str) -> None:
118 message = f"Unexpected character: {repr(char)}"
120 super().__init__(line, col, message=message)
123class EmptyKeyError(ParseError):
124 """
125 An empty key was found during parsing.
126 """
128 def __init__(self, line: int, col: int) -> None:
129 message = "Empty key"
131 super().__init__(line, col, message=message)
134class EmptyTableNameError(ParseError):
135 """
136 An empty table name was found during parsing.
137 """
139 def __init__(self, line: int, col: int) -> None:
140 message = "Empty table name"
142 super().__init__(line, col, message=message)
145class InvalidCharInStringError(ParseError):
146 """
147 The string being parsed contains an invalid character.
148 """
150 def __init__(self, line: int, col: int, char: str) -> None:
151 message = f"Invalid character {repr(char)} in string"
153 super().__init__(line, col, message=message)
156class UnexpectedEofError(ParseError):
157 """
158 The TOML being parsed ended before the end of a statement.
159 """
161 def __init__(self, line: int, col: int) -> None:
162 message = "Unexpected end of file"
164 super().__init__(line, col, message=message)
167class InternalParserError(ParseError):
168 """
169 An error that indicates a bug in the parser.
170 """
172 def __init__(self, line: int, col: int, message: Optional[str] = None) -> None:
173 msg = "Internal parser error"
174 if message:
175 msg += f" ({message})"
177 super().__init__(line, col, message=msg)
180class NonExistentKey(KeyError, TOMLKitError):
181 """
182 A non-existent key was used.
183 """
185 def __init__(self, key):
186 message = f'Key "{key}" does not exist.'
188 super().__init__(message)
191class KeyAlreadyPresent(TOMLKitError):
192 """
193 An already present key was used.
194 """
196 def __init__(self, key):
197 key = getattr(key, "key", key)
198 message = f'Key "{key}" already exists.'
200 super().__init__(message)
203class InvalidControlChar(ParseError):
204 def __init__(self, line: int, col: int, char: int, type: str) -> None:
205 display_code = "\\u00"
207 if char < 16:
208 display_code += "0"
210 display_code += hex(char)[2:]
212 message = (
213 "Control characters (codes less than 0x1f and 0x7f)"
214 f" are not allowed in {type}, "
215 f"use {display_code} instead"
216 )
218 super().__init__(line, col, message=message)
221class InvalidStringError(ValueError, TOMLKitError):
222 def __init__(self, value: str, invalid_sequences: Collection[str], delimiter: str):
223 repr_ = repr(value)[1:-1]
224 super().__init__(
225 f"Invalid string: {delimiter}{repr_}{delimiter}. "
226 f"The character sequences {invalid_sequences} are invalid."
227 )