summaryrefslogtreecommitdiff
path: root/src/config.py
diff options
context:
space:
mode:
authorLouie S <louie@example.com>2023-09-16 09:36:14 -0400
committerLouie S <louie@example.com>2023-09-16 09:36:14 -0400
commit604b485fab100785c4def10dc7242e6408e1f1c6 (patch)
tree916831dbe3e06c5165369e8bd0193753bba3274a /src/config.py
parentfe34cdaa6bd0efdc605ae89c96d7075645eaa5fd (diff)
Create project using setuptools
Diffstat (limited to 'src/config.py')
-rw-r--r--src/config.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/config.py b/src/config.py
new file mode 100644
index 0000000..2c80716
--- /dev/null
+++ b/src/config.py
@@ -0,0 +1,74 @@
+"""
+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 = os.path.join(
+ os.path.expanduser("~"),
+ ".config",
+ "assignment-list-pyqt5",
+ "config")
+
+ if not os.path.exists(self.config_path):
+ self.createConfig()
+
+ self.loadConfig()
+
+ 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()
+ self.config["paths"] = {
+ "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()