battery_boost.shell_commands

Wrappers for executing TLP system commands in a Tkinter context.

Provides functions to initialize, toggle, and query TLP using sudo, with error handling suitable for a GUI application.

TlpCommandError

Bases: Exception

Raised when tlp-stat fails to run properly.

Source code in src/battery_boost/shell_commands.py
22
23
class TlpCommandError(Exception):
    """Raised when tlp-stat fails to run properly."""

initialise_tlp

Initialize TLP to the default state.

Runs sudo tlp start to reset configuration. Shows an error dialog and exits if the command fails.

Source code in src/battery_boost/shell_commands.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def initialise_tlp(_parent: App) -> None:
    """Initialize TLP to the default state.

    Runs `sudo tlp start` to reset configuration. Shows an error dialog and exits
    if the command fails.
    """
    try:
        subprocess.run(['sudo', 'tlp', 'start'], check=True, timeout=_TIMEOUT)
        return

    except Exception as exc:  # pylint: disable=broad-exception-caught
        messagebox.showerror("TLP Command Error",
                             f"Could not initialize TLP.\n{exc}",
                             parent=_parent)
        _parent.quit_app(f"Error: Could not initialize TLP: {exc}")

revoke_permissions

Revoke cached sudo credentials.

Source code in src/battery_boost/shell_commands.py
147
148
149
150
151
152
153
def revoke_permissions() -> None:
    """Revoke cached sudo credentials."""
    try:
        subprocess.run(['sudo', '--remove-timestamp'], check=False)
    except Exception:  # pylint: disable=broad-exception-caught
        # Don't raise. App is shutting down.
        pass

tlp_active

Return True if TLP is installed, enabled, and has run recently.

Source code in src/battery_boost/shell_commands.py
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
def tlp_active() -> bool:
    """Return True if TLP is installed, enabled, and has run recently."""
    try:
        result = subprocess.run(
            ["tlp-stat", "-s"],
            capture_output=True,
            text=True,
            check=False,  # don't raise if tlp-stat exits nonzero
            timeout=_TIMEOUT
        )
        output = result.stdout
        state_enabled = False
        last_run_valid = False

        for line in output.splitlines():
            line = line.strip().lower()
            if line.startswith("state") and 'enabled' in line:
                state_enabled = True
            if line.startswith("last run") and 'n/a' not in line:
                last_run_valid = True
        return state_enabled and last_run_valid
    except (subprocess.SubprocessError,
            FileNotFoundError,
            UnicodeDecodeError,
            subprocess.TimeoutExpired):
        return False

tlp_get_stats

Retrieve TLP battery statistics.

Runs sudo tlp-stat -b and returns stdout.

Raises:

Type Description
TlpCommandError

Exception if the command fails.

Source code in src/battery_boost/shell_commands.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
def tlp_get_stats() -> str:
    """Retrieve TLP battery statistics.

    Runs `sudo tlp-stat -b` and returns stdout.

    Raises:
        TlpCommandError: Exception if the command fails.
    """
    try:
        result = subprocess.run(['sudo', 'tlp-stat', '-b'],
                                text=True,
                                capture_output=True,
                                check=True,
                                timeout=_TIMEOUT)
    # pylint: disable=raise-missing-from
    except subprocess.CalledProcessError as exc:
        raise TlpCommandError(f"Failed to run tlp-stat:\n{exc.stderr or exc}")
    except (OSError, subprocess.TimeoutExpired) as exc:
        raise TlpCommandError(f"System error while running tlp-stat: {exc}")
    except Exception as exc:  # pylint: disable=broad-exception-caught
        raise TlpCommandError(f"Unexpected error: {exc}")
    return result.stdout

tlp_running

Return True if TLP is running, else False.

Source code in src/battery_boost/shell_commands.py
133
134
135
136
137
138
139
140
141
142
143
144
def tlp_running() -> bool:
    """Return True if TLP is running, else False."""
    try:
        result = subprocess.run(['systemctl', 'is-active', 'tlp.service'],
                                capture_output=True,
                                text=True,
                                check=True,
                                timeout=_TIMEOUT)
        return result.stdout.strip() == 'active'
    except (subprocess.CalledProcessError,
            subprocess.TimeoutExpired):
        return False

tlp_toggle_state

Toggle TLP between default and full-charge profiles.

Parameters:

Name Type Description Default
_parent App

The Tkinter api instance, used for error dialogs.

required
current_state BatteryState

The current battery profile.

required

Returns: True if successful, False otherwise.

Source code in src/battery_boost/shell_commands.py
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def tlp_toggle_state(_parent: App, current_state: BatteryState) -> bool:
    """Toggle TLP between default and full-charge profiles.

    Args:
        _parent: The Tkinter api instance, used for error dialogs.
        current_state: The current battery profile.
    Returns:
        True if successful, False otherwise.
    """
    try:
        if current_state == BatteryState.DEFAULT:
            subprocess.run(['sudo', 'tlp', 'fullcharge'],
                           check=True,
                           capture_output=True,
                           timeout=_TIMEOUT)
        else:
            subprocess.run(['sudo', 'tlp', 'start'],
                           check=True,
                           capture_output=True,
                           timeout=_TIMEOUT)
    except FileNotFoundError as exc:
        _parent.quit_on_error(f"Command not found: {exc.filename}",
                              "TLP Command Error")
    except subprocess.CalledProcessError as exc:
        # Special case:fullcharge requires AC power.
        if not _parent.is_on_ac_power():
            return False  # Non-fatal failure

        _parent.quit_on_error(f"TLP command failed: {exc.returncode}:\n"
                              f"{exc.stderr or exc}",
                              "TLP Command Error")

    except (OSError, subprocess.TimeoutExpired) as exc:
        _parent.quit_on_error(f"System error while running TLP command: {exc}",
                              "TLP Command Error")
    return True