diff --git a/nw/common.py b/nw/common.py index add829a2..11546c88 100644 --- a/nw/common.py +++ b/nw/common.py @@ -34,66 +34,66 @@ from nw.constants import nwConst, nwUnicode logger = logging.getLogger(__name__) -def checkString(checkValue, defaultValue, allowNone=False): +def checkString(value, default, allowNone=False): """Check if a variable is a string or a none. """ if allowNone: - if checkValue is None: + if value is None: return None - if checkValue == "None": + if value == "None": return None - if isinstance(checkValue, str): - return str(checkValue) - return defaultValue + if isinstance(value, str): + return str(value) + return default -def checkInt(checkValue, defaultValue, allowNone=False): +def checkInt(value, default, allowNone=False): """Check if a variable is an integer or a none. """ if allowNone: - if checkValue is None: + if value is None: return None - if checkValue == "None": + if value == "None": return None try: - return int(checkValue) + return int(value) except Exception: - return defaultValue + return default -def checkBool(checkValue, defaultValue, allowNone=False): +def checkBool(value, default, allowNone=False): """Check if a variable is a boolean or a none. """ if allowNone: - if checkValue is None: + if value is None: return None - if checkValue == "None": + if value == "None": return None - if isinstance(checkValue, str): - if checkValue == "True": + if isinstance(value, str): + if value == "True": return True - elif checkValue == "False": + elif value == "False": return False else: - return defaultValue - elif isinstance(checkValue, int): - if checkValue == 1: + return default + elif isinstance(value, int): + if value == 1: return True - elif checkValue == 0: + elif value == 0: return False else: - return defaultValue - return defaultValue + return default + return default -def checkHandle(checkValue, defaultValue, allowNone=False): +def checkHandle(value, default, allowNone=False): """Check if a value is a handle. """ if allowNone: - if checkValue is None: + if value is None: return None - if checkValue == "None": + if value == "None": return None - if isHandle(checkValue): - return str(checkValue) - return defaultValue + if isHandle(value): + return str(value) + return default def isHandle(theString): """Check if a string is a valid novelWriter handle. @@ -109,6 +109,16 @@ def isHandle(theString): invalidChar = True return not invalidChar +def hexToInt(value, default=0): + """Convert a hex string to an integer. + """ + if isinstance(value, str): + try: + return int(value, 16) + except Exception: + return default + return default + def colRange(rgbStart, rgbEnd, nStep): """Generate a range of colours from one RGB value to another. """ diff --git a/nw/config.py b/nw/config.py index 3b7b262b..c1519f61 100644 --- a/nw/config.py +++ b/nw/config.py @@ -91,7 +91,7 @@ class Config: self.guiFont = "" # Defaults to system default font self.guiFontSize = 11 self.guiScale = 1.0 # Set automatically by Theme class - self.lastNotes = "" # The latest release notes that have been shown + self.lastNotes = "0x0" # The latest release notes that have been shown ## Sizes self.winGeometry = [1200, 650] diff --git a/nw/core/project.py b/nw/core/project.py index 3c423779..a22f7aa9 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -39,7 +39,7 @@ from nw.core.status import NWStatus from nw.core.options import OptionState from nw.common import ( checkString, checkBool, checkInt, isHandle, formatTimeStamp, - makeFileNameSafe + makeFileNameSafe, hexToInt ) from nw.constants import ( nwFiles, nwItemType, nwItemClass, nwItemLayout, nwLabels, nwAlert @@ -495,7 +495,7 @@ class NWProject(): # Check novelWriter Version # ========================= - if int(hexVersion, 16) > int(nw.__hexversion__, 16): + if hexToInt(hexVersion) > hexToInt(nw.__hexversion__): msgYes = self.theParent.askQuestion("Version Conflict", ( "This project was saved by a newer version of novelWriter, version %s. " "This is version %s. If you continue to open the project, some attributes " diff --git a/nw/guimain.py b/nw/guimain.py index c4ca3730..8c6f8b68 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -47,7 +47,7 @@ from nw.gui import ( ) from nw.core import NWProject, NWDoc, NWIndex from nw.constants import nwItemType, nwItemClass, nwAlert, nwLists -from nw.common import getGuiItem +from nw.common import getGuiItem, hexToInt logger = logging.getLogger(__name__) @@ -279,10 +279,10 @@ class GuiMain(QMainWindow): self.showProjectLoadDialog() # Show the latest release notes, if they haven't been shown before - if self.mainConf.lastNotes != nw.__version__: + if hexToInt(self.mainConf.lastNotes) < hexToInt(nw.__hexversion__): if self.mainConf.showGUI: self.showAboutNWDialog(showNotes=True) - self.mainConf.lastNotes = nw.__version__ + self.mainConf.lastNotes = nw.__hexversion__ logger.debug("novelWriter is ready ...") self.setStatus("novelWriter is ready ...") diff --git a/tests/test_base/test_base_common.py b/tests/test_base/test_base_common.py index b3ab74d5..79b6562a 100644 --- a/tests/test_base/test_base_common.py +++ b/tests/test_base/test_base_common.py @@ -25,7 +25,8 @@ import pytest from nw.common import ( checkString, checkBool, checkInt, colRange, formatInt, transferCase, - fuzzyTime, checkHandle, formatTimeStamp, formatTime + fuzzyTime, checkHandle, formatTimeStamp, formatTime, hexToInt, + makeFileNameSafe ) from tools import cmpList @@ -87,6 +88,19 @@ def testBaseCommon_CheckHandle(): # END Test testBaseCommon_CheckHandle +@pytest.mark.base +def testBaseCommon_HexToInt(): + """Test the hexToInt function. + """ + assert hexToInt(1) == 0 + assert hexToInt("1") == 1 + assert hexToInt("0xff") == 255 + assert hexToInt("0xffff") == 65535 + assert hexToInt("0xffffq") == 0 + assert hexToInt("0xffffq", 12) == 12 + +# END Test testBaseCommon_HexToInt + @pytest.mark.base def testBaseCommon_ColRange(): """Test the colRange function. @@ -210,3 +224,14 @@ def testBaseCommon_FuzzyTime(): assert fuzzyTime(47336400) == "2 years ago" # END Test testBaseCommon_FuzzyTime + +@pytest.mark.base +def testBaseCommon_MakeFileNameSafe(): + """Test the fuzzyTime function. + """ + assert makeFileNameSafe(" aaaa ") == "aaaa" + assert makeFileNameSafe("aaaa,bbbb") == "aaaabbbb" + assert makeFileNameSafe("aaaa\tbbbb") == "aaaabbbb" + assert makeFileNameSafe("aaaa bbbb") == "aaaa bbbb" + +# END Test testBaseCommon_MakeFileNameSafe