Skip to content

helpers.py

cli.helpers

Helpers for CLI Interface.

Attributes

base_formatters module-attribute

base_formatters: dict[str, Callable] = {
    "led_colour": format_led_colour,
    "liquid_level": format_liquid_level,
}

Classes

CommandLoop

Class to handle command loop.

Source code in ember_mug/cli/helpers.py
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
class CommandLoop:
    """Class to handle command loop."""

    def __init__(self) -> None:
        """Start running."""
        self.running = True

    def __iter__(self) -> Generator[None, None, None]:
        """Yield until stopped."""
        try:
            while self.running:
                yield
        except KeyboardInterrupt:
            self.running = False
            raise
Attributes
running instance-attribute
running = True

Functions

build_sub_rows

build_sub_rows(
    row: tuple[str, ...]
) -> dict[int, dict[int, str]]

Build a defaultdict of cells to pad for empty values.

Source code in ember_mug/cli/helpers.py
33
34
35
36
37
38
39
def build_sub_rows(row: tuple[str, ...]) -> dict[int, dict[int, str]]:
    """Build a defaultdict of cells to pad for empty values."""
    sub_rows: dict[int, dict[int, str]] = defaultdict(lambda: defaultdict(lambda: ""))
    for i, col in enumerate(row):
        for j, val in enumerate(str(col).split(", ")):
            sub_rows[j][i] = val
    return sub_rows

print_changes

print_changes(
    changes: list[Change], metric: bool = True
) -> None

Print changes.

Source code in ember_mug/cli/helpers.py
64
65
66
67
68
69
70
71
72
73
74
def print_changes(changes: list[Change], metric: bool = True) -> None:
    """Print changes."""
    formatters: dict[str, Callable] = {
        "current_temp": partial(format_temp, metric=metric),
        "target_temp": partial(format_temp, metric=metric),
        **base_formatters,
    }
    for attr, old_value, new_value in changes:
        if formatter := formatters.get(attr):
            old_value, new_value = formatter(old_value), formatter(new_value)  # noqa: PLW2901
        print(Change(attr, old_value, new_value))

print_info

print_info(mug: EmberMug) -> None

Print all mug data.

Source code in ember_mug/cli/helpers.py
58
59
60
61
def print_info(mug: EmberMug) -> None:
    """Print all mug data."""
    print("Device Data")
    print_table(list(mug.data.formatted.items()))

print_table

print_table(data: list[tuple[str, ...]]) -> None

Print data in a nice little ASCII table.

Source code in ember_mug/cli/helpers.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def print_table(data: list[tuple[str, ...]]) -> None:
    """Print data in a nice little ASCII table."""
    if not data:
        return
    rows = [build_sub_rows(r) for r in data]
    num_columns = max(len(sr) for r in rows for sr in r.values())
    column_sizes = [max(len(sr[i]) for r in rows for sr in r.values()) + 2 for i in range(num_columns)]
    vertical = f'+{"+".join("-" * i for i in column_sizes)}+'
    print(vertical)
    for row in rows:
        for sub_row in row.values():
            inner = "|".join(f" {sub_row[i]:<{width-2}} " for i, width in enumerate(column_sizes))
            print(f"|{inner}|")
        print(vertical)

validate_mac

validate_mac(value: str) -> str

Check if specified MAC Address is valid.

Source code in ember_mug/cli/helpers.py
26
27
28
29
30
def validate_mac(value: str) -> str:
    """Check if specified MAC Address is valid."""
    if not isinstance(value, str) or not re.match(MAC_ADDRESS_REGEX, value):
        raise ArgumentTypeError("Invalid MAC Address")
    return value.lower()