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        msg = f'Tag "{self.tag}" '
52
53        if self.context != "":
54            escaped_context = ascii(self.context).strip("'")[:50]
55            highlighted = escaped_context.replace(
56                self.tag, "\x1b[31m\x1b[1m" + self.tag + "\x1b[0m", 1
57            )
58
59            msg += f'in string "{highlighted}" '
60
61        return f"{msg}{self.cause}."
62
63    def escape_message(self) -> str:
64        """Return message with markup tags escaped."""
65
66        char = self._delimiters[0]
67        return self.message.replace(char, "\\" + char)
68
69    def __str__(self) -> str:
70        """Show message."""
71
72        return self.message
73
74
75class MarkupSyntaxError(ParserSyntaxError):
76    """Raised when parsed markup text contains an error."""
77
78    _delimiters = ("[", "]")
79
80
81class AnsiSyntaxError(ParserSyntaxError):
82    """Raised when parsed ANSI text contains an error."""
83
84    _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):
26class LineLengthError(Exception):
27    """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):
82class AnsiSyntaxError(ParserSyntaxError):
83    """Raised when parsed ANSI text contains an error."""
84
85    _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):
76class MarkupSyntaxError(ParserSyntaxError):
77    """Raised when parsed markup text contains an error."""
78
79    _delimiters = ("[", "]")

Raised when parsed markup text contains an error.

Inherited Members
ParserSyntaxError
ParserSyntaxError
message
escape_message
builtins.BaseException
with_traceback
args