This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcli.py
executable file
·70 lines (50 loc) · 1.48 KB
/
cli.py
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
import logging
import click
import re
from desk_position import DeskPosition
from linak_device import LinakDesk
pass_desk = click.make_pass_decorator(LinakDesk)
def validate_mac(ctx, param, mac):
if re.match('^([0-9A-Fa-f]{2}:){5}[0-9A-Fa-f]{2}$', mac) is None:
raise click.BadParameter(mac + ' is no valid mac address')
return mac
@click.group(invoke_without_command=True)
@click.option('-b', '--bdaddr', required=True, callback=validate_mac)
@click.option('--debug/--normal', default=False)
@click.pass_context
def cli(ctx, bdaddr, debug):
if debug:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
desk = LinakDesk(bdaddr)
desk.read_dpg_data()
ctx.obj = desk
if ctx.invoked_subcommand is None:
ctx.invoke(state)
@cli.command()
@pass_desk
def name(desk):
click.echo("Desk name: %s" % desk.name)
@cli.command()
@pass_desk
def get_height(desk):
click.echo("Desk position: %s" % desk.current_height_with_offset.human_cm)
@cli.command()
@click.option('-t', '--target', required=True, type=click.IntRange(1, 200))
@pass_desk
def move_to(desk, target):
desk.move_to_cm(target)
@cli.command()
@click.argument('slot', type=click.IntRange(1, 2))
@pass_desk
def move_to_fav(desk, slot):
desk.move_to_fav(slot)
@cli.command()
@click.pass_context
def state(ctx):
""" Prints out all available information. """
desk = ctx.obj
click.echo(desk)
if __name__ == "__main__":
cli()