From 39db69f199df4e48baed9f612710df82e285ceb3 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sun, 23 Feb 2020 17:15:20 +0100 Subject: [PATCH] Add error handling and reporting for the Config class, whichs is initialised before the GUI --- nw/config.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- nw/guimain.py | 14 ++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/nw/config.py b/nw/config.py index eb0720a4..5d8b3ec9 100644 --- a/nw/config.py +++ b/nw/config.py @@ -42,9 +42,14 @@ class Config: self.debugInfo = False self.cmdOpen = None + # Config Error Handling + self.hasError = False + self.errData = [] + # Set Paths self.confPath = None self.confFile = None + self.dataPath = None self.homePath = None self.lastPath = None self.appPath = None @@ -172,6 +177,15 @@ class Config: logger.info("Setting config from alternative path: %s" % confPath) self.confPath = confPath + if self.verQtValue >= 50400: + dataRoot = QStandardPaths.writableLocation(QStandardPaths.AppDataLocation) + else: + dataRoot = QStandardPaths.writableLocation(QStandardPaths.DataLocation) + self.dataPath = path.join(path.abspath(dataRoot), self.appHandle) + + logger.verbose("Config path: %s" % self.confPath) + logger.verbose("Data path: %s" % self.dataPath) + self.confFile = self.appHandle+".conf" self.homePath = path.expanduser("~") self.lastPath = self.homePath @@ -192,7 +206,10 @@ class Config: except Exception as e: logger.error("Could not create folder: %s" % self.confPath) logger.error(str(e)) - return False + self.hasError = True + self.errData.append("Could not create folder: %s" % self.confPath) + self.errData.append(str(e)) + self.confPath = None # Check if config file exists if path.isfile(path.join(self.confPath,self.confFile)): @@ -202,6 +219,19 @@ class Config: # If it does not exist, save a copy of the default values self.saveConfig() + # If data folder does not exist, make it. + # This assumes that the os data folder itself exists. + if not path.isdir(self.dataPath): + try: + mkdir(self.dataPath) + except Exception as e: + logger.error("Could not create folder: %s" % self.dataPath) + logger.error(str(e)) + self.hasError = True + self.errData.append("Could not create folder: %s" % self.dataPath) + self.errData.append(str(e)) + self.dataPath = None + # Check the availability of optional packages self._checkOptionalPackages() @@ -222,6 +252,10 @@ class Config: ) except Exception as e: logger.error("Could not load config file") + logger.error(str(e)) + self.hasError = True + self.errData.append("Could not load config file") + self.errData.append(str(e)) return False ## Main @@ -446,12 +480,16 @@ class Config: self.confChanged = False except Exception as e: logger.error("Could not save config file") + logger.error(str(e)) + self.hasError = True + self.errData.append("Could not save config file") + self.errData.append(str(e)) return False return True ## - # Setters + # Setters and Getters ## def setRecent(self, recentPath): @@ -516,6 +554,12 @@ class Config: self.confChanged = True return + def getErrData(self): + errMessage = "
".join(self.errData) + self.hasError = False + self.errData = [] + return errMessage + ## # Internal Functions ## diff --git a/nw/guimain.py b/nw/guimain.py index 670b1534..2c0d15e9 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -166,6 +166,9 @@ class GuiMain(QMainWindow): if self.mainConf.showGUI: self.show() + # Check that config loaded fine + self.reportConfErr() + self.initMain() self.asProjTimer.start() self.asDocTimer.start() @@ -686,6 +689,16 @@ class GuiMain(QMainWindow): return + def reportConfErr(self): + """Checks if the Config module has any errors to report, and let + the user know if this is the case. The Config module caches + errors since it is initialised before the GUI itself. + """ + if self.mainConf.hasError: + self.makeAlert(self.mainConf.getErrData(), nwAlert.ERROR) + return True + return False + ## # Main Window Actions ## @@ -710,6 +723,7 @@ class GuiMain(QMainWindow): self.mainConf.setMainPanePos(self.splitMain.sizes()) self.mainConf.setDocPanePos(self.splitView.sizes()) self.mainConf.saveConfig() + self.reportConfErr() qApp.quit()