Added the option to retrieve the led user input

+ User input is matched with the luxafor.Leds attributes
+ Also added some annotations
This commit is contained in:
Felipe Martin 2017-02-04 16:25:58 +01:00
parent 47bdfdac79
commit 047c303db6
1 changed files with 32 additions and 8 deletions

View File

@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
import sys import sys
import click import click
@ -6,11 +7,14 @@ import click
from luxafor import luxafor from luxafor import luxafor
logger = logging.getLogger()
def get_luxafor(): def get_luxafor():
return luxafor.Luxafor() return luxafor.Luxafor()
def convert_hex_to_dec_color(hex_color): def convert_hex_to_dec_color(hex_color: str) -> tuple:
(red, green, blue) = hex_color[0:2], hex_color[2:4], hex_color[4:6] (red, green, blue) = hex_color[0:2], hex_color[2:4], hex_color[4:6]
red = int(red, 16) red = int(red, 16)
@ -20,12 +24,31 @@ def convert_hex_to_dec_color(hex_color):
return (red, green, blue) return (red, green, blue)
def convert_dec_to_hex_color(red, green, blue): def convert_dec_to_hex_color(red: int, green: int, blue: int) -> str:
colors = (red, green, blue) colors = (red, green, blue)
return "".join([ return "".join([
hex(color).split('x')[1].upper() for color in colors]) hex(color).split('x')[1].upper() for color in colors])
def get_matching_led(led: str) -> hex:
"""
Returns a mathching Led object from the :class:`luxafor.Leds` based
on the user input in the CLI.
If no valid input is given defaults to all leds.
"""
led_group = getattr(luxafor.Leds, led.upper(), False)
if led_group:
return led_group
led_number = getattr(luxafor.Leds, 'LED%s' % led, False)
if led_number:
return led_number
logger.info('Led(s) %s not found, falling back to all' % led)
return luxafor.Leds.ALL
@click.group() @click.group()
def cli(): def cli():
pass pass
@ -60,7 +83,7 @@ def dec2hex(red, green, blue):
@cli.command() @cli.command()
@click.option('--led', default=luxafor.Leds.ALL) @click.option('--led', default='all')
@click.argument('color') @click.argument('color')
def set(led, color): def set(led, color):
"""Sets the flag with a fixed color.""" """Sets the flag with a fixed color."""
@ -68,11 +91,11 @@ def set(led, color):
flag = get_luxafor() flag = get_luxafor()
flag.set_color(*color, led=led) flag.set_color(*color, led=get_matching_led(led))
@cli.command() @cli.command()
@click.option('--led', default=luxafor.Leds.ALL) @click.option('--led', default='all')
@click.option('--speed', default=128, type=click.IntRange(0, 255)) @click.option('--speed', default=128, type=click.IntRange(0, 255))
@click.argument('color') @click.argument('color')
def fade(led, speed, color): def fade(led, speed, color):
@ -80,11 +103,11 @@ def fade(led, speed, color):
flag = get_luxafor() flag = get_luxafor()
flag.fade(*color, led=led, speed=speed) flag.fade(*color, led=get_matching_led(led), speed=speed)
@cli.command() @cli.command()
@click.option('--led', default=luxafor.Leds.ALL) @click.option('--led', default='all')
@click.option('--speed', default=6, type=click.IntRange(0, 255)) @click.option('--speed', default=6, type=click.IntRange(0, 255))
@click.option('--repeat', default=1, type=click.IntRange(1, 255)) @click.option('--repeat', default=1, type=click.IntRange(1, 255))
@click.argument('color') @click.argument('color')
@ -93,7 +116,8 @@ def strobe(led, speed, repeat, color):
flag = get_luxafor() flag = get_luxafor()
flag.strobe(*color, led=led, speed=speed, repeat=repeat) flag.strobe(*color, led=get_matching_led(led),
speed=speed, repeat=repeat)
@cli.command() @cli.command()