From 4292f8cf349f872fdd1d7065fbe159f43a7939a0 Mon Sep 17 00:00:00 2001 From: Louie S Date: Sat, 16 Sep 2023 09:36:14 -0400 Subject: Basic config file creation and loading working --- config.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 config.py (limited to 'config.py') diff --git a/config.py b/config.py new file mode 100644 index 0000000..557de0e --- /dev/null +++ b/config.py @@ -0,0 +1,68 @@ +""" +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 + +Globals = __import__("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): + config = configparser.ConfigParser() + + try: + config.read(self.config_path) + except: + print("Could not parse config file '{}'".format(self.config_path)) + + if "paths" in config: + if config["paths"]["db_path"]: + Globals.db_path = config["paths"]["db_path"] + + def createConfig(self): + config = configparser.ConfigParser() + config["paths"] = { + "db_path": os.path.join( + os.path.expanduser("~"), + ".local", + "share", + "assignment-list-pyqt5", + "data.db" + ) + } + + # 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 create file + try: + with open(self.config_path, 'w') as configfile: + 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() -- cgit