diff options
author | Louie S <louie@example.com> | 2023-09-16 09:36:14 -0400 |
---|---|---|
committer | Louie S <louie@example.com> | 2023-09-16 09:36:14 -0400 |
commit | 4292f8cf349f872fdd1d7065fbe159f43a7939a0 (patch) | |
tree | 68ae921b6d5b805c8d80eccc60c5195ef24b925d | |
parent | 0f71101423a793fcdb78d500da6070c2a49374cf (diff) |
Basic config file creation and loading working
-rwxr-xr-x | assignment-list.py | 4 | ||||
-rw-r--r-- | config.py | 68 | ||||
-rw-r--r-- | db_sqlite.py | 8 |
3 files changed, 79 insertions, 1 deletions
diff --git a/assignment-list.py b/assignment-list.py index 6953695..9897f9f 100755 --- a/assignment-list.py +++ b/assignment-list.py @@ -3,7 +3,8 @@ import sys import time from PyQt5.QtWidgets import QAction, QApplication, QHBoxLayout, QLabel, QMainWindow, QMessageBox, QPushButton, QScrollArea, QToolBar, QVBoxLayout, QWidget from PyQt5.QtGui import QFont -from PyQt5.QtCore import QDate, QDateTime, Qt +from PyQt5.QtCore import QDate, Qt +from config import Config from add_group_form import addGroupForm from edit_group_form import editGroupForm from add_entry_form import addEntryForm @@ -26,6 +27,7 @@ class AssignmentList(QMainWindow): self.setWindowTitle("Assignment List") self.createMenu() self.createToolbar() + Config() self.setupDB() self.displayWidgets() self.show() 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() diff --git a/db_sqlite.py b/db_sqlite.py index 60c8aa0..7fe2745 100644 --- a/db_sqlite.py +++ b/db_sqlite.py @@ -23,6 +23,14 @@ def createTables(): database = QSqlDatabase.addDatabase("QSQLITE") # SQlite version 3 database.setDatabaseName(Globals.db_path) + # Create database parent directory if necessary + if not os.path.exists(os.path.dirname(Globals.db_path)): + try: + os.mkdir(os.path.dirname(Globals.db_path)) + except: + print("Unable to open data source file.") + sys.exit(1) + if not database.open(): print("Unable to open data source file.") sys.exit(1) # Error out. TODO consider throwing a dialog instead |