pytermgui.exceptions
Custom Exception-s used in pytermgui.
1""" 2Custom Exception-s used in pytermgui. 3""" 4 5from __future__ import annotations 6 7from dataclasses import dataclass, field 8 9__all__ = [ 10 "WidthExceededError", 11 "LineLengthError", 12 "AnsiSyntaxError", 13 "MarkupSyntaxError", 14] 15 16 17class TimeoutException(Exception): 18 """Raised when an action has timed out.""" 19 20 21class WidthExceededError(Exception): 22 """Raised when an element's width is larger than the screen.""" 23 24 25class LineLengthError(Exception): 26 """Raised when a widget line is not the expected length.""" 27 28 29class ColorSyntaxError(Exception): 30 """Raised when a color string could not be parsed into a `pytermgui.colors.Color`""" 31 32 33@dataclass 34class ParserSyntaxError(Exception): 35 """Parent exception for unparsable strings. 36 37 This exception takes some basic parameters, and formats 38 a message depending on the _delimiters value. This has to 39 be supplied by each child, while the rest of the arguments 40 are to be given at construction.""" 41 42 tag: str 43 cause: str 44 context: str 45 _delimiters: tuple[str, str] = field(init=False) 46 47 @property 48 def message(self) -> str: 49 """Create message from tag, context and cause.""" 50 51 start, end = self._delimiters 52 full = start + self.tag + end 53 54 escaped_context = ascii(self.context).strip("'")[:50] 55 highlighted = escaped_context.replace( 56 full, "\x1b[31m\x1b[1m" + full + "\x1b[0m", 1 57 ) 58 59 return f'Tag "{start}{self.tag}{end}" in string "{highlighted}" {self.cause}.' 60 61 def escape_message(self) -> str: 62 """Return message with markup tags escaped.""" 63 64 char = self._delimiters[0] 65 return self.message.replace(char, "\\" + char) 66 67 def __str__(self) -> str: 68 """Show message.""" 69 70 return self.message 71 72 73class MarkupSyntaxError(ParserSyntaxError): 74 """Raised when parsed markup text contains an error.""" 75 76 _delimiters = ("[", "]") 77 78 79class AnsiSyntaxError(ParserSyntaxError): 80 """Raised when parsed ANSI text contains an error.""" 81 82 _delimiters = ("\\x1b[", "m")
class
WidthExceededError(builtins.Exception):
22class WidthExceededError(Exception): 23 """Raised when an element's width is larger than the screen."""
Raised when an element's width is larger than the screen.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args
class
LineLengthError(builtins.Exception):
Raised when a widget line is not the expected length.
Inherited Members
- builtins.Exception
- Exception
- builtins.BaseException
- with_traceback
- args
80class AnsiSyntaxError(ParserSyntaxError): 81 """Raised when parsed ANSI text contains an error.""" 82 83 _delimiters = ("\\x1b[", "m")
Raised when parsed ANSI text contains an error.
Inherited Members
- builtins.BaseException
- with_traceback
- args
74class MarkupSyntaxError(ParserSyntaxError): 75 """Raised when parsed markup text contains an error.""" 76 77 _delimiters = ("[", "]")
Raised when parsed markup text contains an error.
Inherited Members
- builtins.BaseException
- with_traceback
- args