Merge branch 'main' into dev

This commit is contained in:
Veronica K. B. Olsen
2021-01-17 20:58:15 +01:00
5 changed files with 71 additions and 36 deletions
+39 -29
View File
@@ -34,66 +34,66 @@ from nw.constants import nwConst, nwUnicode
logger = logging.getLogger(__name__) 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. """Check if a variable is a string or a none.
""" """
if allowNone: if allowNone:
if checkValue is None: if value is None:
return None return None
if checkValue == "None": if value == "None":
return None return None
if isinstance(checkValue, str): if isinstance(value, str):
return str(checkValue) return str(value)
return defaultValue return default
def checkInt(checkValue, defaultValue, allowNone=False): def checkInt(value, default, allowNone=False):
"""Check if a variable is an integer or a none. """Check if a variable is an integer or a none.
""" """
if allowNone: if allowNone:
if checkValue is None: if value is None:
return None return None
if checkValue == "None": if value == "None":
return None return None
try: try:
return int(checkValue) return int(value)
except Exception: 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. """Check if a variable is a boolean or a none.
""" """
if allowNone: if allowNone:
if checkValue is None: if value is None:
return None return None
if checkValue == "None": if value == "None":
return None return None
if isinstance(checkValue, str): if isinstance(value, str):
if checkValue == "True": if value == "True":
return True return True
elif checkValue == "False": elif value == "False":
return False return False
else: else:
return defaultValue return default
elif isinstance(checkValue, int): elif isinstance(value, int):
if checkValue == 1: if value == 1:
return True return True
elif checkValue == 0: elif value == 0:
return False return False
else: else:
return defaultValue return default
return defaultValue return default
def checkHandle(checkValue, defaultValue, allowNone=False): def checkHandle(value, default, allowNone=False):
"""Check if a value is a handle. """Check if a value is a handle.
""" """
if allowNone: if allowNone:
if checkValue is None: if value is None:
return None return None
if checkValue == "None": if value == "None":
return None return None
if isHandle(checkValue): if isHandle(value):
return str(checkValue) return str(value)
return defaultValue return default
def isHandle(theString): def isHandle(theString):
"""Check if a string is a valid novelWriter handle. """Check if a string is a valid novelWriter handle.
@@ -109,6 +109,16 @@ def isHandle(theString):
invalidChar = True invalidChar = True
return not invalidChar 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): def colRange(rgbStart, rgbEnd, nStep):
"""Generate a range of colours from one RGB value to another. """Generate a range of colours from one RGB value to another.
""" """
+1 -1
View File
@@ -91,7 +91,7 @@ class Config:
self.guiFont = "" # Defaults to system default font self.guiFont = "" # Defaults to system default font
self.guiFontSize = 11 self.guiFontSize = 11
self.guiScale = 1.0 # Set automatically by Theme class 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 ## Sizes
self.winGeometry = [1200, 650] self.winGeometry = [1200, 650]
+2 -2
View File
@@ -39,7 +39,7 @@ from nw.core.status import NWStatus
from nw.core.options import OptionState from nw.core.options import OptionState
from nw.common import ( from nw.common import (
checkString, checkBool, checkInt, isHandle, formatTimeStamp, checkString, checkBool, checkInt, isHandle, formatTimeStamp,
makeFileNameSafe makeFileNameSafe, hexToInt
) )
from nw.constants import ( from nw.constants import (
nwFiles, nwItemType, nwItemClass, nwItemLayout, nwLabels, nwAlert nwFiles, nwItemType, nwItemClass, nwItemLayout, nwLabels, nwAlert
@@ -495,7 +495,7 @@ class NWProject():
# Check novelWriter Version # Check novelWriter Version
# ========================= # =========================
if int(hexVersion, 16) > int(nw.__hexversion__, 16): if hexToInt(hexVersion) > hexToInt(nw.__hexversion__):
msgYes = self.theParent.askQuestion("Version Conflict", ( msgYes = self.theParent.askQuestion("Version Conflict", (
"This project was saved by a newer version of novelWriter, version %s. " "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 " "This is version %s. If you continue to open the project, some attributes "
+3 -3
View File
@@ -47,7 +47,7 @@ from nw.gui import (
) )
from nw.core import NWProject, NWDoc, NWIndex from nw.core import NWProject, NWDoc, NWIndex
from nw.constants import nwItemType, nwItemClass, nwAlert, nwLists from nw.constants import nwItemType, nwItemClass, nwAlert, nwLists
from nw.common import getGuiItem from nw.common import getGuiItem, hexToInt
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -279,10 +279,10 @@ class GuiMain(QMainWindow):
self.showProjectLoadDialog() self.showProjectLoadDialog()
# Show the latest release notes, if they haven't been shown before # 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: if self.mainConf.showGUI:
self.showAboutNWDialog(showNotes=True) self.showAboutNWDialog(showNotes=True)
self.mainConf.lastNotes = nw.__version__ self.mainConf.lastNotes = nw.__hexversion__
logger.debug("novelWriter is ready ...") logger.debug("novelWriter is ready ...")
self.setStatus("novelWriter is ready ...") self.setStatus("novelWriter is ready ...")
+26 -1
View File
@@ -25,7 +25,8 @@ import pytest
from nw.common import ( from nw.common import (
checkString, checkBool, checkInt, colRange, formatInt, transferCase, checkString, checkBool, checkInt, colRange, formatInt, transferCase,
fuzzyTime, checkHandle, formatTimeStamp, formatTime fuzzyTime, checkHandle, formatTimeStamp, formatTime, hexToInt,
makeFileNameSafe
) )
from tools import cmpList from tools import cmpList
@@ -87,6 +88,19 @@ def testBaseCommon_CheckHandle():
# END Test 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 @pytest.mark.base
def testBaseCommon_ColRange(): def testBaseCommon_ColRange():
"""Test the colRange function. """Test the colRange function.
@@ -210,3 +224,14 @@ def testBaseCommon_FuzzyTime():
assert fuzzyTime(47336400) == "2 years ago" assert fuzzyTime(47336400) == "2 years ago"
# END Test testBaseCommon_FuzzyTime # 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