Proper handling of files with configparser
This commit is contained in:
+6
-6
@@ -310,10 +310,10 @@ class Config:
|
|||||||
"""
|
"""
|
||||||
logger.debug("Loading config file")
|
logger.debug("Loading config file")
|
||||||
cnfParse = configparser.ConfigParser()
|
cnfParse = configparser.ConfigParser()
|
||||||
|
cnfPath = path.join(self.confPath, self.confFile)
|
||||||
try:
|
try:
|
||||||
cnfParse.read_file(
|
with open(cnfPath, mode="r", encoding="utf8") as inFile:
|
||||||
open(path.join(self.confPath,self.confFile),mode="r",encoding="utf8")
|
cnfParse.read_file(inFile)
|
||||||
)
|
|
||||||
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))
|
logger.error(str(e))
|
||||||
@@ -562,10 +562,10 @@ class Config:
|
|||||||
cnfParse.set(cnfSec,"lastpath", str(self.lastPath))
|
cnfParse.set(cnfSec,"lastpath", str(self.lastPath))
|
||||||
|
|
||||||
# Write config file
|
# Write config file
|
||||||
|
cnfPath = path.join(self.confPath, self.confFile)
|
||||||
try:
|
try:
|
||||||
cnfParse.write(
|
with open(cnfPath, mode="w", encoding="utf8") as outFile:
|
||||||
open(path.join(self.confPath, self.confFile), mode="w", encoding="utf8")
|
cnfParse.write(outFile)
|
||||||
)
|
|
||||||
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")
|
||||||
|
|||||||
+12
-6
@@ -265,7 +265,8 @@ class GuiTheme:
|
|||||||
# Config File
|
# Config File
|
||||||
confParser = configparser.ConfigParser()
|
confParser = configparser.ConfigParser()
|
||||||
try:
|
try:
|
||||||
confParser.read_file(open(self.confFile,mode="r",encoding="utf8"))
|
with open(self.confFile, mode="r", encoding="utf8") as inFile:
|
||||||
|
confParser.read_file(inFile)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Could not load theme settings from: %s" % self.confFile)
|
logger.error("Could not load theme settings from: %s" % self.confFile)
|
||||||
return False
|
return False
|
||||||
@@ -319,7 +320,8 @@ class GuiTheme:
|
|||||||
|
|
||||||
confParser = configparser.ConfigParser()
|
confParser = configparser.ConfigParser()
|
||||||
try:
|
try:
|
||||||
confParser.read_file(open(self.syntaxFile, mode="r", encoding="utf8"))
|
with open(self.syntaxFile, mode="r", encoding="utf8") as inFile:
|
||||||
|
confParser.read_file(inFile)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Could not load syntax colours from: %s" % self.syntaxFile)
|
logger.error("Could not load syntax colours from: %s" % self.syntaxFile)
|
||||||
return False
|
return False
|
||||||
@@ -370,7 +372,8 @@ class GuiTheme:
|
|||||||
themeConf = path.join(self.mainConf.themeRoot, self.guiPath, themeDir, self.confName)
|
themeConf = path.join(self.mainConf.themeRoot, self.guiPath, themeDir, self.confName)
|
||||||
logger.verbose("Checking theme config for '%s'" % themeDir)
|
logger.verbose("Checking theme config for '%s'" % themeDir)
|
||||||
try:
|
try:
|
||||||
confParser.read_file(open(themeConf, mode="r", encoding="utf8"))
|
with open(themeConf, mode="r", encoding="utf8") as inFile:
|
||||||
|
confParser.read_file(inFile)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.theParent.makeAlert(
|
self.theParent.makeAlert(
|
||||||
["Could not load theme config file.",str(e)], nwAlert.ERROR
|
["Could not load theme config file.",str(e)], nwAlert.ERROR
|
||||||
@@ -402,7 +405,8 @@ class GuiTheme:
|
|||||||
continue
|
continue
|
||||||
logger.verbose("Checking theme syntax for '%s'" % syntaxFile)
|
logger.verbose("Checking theme syntax for '%s'" % syntaxFile)
|
||||||
try:
|
try:
|
||||||
confParser.read_file(open(syntaxPath, mode="r", encoding="utf8"))
|
with open(syntaxPath, mode="r", encoding="utf8") as inFile:
|
||||||
|
confParser.read_file(inFile)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.theParent.makeAlert(
|
self.theParent.makeAlert(
|
||||||
["Could not load syntax file.",str(e)], nwAlert.ERROR
|
["Could not load syntax file.",str(e)], nwAlert.ERROR
|
||||||
@@ -588,7 +592,8 @@ class GuiIcons:
|
|||||||
# Config File
|
# Config File
|
||||||
confParser = configparser.ConfigParser()
|
confParser = configparser.ConfigParser()
|
||||||
try:
|
try:
|
||||||
confParser.read_file(open(self.confFile, mode="r", encoding="utf8"))
|
with open(self.confFile, mode="r", encoding="utf8") as inFile:
|
||||||
|
confParser.read_file(inFile)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error("Could not load icon theme settings from: %s" % self.confFile)
|
logger.error("Could not load icon theme settings from: %s" % self.confFile)
|
||||||
return False
|
return False
|
||||||
@@ -682,7 +687,8 @@ class GuiIcons:
|
|||||||
themeConf = path.join(themePath, self.confName)
|
themeConf = path.join(themePath, self.confName)
|
||||||
logger.verbose("Checking icon theme config for '%s'" % themeDir)
|
logger.verbose("Checking icon theme config for '%s'" % themeDir)
|
||||||
try:
|
try:
|
||||||
confParser.read_file(open(themeConf, mode="r", encoding="utf8"))
|
with open(themeConf, mode="r", encoding="utf8") as inFile:
|
||||||
|
confParser.read_file(inFile)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.theParent.makeAlert(
|
self.theParent.makeAlert(
|
||||||
["Could not load theme config file.",str(e)], nwAlert.ERROR
|
["Could not load theme config file.",str(e)], nwAlert.ERROR
|
||||||
|
|||||||
Reference in New Issue
Block a user