Moved the value check functions to a common.py file
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""novelWriter Common Functions
|
||||
|
||||
novelWriter – Common Functions
|
||||
================================
|
||||
Various functions used multiple places
|
||||
|
||||
File History:
|
||||
Created: 2019-05-12 [0.1.0]
|
||||
|
||||
"""
|
||||
|
||||
import logging
|
||||
import nw
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def checkString(checkValue, defaultValue, allowNone=False):
|
||||
if allowNone:
|
||||
if checkValue == None: return None
|
||||
if checkValue == "None": return None
|
||||
if isinstance(checkValue,str): return str(checkValue)
|
||||
return defaultValue
|
||||
|
||||
def checkInt(checkValue, defaultValue, allowNone=False):
|
||||
if allowNone:
|
||||
if checkValue == None: return None
|
||||
if checkValue == "None": return None
|
||||
try:
|
||||
return int(checkValue)
|
||||
except:
|
||||
return defaultValue
|
||||
|
||||
def checkBool(checkValue, defaultValue, allowNone=False):
|
||||
if allowNone:
|
||||
if checkValue == None: return None
|
||||
if checkValue == "None": return None
|
||||
if isinstance(checkValue, str):
|
||||
if checkValue == "True":
|
||||
return True
|
||||
elif checkValue == "False":
|
||||
return False
|
||||
else:
|
||||
return defaultValue
|
||||
elif isinstance(checkValue, int):
|
||||
if checkValue == 1:
|
||||
return True
|
||||
elif checkValue == 0:
|
||||
return False
|
||||
else:
|
||||
return defaultValue
|
||||
return defaultValue
|
||||
+9
-21
@@ -13,11 +13,12 @@
|
||||
import logging
|
||||
import nw
|
||||
|
||||
from os import path, mkdir
|
||||
from lxml import etree
|
||||
from datetime import datetime
|
||||
from os import path, mkdir
|
||||
from lxml import etree
|
||||
from datetime import datetime
|
||||
|
||||
from nw.enum import nwItemType, nwItemClass, nwItemLayout
|
||||
from nw.enum import nwItemType, nwItemClass, nwItemLayout
|
||||
from nw.common import checkInt
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -153,7 +154,7 @@ class NWItem():
|
||||
return
|
||||
|
||||
def setStatus(self, theStatus):
|
||||
theStatus = self._checkInt(theStatus,0)
|
||||
theStatus = checkInt(theStatus,0)
|
||||
self.itemStatus = theStatus
|
||||
return
|
||||
|
||||
@@ -169,31 +170,18 @@ class NWItem():
|
||||
##
|
||||
|
||||
def setCharCount(self, theCount):
|
||||
theCount = self._checkInt(theCount,0)
|
||||
theCount = checkInt(theCount,0)
|
||||
self.charCount = theCount
|
||||
return
|
||||
|
||||
def setWordCount(self, theCount):
|
||||
theCount = self._checkInt(theCount,0)
|
||||
theCount = checkInt(theCount,0)
|
||||
self.wordCount = theCount
|
||||
return
|
||||
|
||||
def setParaCount(self, theCount):
|
||||
theCount = self._checkInt(theCount,0)
|
||||
theCount = checkInt(theCount,0)
|
||||
self.paraCount = theCount
|
||||
return
|
||||
|
||||
##
|
||||
# Internal Functions
|
||||
##
|
||||
|
||||
def _checkInt(self,checkValue,defaultValue,allowNone=False):
|
||||
if allowNone:
|
||||
if checkValue == None: return None
|
||||
if checkValue == "None": return None
|
||||
try:
|
||||
return int(checkValue)
|
||||
except:
|
||||
return defaultValue
|
||||
|
||||
# END Class NWItem
|
||||
|
||||
+4
-39
@@ -20,6 +20,7 @@ from datetime import datetime
|
||||
from time import time
|
||||
|
||||
from nw.enum import nwItemType, nwItemClass, nwItemLayout
|
||||
from nw.common import checkString, checkBool
|
||||
from nw.project.item import NWItem
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -197,7 +198,7 @@ class NWProject():
|
||||
for xItem in xChild:
|
||||
if xItem.text is None: continue
|
||||
if xItem.tag == "spellCheck":
|
||||
self.spellCheck = self._checkBool(xItem.text,False)
|
||||
self.spellCheck = checkBool(xItem.text,False)
|
||||
elif xChild.tag == "content":
|
||||
logger.debug("Found project content")
|
||||
for xItem in xChild:
|
||||
@@ -427,8 +428,8 @@ class NWProject():
|
||||
return
|
||||
|
||||
def _appendItem(self, tHandle, pHandle, nwItem):
|
||||
tHandle = self._checkString(tHandle,self._makeHandle(),False)
|
||||
pHandle = self._checkString(pHandle,None,True)
|
||||
tHandle = checkString(tHandle,self._makeHandle(),False)
|
||||
pHandle = checkString(pHandle,None,True)
|
||||
logger.verbose("Adding entry %s with parent %s" % (str(tHandle),str(pHandle)))
|
||||
|
||||
nwItem.setHandle(tHandle)
|
||||
@@ -466,40 +467,4 @@ class NWProject():
|
||||
itemHandle = self._makeHandle(addSeed+"!")
|
||||
return itemHandle
|
||||
|
||||
def _checkString(self,checkValue,defaultValue,allowNone=False):
|
||||
if allowNone:
|
||||
if checkValue == None: return None
|
||||
if checkValue == "None": return None
|
||||
if isinstance(checkValue,str): return str(checkValue)
|
||||
return defaultValue
|
||||
|
||||
def _checkInt(self,checkValue,defaultValue,allowNone=False):
|
||||
if allowNone:
|
||||
if checkValue == None: return None
|
||||
if checkValue == "None": return None
|
||||
try:
|
||||
return int(checkValue)
|
||||
except:
|
||||
return defaultValue
|
||||
|
||||
def _checkBool(self,checkValue,defaultValue,allowNone=False):
|
||||
if allowNone:
|
||||
if checkValue == None: return None
|
||||
if checkValue == "None": return None
|
||||
if isinstance(checkValue, str):
|
||||
if checkValue == "True":
|
||||
return True
|
||||
elif checkValue == "False":
|
||||
return False
|
||||
else:
|
||||
return defaultValue
|
||||
elif isinstance(checkValue, int):
|
||||
if checkValue == 1:
|
||||
return True
|
||||
elif checkValue == 0:
|
||||
return False
|
||||
else:
|
||||
return defaultValue
|
||||
return defaultValue
|
||||
|
||||
# END Class NWProject
|
||||
|
||||
Reference in New Issue
Block a user