summaryrefslogtreecommitdiffstats
path: root/src/config.py
blob: 85b405b5c3020ee0b73d77a4016e25457658f42d (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
import configparser
import distutils
import distutils.util
import os
from pathlib import Path


class config:
    # Class constants
    default_config = {
        "deploy_type": "quick",
        "last_paired_device": "None",
        "paired": "False",
        "adapter": "None",
    }
    config_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.join(
        os.path.expanduser("~"), ".config"
    )
    config_file = os.path.join(config_dir, "siglo.ini")

    def load_defaults(self):
        if not Path(self.config_dir).is_dir():
            Path.mkdir(Path(self.config_dir))
        # if config file is not valid, load defaults
        if not self.file_valid():
            config = configparser.ConfigParser()
            config["settings"] = self.default_config
            with open(self.config_file, "w") as f:
                config.write(f)

    def file_valid(self):
        if not Path(self.config_file).is_file():
            return False
        else:
            config = configparser.ConfigParser()
            config.read(self.config_file)
            for key in list(self.default_config.keys()):
                if not key in config["settings"]:
                    return False
            return True

    def get_property(self, key):
        config = configparser.ConfigParser()
        config.read(self.config_file)
        prop = config["settings"][key]
        if key == "paired":
            prop = bool(distutils.util.strtobool(prop))
        return prop

    def set_property(self, key, val):
        config = configparser.ConfigParser()
        config.read(self.config_file)
        config["settings"][key] = val
        with open(self.config_file, "w") as f:
            config.write(f)