summaryrefslogtreecommitdiff
path: root/src/config.py
blob: 0fa034e6b69c31b9890e18a6fea5f4f6afc94357 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Handle reading of the config file, which on POSIX-compliant systems will be
created in ~/.config/assignment-list-pyqt5/config.py
"""

import configparser
import os
import sys

import src.globals as Globals

class Config():
    def __init__(self):
        self.config_path = self.getConfigPath()

        if not os.path.exists(self.config_path):
            self.createConfig()

        self.loadConfig()

    def getConfigPath(self):
        # Windows config path is "$LOCALAPPDATA/assignment-list-pyqt5/config"
        if sys.platform.startswith("win32"):
            return os.path.join(os.path.expandvars("$LOCALAPPDATA"), 
                                "assignment-list-pyqt5",
                                "config")
        # Unix config path is "$HOME/.config/assignment-list-pyqt5/config"
        else:
            return os.path.join(
                os.path.expanduser("~"),
                ".config",
                "assignment-list-pyqt5",
                "config")

    def loadConfig(self):
        self.config = configparser.ConfigParser()

        try:
            self.config.read(self.config_path)
        except:
            print("Could not parse config file '{}'".format(self.config_path))
        
        if "paths" in self.config:
            if self.config["paths"]["db_path"]:
                Globals.db_path = self.config["paths"]["db_path"]

    def createConfig(self):
        self.config = configparser.ConfigParser()
        if sys.platform.startswith("win32"):
            self.config["paths"] = {
                # Windows default DB path is "$APPDATA/assignment-list-pyqt5/data.db"
                "db_path": os.path.join(
                    os.path.expandvars("$APPDATA"),
                    "assignment-list-pyqt5",
                    "data.db"
                )
            }
        else:
            self.config["paths"] = {
                # Unix default DB path is "$HOME/.local/share/assignment-list-pyqt5/data.db"
                "db_path": os.path.join(
                    os.path.expanduser("~"),
                    ".local",
                    "share",
                    "assignment-list-pyqt5",
                    "data.db"
                )
            }

        self.updateConfig()

    def updateConfig(self):
        """
        Update the configuration file with values from self.config
        """
        # Attempt to create directory if necessary
        if not os.path.exists(os.path.dirname(self.config_path)):
            try:
                os.mkdir(os.path.dirname(self.config_path))
            except:
                print("Error: Could not create config directory '{}'".format(os.path.dirname(self.config_path)))
                sys.exit(1)

        # Attempt to write to file
        try:
            with open(self.config_path, 'w') as configfile:
                self.config.write(configfile)
        except:
            print("Error: Could not open config file '{}'".format(self.config_path))
            sys.exit(1)

        print("Successfully created config at {}".format(self.config_path))

if __name__ == "__main__":
    Config()