File: cli.py

package info (click to toggle)
python-idasen 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 256 kB
  • sloc: python: 976; makefile: 8
file content (329 lines) | stat: -rwxr-xr-x 10,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import functools

from . import IdasenDesk
from typing import Any
from typing import Callable
from typing import Dict
from typing import List
from typing import Optional
import argparse
import asyncio
import importlib.metadata
import logging
import os
import sys
import voluptuous as vol
import yaml
import platform

HOME = os.path.expanduser("~")
IDASEN_CONFIG_DIRECTORY = os.path.join(HOME, ".config", "idasen")
IDASEN_CONFIG_PATH = os.path.join(IDASEN_CONFIG_DIRECTORY, "idasen.yaml")

DEFAULT_CONFIG: Dict[str, Any] = {
    "positions": {"stand": 1.1, "sit": 0.75},
    "mac_address": "AA:AA:AA:AA:AA:AA",
}

CONFIG_SCHEMA = vol.Schema(
    {
        "mac_address": vol.All(str, vol.Length(min=17, max=36)),
        "positions": {
            str: vol.All(
                vol.Any(float, int),
                vol.Range(min=IdasenDesk.MIN_HEIGHT, max=IdasenDesk.MAX_HEIGHT),
            )
        },
    },
    extra=False,
)

RESERVED_NAMES = {"init", "pair", "monitor", "height", "speed", "save", "delete"}


def save_config(config: dict, path: str = IDASEN_CONFIG_PATH):
    with open(path, "w") as f:
        yaml.dump(config, f)


def load_config(path: str = IDASEN_CONFIG_PATH) -> dict:
    """Load user config."""
    try:
        with open(path, "r") as f:
            config = yaml.load(f, Loader=yaml.FullLoader)
    except FileNotFoundError:
        return {}

    # convert old config file format
    if "positions" not in config:
        config["positions"] = dict()
        config["positions"]["sit"] = config.pop(
            "sit_height", DEFAULT_CONFIG["positions"]["sit"]
        )
        config["positions"]["stand"] = config.pop(
            "stand_height", DEFAULT_CONFIG["positions"]["stand"]
        )

        save_config(config, path)

    try:
        config = CONFIG_SCHEMA(config)
    except vol.Invalid as e:
        print(f"Invalid configuration: {e}", file=sys.stderr)
        sys.exit(1)
    else:
        for position in config["positions"]:
            if position in RESERVED_NAMES:
                print(
                    "Invalid configuration, "
                    f"position with name '{position}' is a reserved name.",
                    file=sys.stderr,
                )
                sys.exit(1)

        return config


def add_common_args(parser: argparse.ArgumentParser):
    parser.add_argument(
        "--mac-address",
        dest="mac_address",
        type=str,
        help="MAC address of the Idasen desk.",
    )
    parser.add_argument(
        "--verbose", "-v", action="count", default=0, help="Increase logging verbosity."
    )
    parser.add_argument(
        "--version", action="store_true", help="Prints version information."
    )


def get_parser(config: dict) -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="ikea IDÅSEN desk control")
    add_common_args(parser)
    sub = parser.add_subparsers(dest="sub", help="Subcommands", required=False)

    height_parser = sub.add_parser("height", help="Get the desk height.")
    speed_parser = sub.add_parser("speed", help="Get the desk speed.")
    monitor_parser = sub.add_parser("monitor", help="Monitor the desk position.")
    init_parser = sub.add_parser("init", help="Initialize a new configuration file.")
    save_parser = sub.add_parser("save", help="Save current desk position.")
    pair_parser = sub.add_parser("pair", help="Pair with device.")
    save_parser.add_argument("name", help="Position name")
    delete_parser = sub.add_parser("delete", help="Remove position with given name.")
    delete_parser.add_argument("name", help="Position name")

    positions = config.get("positions", {})
    for name, value in positions.items():
        subcommand = sub.add_parser(name, help=f"Move the desk to {value}m.")
        add_common_args(subcommand)

    init_parser.add_argument(
        "-f",
        "--force",
        action="store_true",
        help="Overwrite any existing configuration files.",
    )

    add_common_args(init_parser)
    add_common_args(pair_parser)
    add_common_args(height_parser)
    add_common_args(speed_parser)
    add_common_args(monitor_parser)
    add_common_args(save_parser)
    add_common_args(delete_parser)

    return parser


async def init(args: argparse.Namespace) -> int:
    if not args.force and os.path.isfile(IDASEN_CONFIG_PATH):
        print("Configuration file already exists.", file=sys.stderr)
        print("Use --force to overwrite existing configuration.", file=sys.stderr)
        return 1
    else:
        mac = await IdasenDesk.discover()
        if mac is not None:
            print(f"Discovered desk's MAC address: {mac}", file=sys.stderr)
            DEFAULT_CONFIG["mac_address"] = str(mac)
        else:
            print("Failed to discover desk's MAC address", file=sys.stderr)
        os.makedirs(IDASEN_CONFIG_DIRECTORY, exist_ok=True)
        with open(IDASEN_CONFIG_PATH, "w") as f:
            f.write("# https://newam.github.io/idasen/index.html#configuration\n")
            yaml.dump(DEFAULT_CONFIG, f)
        print(
            f"Created new configuration file at: {IDASEN_CONFIG_PATH}", file=sys.stderr
        )
        print("'idasen pair' can be used to pair to desk.")

    return 0


async def pair(args: argparse.Namespace) -> Optional[int]:
    try:
        async with IdasenDesk(args.mac_address, exit_on_fail=True) as desk:
            await desk.pair()
    except NotImplementedError as e:
        if platform.system() == "Darwin":
            print(
                "The pair subcommand does not function reliably on macOS.\n"
                "A pairing dialogue is shown if the OS deems that pairing is needed.\n"
                "Retrying can help.\n\n"
                "See docs at https://bleak.readthedocs.io/en/latest/backends/macos.html"
            )
            return 1
        else:
            raise e
    return None


async def monitor(args: argparse.Namespace) -> None:
    try:
        async with IdasenDesk(args.mac_address, exit_on_fail=True) as desk:

            async def printer(height: float, speed: float):
                print(f"{height:.3f} meters - {speed:.3f} meters/second", flush=True)

            await desk.monitor(printer)
            while True:
                await asyncio.sleep(1000000)
    except (KeyboardInterrupt, asyncio.exceptions.CancelledError):
        pass


async def height(args: argparse.Namespace):
    async with IdasenDesk(args.mac_address, exit_on_fail=True) as desk:
        height = await desk.get_height()
        print(f"{height:.3f} meters")


async def speed(args: argparse.Namespace):
    async with IdasenDesk(args.mac_address, exit_on_fail=True) as desk:
        speed = await desk.get_speed()
        print(f"{speed:.3f} meters/second")


async def move_to(args: argparse.Namespace, position: float) -> None:
    async with IdasenDesk(args.mac_address, exit_on_fail=True) as desk:
        await desk.move_to_target(target=position)


async def save(args: argparse.Namespace, config: dict) -> int:
    if args.name in RESERVED_NAMES:
        print(f"Position with name '{args.name}' is a reserved name.", file=sys.stderr)
        return 1

    async with IdasenDesk(args.mac_address, exit_on_fail=True) as desk:
        height = await desk.get_height()

    config["positions"][args.name] = height
    save_config(config)

    print(f"Saved position '{args.name}' with height: {height}m.")
    return 0


async def delete(args: argparse.Namespace, config: dict) -> int:
    position = config["positions"].pop(args.name, None)
    if args.name in RESERVED_NAMES:
        print(f"Position with name '{args.name}' is a reserved name.", file=sys.stderr)
        return 1
    elif position is None:
        print(f"Position with name '{args.name}' doesn't exist.", file=sys.stderr)
    else:
        save_config(config)
        print(f"Position with name '{args.name}' removed.")

    return 0


def from_config(
    args: argparse.Namespace,
    config: dict,
    parser: argparse.ArgumentParser,
    key: str,
    raise_error: bool = True,
):
    if hasattr(args, key) and getattr(args, key) is None:
        if key in config:
            setattr(args, key, config[key])
        elif raise_error:
            parser.error(f"{key} must be provided via the CLI or the config file")


def count_to_level(count: int) -> int:
    if count == 1:
        return logging.ERROR
    elif count == 2:
        return logging.WARNING
    elif count == 3:
        return logging.INFO
    elif count >= 4:
        return logging.DEBUG

    return logging.CRITICAL


def subcommand_to_callable(sub: str, config: dict) -> Callable:
    if sub == "init":
        return init
    elif sub == "pair":
        return pair
    elif sub == "monitor":
        return monitor
    elif sub == "height":
        return height
    elif sub == "speed":
        return speed
    elif sub == "save":
        return functools.partial(save, config=config)
    elif sub == "delete":
        return functools.partial(delete, config=config)
    elif sub in config.get("positions", {}):
        position = config["positions"][sub]
        return functools.partial(move_to, position=position)
    else:
        raise AssertionError(f"internal error, please report this bug {sub=}")


def main(argv: Optional[List[str]] = None):
    config = load_config()
    parser = get_parser(config)
    args = parser.parse_args(argv)

    from_config(args, config, parser, "mac_address", raise_error=args.sub != "init")

    level = count_to_level(args.verbose)

    root_logger = logging.getLogger()

    handler = logging.StreamHandler(stream=sys.stderr)
    handler.setLevel(level)
    formatter = logging.Formatter("{levelname} {name} {message}", style="{")
    handler.setFormatter(formatter)
    root_logger.addHandler(handler)
    root_logger.setLevel(level)

    if args.version:
        version = importlib.metadata.version("idasen")
        print(version)
        sys.exit(0)
    elif args.sub is None:
        print("A subcommand is required")
        parser.print_usage()
        sys.exit(1)
    else:
        func = subcommand_to_callable(args.sub, config)

        rc = asyncio.run(func(args))

        if rc is None:
            rc = 0

        sys.exit(rc)


if __name__ == "__main__":
    main()