Verified Commit 898e9c60 authored by Vojtech Myslivec's avatar Vojtech Myslivec
Browse files

Add support for uCI configuration

parent 20dd6e07
......@@ -7,18 +7,17 @@ from time import sleep
from .helpers import usage, trap_signals, cleanup
from .rainbow import disable_leds, set_led
from .config import Config
from .default_settings import COLORS, ENABLE_PROBABILITY, LEDS, SLEEP_MAX
def blink():
if random() < ENABLE_PROBABILITY:
def blink(conf):
if random() < conf.enable_probability:
random_state = "enable"
else:
random_state = "disable"
random_led = choice(LEDS)
random_color = choice(COLORS)
random_led = choice(conf.leds)
random_color = choice(conf.colors)
set_led(random_led, random_state)
set_led(random_led, random_color)
......@@ -26,12 +25,13 @@ def blink():
def main():
usage()
conf = Config()
trap_signals()
disable_leds()
while True:
blink()
random_sleep = random() * SLEEP_MAX
blink(conf)
random_sleep = random() * conf.sleep_max
sleep(random_sleep)
cleanup()
"""
Config handler for christmas
"""
import uci
from .default_settings import COLORS, ENABLE_PROBABILITY, LEDS, SLEEP_MAX
def get_value_from_uci(uci_context, option):
try:
return uci_context.get("christmas", "christmas", option)
except uci.UciExceptionNotFound:
return None
class Config:
def __init__(self):
self._prepare_defaults()
self._load_uci()
def _prepare_defaults(self):
self.colors = COLORS
self.enable_probability = ENABLE_PROBABILITY
self.leds = LEDS
self.sleep_max = SLEEP_MAX
def _load_uci(self):
with uci.Uci() as uci_context:
self._set_colors_from_uci(uci_context)
self._set_probability_from_uci(uci_context)
self._set_sleep_from_uci(uci_context)
def _set_colors_from_uci(self, uci_context):
value = get_value_from_uci(uci_context, "colors")
if value:
self.colors = value
def _set_probability_from_uci(self, uci_context):
value = get_value_from_uci(uci_context, "enable_probability")
if value:
self.enable_probability = float(value)
def _set_sleep_from_uci(self, uci_context):
value = get_value_from_uci(uci_context, "sleep_max")
if value:
self.sleep = int(value)
......@@ -14,6 +14,9 @@ setup(
license='GNU GPL v3',
url="https://gitlab.labs.nic.cz/turris/sentinel/sview",
packages=find_packages(),
install_requires=[
"pyuci",
],
entry_points={
'console_scripts': [
'christmas=christmas.__main__:main'
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment