Replace old option state class with new one everywhere

This commit is contained in:
Veronica K. B. Olsen
2020-02-19 19:43:44 +01:00
parent ecb739425b
commit 5f0b331655
6 changed files with 132 additions and 207 deletions
+2 -2
View File
@@ -2,7 +2,7 @@
from nw.tools.analyse import TextAnalysis
from nw.tools.legacy import projectMaintenance
from nw.tools.optlaststate import OptLastState
from nw.tools.optionstate import OptionState
from nw.tools.spellcheck import NWSpellCheck
from nw.tools.spellenchant import NWSpellEnchant
from nw.tools.spellsimple import NWSpellSimple
@@ -12,7 +12,7 @@ from nw.tools.wordcount import countWords
__all__ = [
"TextAnalysis",
"projectMaintenance",
"OptLastState",
"OptionState",
"NWSpellCheck",
"NWSpellEnchant",
"NWSpellSimple",
@@ -129,74 +129,22 @@ class OptionState():
return defaultValue
return defaultValue
# END Class OptionState
class OptLastState():
def __init__(self, theProject, theFile):
self.theProject = theProject
self.theFile = theFile
self.theState = {}
self.stringOpt = ()
self.boolOpt = ()
self.intOpt = ()
return
def loadSettings(self):
stateFile = path.join(self.theProject.projMeta,self.theFile)
theState = {}
if path.isfile(stateFile):
logger.debug("Loading options file")
try:
with open(stateFile,mode="r",encoding="utf8") as inFile:
theJson = inFile.read()
theState = json.loads(theJson)
except Exception as e:
logger.error("Failed to load options file")
logger.error(str(e))
return False
for anOpt in theState:
self.theState[anOpt] = theState[anOpt]
return True
def saveSettings(self):
stateFile = path.join(self.theProject.projMeta,self.theFile)
logger.debug("Saving options file")
try:
with open(stateFile,mode="w+",encoding="utf8") as outFile:
outFile.write(json.dumps(self.theState, indent=2))
except Exception as e:
logger.error("Failed to save options file")
logger.error(str(e))
return False
return True
def setSetting(self, setName, setValue):
if setName in self.theState:
self.theState[setName] = setValue
else:
return False
return True
def getSetting(self, setName):
if setName in self.stringOpt:
return checkString(self.theState[setName],self.theState[setName],False)
elif setName in self.boolOpt:
return checkBool(self.theState[setName],self.theState[setName],False)
elif setName in self.intOpt:
return checkInt(self.theState[setName],self.theState[setName],False)
return None
def validIntRange(self, theValue, intA, intB, intDefault):
"""Check that an int is in a given range. If it isn't, return
the default value.
"""
if isinstance(theValue, int):
if theValue >= intA and theValue <= intB:
return theValue
return intDefault
def validIntTuple(self, theValue, theTuple, intDefault):
"""Check that an int is an element of a tuple. If it isn't,
return the default value.
"""
if isinstance(theValue, int):
if theValue in theTuple:
return theValue
return intDefault
# END Class OptLastState
# END Class OptionState