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") class ConfigWrapper: # simple class to wrap a dict into a object def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) def load_and_validate_config(path): try: with open(path, "r") as cf: config = toml_load(cf.read()) except Exception as e: LOG.exception(f"{path} is no valid toml configuration file") exit(1) if not config.get("mqtt", {}).get("host"): LOG.error( f'configuration option "mqtt" "host" is missing in config, but required to exist' ) exit(1) universes = {} used_universes = {} for universe, uconfig in config.get("universes", {}).items(): universes[universe] = ConfigWrapper( alert_brightness=max(int(uconfig.get("alert_brightness", 255)), 10), filters=uconfig.get("filters", []), lights=uconfig.get("lights", {}), multicast=bool(uconfig.get("multicast", False) is True), rainbow_brightness=int(uconfig.get("rainbow_brightness", 150)), target=uconfig.get("target", "127.0.0.1"), universe=int(uconfig.get("universe", 1)), ) if universes[universe].universe in used_universes: LOG.warning( f"universe {universes[universe].universe} used by both {universe} and {used_universes[universes[universe]].universe}" ) conf = ConfigWrapper( mqtt=ConfigWrapper( host=config["mqtt"]["host"], user=config["mqtt"].get("user"), password=config["mqtt"].get("password"), topic=config["mqtt"].get("topic", "/voc/alert"), ), universes=universes, ) return conf