Skip to content

exceptions

Custom Exception-s used in pytermgui.

AnsiSyntaxError

Bases: ParserSyntaxError

Raised when parsed ANSI text contains an error.

Source code in pytermgui/exceptions.py
90
91
92
93
class AnsiSyntaxError(ParserSyntaxError):
    """Raised when parsed ANSI text contains an error."""

    _delimiters = ("\\x1b[", "m")

ColorSyntaxError

Bases: Exception

Raised when a color string could not be parsed into a pytermgui.colors.Color

Source code in pytermgui/exceptions.py
29
30
class ColorSyntaxError(Exception):
    """Raised when a color string could not be parsed into a `pytermgui.colors.Color`"""

LineLengthError

Bases: Exception

Raised when a widget line is not the expected length.

Source code in pytermgui/exceptions.py
25
26
class LineLengthError(Exception):
    """Raised when a widget line is not the expected length."""

MarkupSyntaxError

Bases: ParserSyntaxError

Raised when parsed markup text contains an error.

Source code in pytermgui/exceptions.py
84
85
86
87
class MarkupSyntaxError(ParserSyntaxError):
    """Raised when parsed markup text contains an error."""

    _delimiters = ("[", "]")

ParserSyntaxError dataclass

Bases: Exception

Parent exception for unparsable strings.

This exception takes some basic parameters, and formats a message depending on the _delimiters value. This has to be supplied by each child, while the rest of the arguments are to be given at construction.

Source code in pytermgui/exceptions.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@dataclass
class ParserSyntaxError(Exception):
    """Parent exception for unparsable strings.

    This exception takes some basic parameters, and formats
    a message depending on the _delimiters value. This has to
    be supplied by each child, while the rest of the arguments
    are to be given at construction."""

    tag: str
    cause: str
    context: str
    _delimiters: tuple[str, str] = field(init=False)

    @property
    def message(self) -> str:
        """Create message from tag, context and cause."""

        msg = f'Tag "{self.tag}" '

        if self.context != "":
            index = self.context.find(self.tag)
            escaped_context = repr(self.context).strip("'")

            if len(self.context) > 50:
                escaped_context = (
                    "..."
                    + ascii(self.context).strip("'")[max(index - 25, 0) : index + 25]
                    + "..."
                )

            highlighted = escaped_context.replace(
                self.tag, "\x1b[31m\x1b[1m" + self.tag + "\x1b[0m", 1
            )

            msg += f'in string "{highlighted}" '

        return f"{msg}{self.cause}."

    def escape_message(self) -> str:
        """Return message with markup tags escaped."""

        char = self._delimiters[0]
        return self.message.replace(char, "\\" + char)

    def __str__(self) -> str:
        """Show message."""

        return self.message

__str__()

Show message.

Source code in pytermgui/exceptions.py
78
79
80
81
def __str__(self) -> str:
    """Show message."""

    return self.message

escape_message()

Return message with markup tags escaped.

Source code in pytermgui/exceptions.py
72
73
74
75
76
def escape_message(self) -> str:
    """Return message with markup tags escaped."""

    char = self._delimiters[0]
    return self.message.replace(char, "\\" + char)

message() property

Create message from tag, context and cause.

Source code in pytermgui/exceptions.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
@property
def message(self) -> str:
    """Create message from tag, context and cause."""

    msg = f'Tag "{self.tag}" '

    if self.context != "":
        index = self.context.find(self.tag)
        escaped_context = repr(self.context).strip("'")

        if len(self.context) > 50:
            escaped_context = (
                "..."
                + ascii(self.context).strip("'")[max(index - 25, 0) : index + 25]
                + "..."
            )

        highlighted = escaped_context.replace(
            self.tag, "\x1b[31m\x1b[1m" + self.tag + "\x1b[0m", 1
        )

        msg += f'in string "{highlighted}" '

    return f"{msg}{self.cause}."

TimeoutException

Bases: Exception

Raised when an action has timed out.

Source code in pytermgui/exceptions.py
17
18
class TimeoutException(Exception):
    """Raised when an action has timed out."""

WidthExceededError

Bases: Exception

Raised when an element's width is larger than the screen.

Source code in pytermgui/exceptions.py
21
22
class WidthExceededError(Exception):
    """Raised when an element's width is larger than the screen."""