diff --git a/novelwriter/common.py b/novelwriter/common.py index ec4bd5bd..e0c2dc67 100644 --- a/novelwriter/common.py +++ b/novelwriter/common.py @@ -45,52 +45,54 @@ logger = logging.getLogger(__name__) # Checker Functions # =============================================================================================== # -def checkString(value, default, allowNone=False): +def checkStringNone(value, default): """Check if a variable is a string or a None. """ - if allowNone and (value is None or value == "None"): + if value is None or value == "None": return None if isinstance(value, str): return str(value) return default -def checkInt(value, default, allowNone=False): - """Check if a variable is an integer or a None. +def checkString(value, default): + """Check if a variable is a string. + """ + if isinstance(value, str): + return str(value) + return default + + +def checkInt(value, default): + """Check if a variable is an integer. """ - if allowNone and (value is None or value == "None"): - return None try: return int(value) except Exception: return default -def checkFloat(value, default, allowNone=False): - """Check if a variable is a float or a None. +def checkFloat(value, default): + """Check if a variable is a float. """ - if allowNone and (value is None or value == "None"): - return None try: return float(value) except Exception: return default -def checkBool(value, default, allowNone=False): - """Check if a variable is a boolean or a None. +def checkBool(value, default): + """Check if a variable is a boolean. """ - if allowNone and (value is None or value == "None"): - return None - - if isinstance(value, str): + if isinstance(value, bool): + return value + elif isinstance(value, str): if value == "True": return True elif value == "False": return False else: return default - elif isinstance(value, int): if value == 1: return True @@ -98,7 +100,6 @@ def checkBool(value, default, allowNone=False): return False else: return default - return default @@ -174,16 +175,6 @@ def hexToInt(value, default=0): return default -def checkIntRange(value, first, last, default): - """Check that an int is in a given range. If it isn't, return the - default value. - """ - if isinstance(value, int): - if value >= first and value <= last: - return value - return default - - def minmax(value, minVal, maxVal): """Make sure an integer is between min and max value (inclusive). """ @@ -263,7 +254,7 @@ def splitVersionNumber(value): and patch, and computes an integer value aabbcc. """ if not isinstance(value, str): - return [0, 0, 0, 0] + return 0, 0, 0, 0 vMajor = 0 vMinor = 0 @@ -282,7 +273,7 @@ def splitVersionNumber(value): vInt = vMajor*10000 + vMinor*100 + vPatch - return [vMajor, vMinor, vPatch, vInt] + return vMajor, vMinor, vPatch, vInt def transferCase(theSource, theTarget): diff --git a/novelwriter/core/options.py b/novelwriter/core/options.py index e1fcbc66..9d840e0f 100644 --- a/novelwriter/core/options.py +++ b/novelwriter/core/options.py @@ -188,8 +188,7 @@ class OptionState: the default value. """ if group in self._theState: - if name in self._theState[group]: - return checkBool(self._theState[group].get(name, default), default) + return checkBool(self._theState[group].get(name, default), default) return default def getEnum(self, group, name, lookup, default): diff --git a/novelwriter/core/project.py b/novelwriter/core/project.py index dcfa66d7..da3c4910 100644 --- a/novelwriter/core/project.py +++ b/novelwriter/core/project.py @@ -44,7 +44,7 @@ from novelwriter.core.document import NWDoc from novelwriter.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert from novelwriter.error import logException from novelwriter.common import ( - checkString, checkBool, checkInt, isHandle, formatTimeStamp, + checkString, checkBool, checkInt, checkStringNone, isHandle, formatTimeStamp, makeFileNameSafe, hexToInt, minmax, simplified ) from novelwriter.constants import trConst, nwFiles, nwLabels @@ -593,13 +593,13 @@ class NWProject: if xItem.text is None: continue if xItem.tag == "name": - self.projName = checkString(simplified(xItem.text), "") + self.projName = simplified(checkString(xItem.text, "")) logger.verbose("Working Title: '%s'", self.projName) elif xItem.tag == "title": - self.bookTitle = checkString(simplified(xItem.text), "") + self.bookTitle = simplified(checkString(xItem.text, "")) logger.verbose("Title is '%s'", self.bookTitle) elif xItem.tag == "author": - author = checkString(simplified(xItem.text), "") + author = simplified(checkString(xItem.text, "")) if author: self.bookAuthors.append(author) logger.verbose("Author: '%s'", author) @@ -618,25 +618,25 @@ class NWProject: if xItem.tag == "doBackup": self.doBackup = checkBool(xItem.text, False) elif xItem.tag == "language": - self.projLang = checkString(xItem.text, None, True) + self.projLang = checkStringNone(xItem.text, None) elif xItem.tag == "spellCheck": self.spellCheck = checkBool(xItem.text, False) elif xItem.tag == "spellLang": - self.projSpell = checkString(xItem.text, None, True) + self.projSpell = checkStringNone(xItem.text, None) elif xItem.tag == "lastEdited": - self.lastEdited = checkString(xItem.text, None, True) + self.lastEdited = checkStringNone(xItem.text, None) elif xItem.tag == "lastViewed": - self.lastViewed = checkString(xItem.text, None, True) + self.lastViewed = checkStringNone(xItem.text, None) elif xItem.tag == "lastNovel": - self.lastNovel = checkString(xItem.text, None, True) + self.lastNovel = checkStringNone(xItem.text, None) elif xItem.tag == "lastOutline": - self.lastOutline = checkString(xItem.text, None, True) + self.lastOutline = checkStringNone(xItem.text, None) elif xItem.tag == "lastWordCount": - self.lastWCount = checkInt(xItem.text, 0, False) + self.lastWCount = checkInt(xItem.text, 0) elif xItem.tag == "novelWordCount": - self.lastNovelWC = checkInt(xItem.text, 0, False) + self.lastNovelWC = checkInt(xItem.text, 0) elif xItem.tag == "notesWordCount": - self.lastNotesWC = checkInt(xItem.text, 0, False) + self.lastNotesWC = checkInt(xItem.text, 0) elif xItem.tag == "status": self.statusItems.unpackXML(xItem) elif xItem.tag == "importance": @@ -645,12 +645,12 @@ class NWProject: for xEntry in xItem: if xEntry.tag == "entry" and "key" in xEntry.attrib: self.autoReplace[xEntry.attrib["key"]] = checkString( - xEntry.text, None, False + xEntry.text, "ERROR" ) elif xItem.tag == "titleFormat": titleFormat = self.titleFormat.copy() for xEntry in xItem: - titleFormat[xEntry.tag] = checkString(xEntry.text, "", False) + titleFormat[xEntry.tag] = checkString(xEntry.text, "") self.setTitleFormat(titleFormat) elif xChild.tag == "content": @@ -1091,7 +1091,7 @@ class NWProject: def setSpellLang(self, theLang): """Set the project-specific spell check language. """ - theLang = checkString(theLang, None, True) + theLang = checkStringNone(theLang, None) if self.projSpell != theLang: self.projSpell = theLang self.setProjectChanged(True) @@ -1101,7 +1101,7 @@ class NWProject: def setProjectLang(self, theLang): """Set the project-specific language. """ - theLang = checkString(theLang, None, True) + theLang = checkStringNone(theLang, None) if self.projLang != theLang: self.projLang = theLang self._loadProjectLocalisation() diff --git a/novelwriter/tools/writingstats.py b/novelwriter/tools/writingstats.py index 8d372641..5fcdd605 100644 --- a/novelwriter/tools/writingstats.py +++ b/novelwriter/tools/writingstats.py @@ -39,7 +39,7 @@ from PyQt5.QtWidgets import ( from novelwriter.enum import nwAlert from novelwriter.error import formatException -from novelwriter.common import formatTime, checkInt, checkIntRange, checkIntTuple +from novelwriter.common import formatTime, checkInt, checkIntTuple, minmax from novelwriter.custom import QSwitch from novelwriter.constants import nwConst, nwFiles @@ -116,7 +116,7 @@ class GuiWritingStats(QDialog): hHeader.setTextAlignment(self.C_IDLE, Qt.AlignRight) hHeader.setTextAlignment(self.C_COUNT, Qt.AlignRight) - sortCol = checkIntRange(pOptions.getInt("GuiWritingStats", "sortCol", 0), 0, 2, 0) + sortCol = minmax(pOptions.getInt("GuiWritingStats", "sortCol", 0), 0, 2) sortOrder = checkIntTuple( pOptions.getInt("GuiWritingStats", "sortOrder", Qt.DescendingOrder), (Qt.AscendingOrder, Qt.DescendingOrder), Qt.DescendingOrder