Skip to content

util

Utility functions for the package.

GetEnvError

Bases: Exception

Exception raised when an environment variable is not found.

get_config_path

get_config_path() -> Path

Get the path to the app config file.

Returns:

Type Description
Path

The path to the app config file.

Source code in src/images_upload_cli/util.py
17
18
19
20
21
22
23
24
def get_config_path() -> Path:
    """Get the path to the app config file.

    Returns:
       The path to the app config file.
    """
    app_dir = click.get_app_dir("images-upload-cli")
    return Path(app_dir) / ".env"

get_env

get_env(variable: str) -> str

Get the value of an environment variable.

Parameters:

Name Type Description Default
variable str

The name of the environment variable to retrieve.

required

Returns:

Type Description
str

The value of the environment variable, if found.

Raises:

Type Description
GetEnvError

If the environment variable is not found.

Source code in src/images_upload_cli/util.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def get_env(variable: str) -> str:
    """Get the value of an environment variable.

    Args:
        variable: The name of the environment variable to retrieve.

    Returns:
        The value of the environment variable, if found.

    Raises:
        GetEnvError: If the environment variable is not found.
    """
    if value := getenv(variable):
        return value

    msg = f"Please setup {variable} in environment variables or in '{get_config_path()}'."
    raise GetEnvError(msg)

human_size

human_size(num: float, suffix: str = 'B') -> str

Convert bytes to human-readable format.

Parameters:

Name Type Description Default
num float

The number of bytes to be converted.

required
suffix str

The suffix to be appended to the converted size.

'B'

Returns:

Type Description
str

The human-readable size with the appropriate unit and suffix.

Source code in src/images_upload_cli/util.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def human_size(num: float, suffix: str = "B") -> str:
    """Convert bytes to human-readable format.

    Args:
        num: The number of bytes to be converted.
        suffix: The suffix to be appended to the converted size.

    Returns:
        The human-readable size with the appropriate unit and suffix.
    """
    units = ["", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"]
    round_num = 1024.0

    for unit in units:
        if abs(num) < round_num:
            return f"{num:3.1f} {unit}{suffix}"
        num /= round_num

    return f"{num:.1f} Yi{suffix}"

notify_send

notify_send(text_to_print: str) -> None

Send desktop notifications via libnotify.

Parameters:

Name Type Description Default
text_to_print str

The text to be displayed in the desktop notification.

required
Source code in src/images_upload_cli/util.py
67
68
69
70
71
72
73
74
def notify_send(text_to_print: str) -> None:
    """Send desktop notifications via libnotify.

    Args:
        text_to_print: The text to be displayed in the desktop notification.
    """
    if notify_send := which("notify-send"):
        Popen([notify_send, "-a", "images-upload-cli", text_to_print])  # noqa: S603

log_on_error

log_on_error(response: Response) -> None

Logs an error message based on the HTTP response.

Parameters:

Name Type Description Default
response Response

The HTTP response object.

required
Source code in src/images_upload_cli/util.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def log_on_error(response: Response) -> None:
    """Logs an error message based on the HTTP response.

    Args:
        response: The HTTP response object.
    """
    status_class = response.status_code // 100
    error_types = {
        1: "Informational response",
        3: "Redirect response",
        4: "Client error",
        5: "Server error",
    }
    error_type = error_types.get(status_class, "Invalid status code")

    logger.error(
        f"{error_type} '{response.status_code} {response.reason_phrase}' for url '{response.url}'."
    )
    logger.debug(f"Response text:\n{response.text}")