Add error handling and reporting for the Config class, whichs is initialised before the GUI

This commit is contained in:
Veronica K. B. Olsen
2020-02-23 17:15:20 +01:00
parent 5d129379a3
commit 39db69f199
2 changed files with 60 additions and 2 deletions
+46 -2
View File
@@ -42,9 +42,14 @@ class Config:
self.debugInfo = False self.debugInfo = False
self.cmdOpen = None self.cmdOpen = None
# Config Error Handling
self.hasError = False
self.errData = []
# Set Paths # Set Paths
self.confPath = None self.confPath = None
self.confFile = None self.confFile = None
self.dataPath = None
self.homePath = None self.homePath = None
self.lastPath = None self.lastPath = None
self.appPath = None self.appPath = None
@@ -172,6 +177,15 @@ class Config:
logger.info("Setting config from alternative path: %s" % confPath) logger.info("Setting config from alternative path: %s" % confPath)
self.confPath = 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.confFile = self.appHandle+".conf"
self.homePath = path.expanduser("~") self.homePath = path.expanduser("~")
self.lastPath = self.homePath self.lastPath = self.homePath
@@ -192,7 +206,10 @@ class Config:
except Exception as e: except Exception as e:
logger.error("Could not create folder: %s" % self.confPath) logger.error("Could not create folder: %s" % self.confPath)
logger.error(str(e)) 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 # Check if config file exists
if path.isfile(path.join(self.confPath,self.confFile)): 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 # If it does not exist, save a copy of the default values
self.saveConfig() 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 # Check the availability of optional packages
self._checkOptionalPackages() self._checkOptionalPackages()
@@ -222,6 +252,10 @@ class Config:
) )
except Exception as e: except Exception as e:
logger.error("Could not load config file") 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 return False
## Main ## Main
@@ -446,12 +480,16 @@ class Config:
self.confChanged = False self.confChanged = False
except Exception as e: except Exception as e:
logger.error("Could not save config file") 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 False
return True return True
## ##
# Setters # Setters and Getters
## ##
def setRecent(self, recentPath): def setRecent(self, recentPath):
@@ -516,6 +554,12 @@ class Config:
self.confChanged = True self.confChanged = True
return return
def getErrData(self):
errMessage = "<br>".join(self.errData)
self.hasError = False
self.errData = []
return errMessage
## ##
# Internal Functions # Internal Functions
## ##
+14
View File
@@ -166,6 +166,9 @@ class GuiMain(QMainWindow):
if self.mainConf.showGUI: if self.mainConf.showGUI:
self.show() self.show()
# Check that config loaded fine
self.reportConfErr()
self.initMain() self.initMain()
self.asProjTimer.start() self.asProjTimer.start()
self.asDocTimer.start() self.asDocTimer.start()
@@ -686,6 +689,16 @@ class GuiMain(QMainWindow):
return 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 # Main Window Actions
## ##
@@ -710,6 +723,7 @@ class GuiMain(QMainWindow):
self.mainConf.setMainPanePos(self.splitMain.sizes()) self.mainConf.setMainPanePos(self.splitMain.sizes())
self.mainConf.setDocPanePos(self.splitView.sizes()) self.mainConf.setDocPanePos(self.splitView.sizes())
self.mainConf.saveConfig() self.mainConf.saveConfig()
self.reportConfErr()
qApp.quit() qApp.quit()