import sys import __main__ import os.path import yaml class Config: INPUT_PW = "pw-record" INPUT_STDIN = "stdin" INPUT_MODES = [INPUT_PW, INPUT_STDIN] def __init__(self): self.input_mode = "" self.intents_dir = "" self.applications_dir = "" self.lock = "" def update(self, **entries): self.__dict__.update(entries) if not self.intents_dir.startswith("/"): self.intents_dir = os.path.join(os.path.dirname(__main__.__file__), self.intents_dir) def validate(self): if self.input_mode not in self.INPUT_MODES: sys.exit(f"Invalid input_mode '{self.input_mode}', valid options: {", ".join(self.INPUT_MODES)}") def load_config(): config = Config() with open(os.path.join(os.path.dirname(__main__.__file__), "config.yaml")) as stream: config.update(**yaml.safe_load(stream)) user_config = os.path.join(os.environ["HOME"], ".config", "hestia", "config.yaml") if os.path.exists(user_config): with open(user_config) as stream: config.update(**yaml.safe_load(stream)) config.validate() return config