Skip to content

utils

Utility functions for turtle-canon CLI.

Cache

Small cache.

Source code in turtle_canon/cli/utils.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
class Cache:
    """Small cache."""

    def __init__(self) -> None:
        self._errors: list[str | Exception] = []
        self._warnings: list[str | Exception] = []
        self._files: list[str | Path] = []

    @property
    def errors(self) -> list[str | Exception]:
        """Get `errors` attribute."""
        return self._errors

    @property
    def warnings(self) -> list[str | Exception]:
        """Get `warnings` attribute."""
        return self._warnings

    @property
    def files(self) -> list[str | Path]:
        """Get `files` attribute."""
        return self._files

    def add_error(self, error: str | Exception) -> None:
        """Add an error to the cache."""
        if not isinstance(error, (str, Exception)):
            raise TypeError("error must be either a str or Exception")
        self._errors.append(error)

    def add_warning(self, warning: str | Exception) -> None:
        """Add a warning to the cache."""
        if not isinstance(warning, (str, Exception)):
            raise TypeError("warning must be either a str or Exception")
        self._warnings.append(warning)

    def add_file(self, file: str | Path) -> None:
        """Add a file to the cache."""
        if not isinstance(file, (str, Path)):
            raise TypeError("file must be either a str or Exception")
        self._files.append(file)

errors: list[str | Exception] property

Get errors attribute.

files: list[str | Path] property

Get files attribute.

warnings: list[str | Exception] property

Get warnings attribute.

add_error(error)

Add an error to the cache.

Source code in turtle_canon/cli/utils.py
37
38
39
40
41
def add_error(self, error: str | Exception) -> None:
    """Add an error to the cache."""
    if not isinstance(error, (str, Exception)):
        raise TypeError("error must be either a str or Exception")
    self._errors.append(error)

add_file(file)

Add a file to the cache.

Source code in turtle_canon/cli/utils.py
49
50
51
52
53
def add_file(self, file: str | Path) -> None:
    """Add a file to the cache."""
    if not isinstance(file, (str, Path)):
        raise TypeError("file must be either a str or Exception")
    self._files.append(file)

add_warning(warning)

Add a warning to the cache.

Source code in turtle_canon/cli/utils.py
43
44
45
46
47
def add_warning(self, warning: str | Exception) -> None:
    """Add a warning to the cache."""
    if not isinstance(warning, (str, Exception)):
        raise TypeError("warning must be either a str or Exception")
    self._warnings.append(warning)

print_changed_files(files, exit_after=False)

Print list of changed files.

Parameters:

Name Type Description Default
files Sequence[str | Path]

List of files with changes.

required
exit_after bool

Whether or not to call sys.exit(1) after printing the message.

False
Source code in turtle_canon/cli/utils.py
146
147
148
149
150
151
152
153
154
155
156
157
def print_changed_files(files: Sequence[str | Path], exit_after: bool = False) -> None:
    """Print list of changed files.

    Parameters:
        files: List of files with changes.
        exit_after: Whether or not to call `sys.exit(1)` after printing the message.

    """
    res = "\nChanged files:\n"
    res += "{}\n".join(str(_) for _ in files)

    _print_message(res, target=sys.stdout, prefix="", exit_after=exit_after)

print_error(message, exit_after=True)

Print an error message to the console.

Parameters:

Name Type Description Default
message str | Exception

The error message to print.

required
exit_after bool

Whether or not to call sys.exit(1) after printing the message.

True
Source code in turtle_canon/cli/utils.py
79
80
81
82
83
84
85
86
87
88
89
90
def print_error(message: str | Exception, exit_after: bool = True) -> None:
    """Print an error message to the console.

    Parameters:
        message: The error message to print.
        exit_after: Whether or not to call `sys.exit(1)` after printing the message.

    """
    res = f"Misfire !\n\n{message}"
    if isinstance(message, Exception):
        res += f"\n\nGeneral information about the exception: {message.__doc__}"
    _print_message(res, target=sys.stderr, prefix="ERROR: ", exit_after=exit_after)

print_summary(errors=None, warnings=None, exit_after=False)

Print a summary, including of error and/or warning messages.

Parameters:

Name Type Description Default
errors Sequence[str | Exception] | None

List of error messages.

None
warnings Sequence[str | Exception] | None

List of warning messages.

None
exit_after bool

Whether or not to call sys.exit(1) after printing the message.

False
Source code in turtle_canon/cli/utils.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
def print_summary(
    errors: Sequence[str | Exception] | None = None,
    warnings: Sequence[str | Exception] | None = None,
    exit_after: bool = False,
) -> None:
    """Print a summary, including of error and/or warning messages.

    Parameters:
        errors: List of error messages.
        warnings: List of warning messages.
        exit_after: Whether or not to call `sys.exit(1)` after printing the message.

    """
    res = ""
    target = sys.stdout

    if errors or warnings:
        res += "The balls are stuck !\n\n"
        target = sys.stderr
    else:
        res += "Successful Fire !\n"

    if errors:
        res += "ERRORS:\n"
        for error in errors:
            res += f"* {error}\n"
            if isinstance(error, Exception):
                res += f"  General info: {error.__doc__}\n"

    if warnings:
        res += "WARNINGS:\n"
        for warning in warnings:
            res += f"* {warning}\n"
            if isinstance(warning, Exception):
                res += f"  General info: {warning.__doc__}\n"

    _print_message(res[:-1], target=target, prefix="", exit_after=exit_after)

print_warning(message, exit_after=False)

Print a warnings message to the console.

Parameters:

Name Type Description Default
message str | Exception

The warning message to print.

required
exit_after bool

Whether or not to call sys.exit(1) after printing the message.

False
Source code in turtle_canon/cli/utils.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def print_warning(message: str | Exception, exit_after: bool = False) -> None:
    """Print a warnings message to the console.

    Parameters:
        message: The warning message to print.
        exit_after: Whether or not to call `sys.exit(1)` after printing the message.

    """
    res = f"Don't come too close !\n\n{message}"
    if isinstance(message, Exception):
        res += f"\n\nGeneral information about the warning: {message.__doc__}"
    _print_message(res, target=sys.stderr, prefix="WARNING: ", exit_after=exit_after)