viri-leds-dmx-sacn/conf.py

49 lines
1.2 KiB
Python

try:
# python 3.11
from tomllib import loads as toml_load
except ImportError:
from rtoml import load as toml_load
import logging
from sys import exit
LOG = logging.getLogger('Config')
def load_and_validate_config(path):
try:
with open(path, 'r') as cf:
config = toml_load(cf.read())
except Exception as e:
LOG.error(f'{path} is no valid toml configuration file')
exit(1)
# validate options exist.
for section, option in (
('mqtt', 'host'),
('mqtt', 'topic'),
('sacn', 'multicast'),
('alerts', 'brightness'),
('rainbow', 'enable'),
('rainbow', 'intensity'),
('rainbow', 'brightness'),
('rainbow', 'speed'),
):
if config.get(section, {}).get(option) is None:
LOG.error(
f'configuration option "{section}" "{option}" is missing in config'
)
exit(1)
# dmx values
for section, option in (
('alerts', 'brightness'),
('rainbow', 'intensity'),
('rainbow', 'brightness'),
):
if int(config[section][option]) < 10:
LOG.error(f'value of "{section}" "{option}" must be atleast 10')
exit(1)
return config