From ea34884d8cac13f14bb2d34a94f6ddc6e29ab324 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" Date: Sun, 12 May 2019 11:59:47 +0200 Subject: [PATCH] Updated config class to split the initialisation into a function --- nw/__init__.py | 4 +-- nw/config.py | 67 +++++++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/nw/__init__.py b/nw/__init__.py index c1388a58..8f5c06dc 100644 --- a/nw/__init__.py +++ b/nw/__init__.py @@ -62,10 +62,10 @@ logger = logging.getLogger(__name__) # Load the main config as a global object CONFIG = Config() +CONFIG.initConfig() def main(sysArgs): - """ - Parses command line, sets up logging, and launches main GUI. + """Parses command line, sets up logging, and launches main GUI. """ # Valid Input Options diff --git a/nw/config.py b/nw/config.py index 68b76411..2555c58c 100644 --- a/nw/config.py +++ b/nw/config.py @@ -6,7 +6,7 @@ This class reads and store the main preferences of the application File History: - Created: 2018-0+-22 [0.0.1] + Created: 2018-09-22 [0.0.1] """ @@ -28,25 +28,18 @@ class Config: def __init__(self): # Set Application Variables - self.appName = nw.__package__ - self.appHandle = nw.__package__.lower() - self.showGUI = True - self.debugGUI = False + self.appName = nw.__package__ + self.appHandle = nw.__package__.lower() + self.showGUI = True + self.debugGUI = False # Set Paths - self.confPath = user_config_dir(self.appHandle) - self.confFile = self.appHandle+".conf" - self.homePath = path.expanduser("~") - self.appPath = path.dirname(__file__) - self.guiPath = path.join(self.appPath,"gui") - self.themePath = path.join(self.appPath,"themes") - self.recentList = [""]*10 - - # If config folder does not exist, make it. - # This assumes that the os config folder itself exists. - # TODO: This does not work on Windows - if not path.isdir(self.confPath): - mkdir(self.confPath) + self.confPath = None + self.confFile = None + self.homePath = None + self.appPath = None + self.guiPath = None + self.themePath = None # Set default values self.confChanged = False @@ -81,13 +74,8 @@ class Config: self.spellLanguage = "en_GB" - # Check if config file exists - if path.isfile(path.join(self.confPath,self.confFile)): - self.loadConfig() - - # Save a copy of the default config if no file exists - if not path.isfile(path.join(self.confPath,self.confFile)): - self.saveConfig() + # Path + self.recentList = [""]*10 return @@ -95,6 +83,35 @@ class Config: # Actions ## + def initConfig(self, confPath=None): + + if confPath is None: + self.confPath = user_config_dir(self.appHandle) + else: + self.confPath = confPath + + self.confFile = self.appHandle+".conf" + self.homePath = path.expanduser("~") + self.appPath = path.dirname(__file__) + self.guiPath = path.join(self.appPath,"gui") + self.themePath = path.join(self.appPath,"themes") + + # If config folder does not exist, make it. + # This assumes that the os config folder itself exists. + # TODO: This does not work on Windows + if not path.isdir(self.confPath): + mkdir(self.confPath) + + # Check if config file exists + if path.isfile(path.join(self.confPath,self.confFile)): + # If it exists, load it + self.loadConfig() + else: + # If it does not exist, save a copy of the defaults + self.saveConfig() + + return + def loadConfig(self): logger.debug("Loading config file")