pytermgui.exceptions

Custom Exception-s used in pytermgui.

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

Raised when a widget line is not the expected length.

Inherited Members
builtins.Exception
Exception
builtins.BaseException
with_traceback
args
#   class AnsiSyntaxError(ParserSyntaxError):
View Source
79class AnsiSyntaxError(ParserSyntaxError):
80    """Raised when parsed ANSI text contains an error."""
81
82    _delimiters = ("\\x1b[", "m")

Raised when parsed ANSI text contains an error.

Inherited Members
ParserSyntaxError
ParserSyntaxError
message
escape_message
builtins.BaseException
with_traceback
args
#   class MarkupSyntaxError(ParserSyntaxError):
View Source
73class MarkupSyntaxError(ParserSyntaxError):
74    """Raised when parsed markup text contains an error."""
75
76    _delimiters = ("[", "]")

Raised when parsed markup text contains an error.

Inherited Members
ParserSyntaxError
ParserSyntaxError
message
escape_message
builtins.BaseException
with_traceback
args