summaryrefslogtreecommitdiff
path: root/modules/config.py
blob: 1a5865be7de28200c233f75ca0d5154d25a894bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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.responses_dir = ""
        self.resources_dir = ""
        self.applications_dir = ""

        self.lock = ""

        self.hestia_images = ""

    def update(self, **entries) -> None:
        self.__dict__.update(entries)

        self.intents_dir = Config.__convert_to_absolute_path(self.intents_dir)
        self.responses_dir = Config.__convert_to_absolute_path(self.responses_dir)
        self.resources_dir = Config.__convert_to_absolute_path(self.resources_dir)

        self.hestia_images = os.path.join(self.resources_dir, "hestia_images")

    @staticmethod
    def __convert_to_absolute_path(path: str) -> str:
        if not path.startswith("/"):
            path = os.path.join(os.path.dirname(__main__.__file__), path)

        return path

    def validate(self) -> None:
        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 = 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