Improve a few functions in common and item
This commit is contained in:
+7
-6
@@ -152,13 +152,13 @@ def formatInt(theInt):
|
||||
theVal /= 1000.0
|
||||
if theVal < 1000.0:
|
||||
if theVal < 10.0:
|
||||
return "%4.2f%s%s" % (theVal, nwUnicode.U_THNSP, pF)
|
||||
return f"{theVal:4.2f}{nwUnicode.U_THNSP}{pF}"
|
||||
elif theVal < 100.0:
|
||||
return "%4.1f%s%s" % (theVal, nwUnicode.U_THNSP, pF)
|
||||
return f"{theVal:4.1f}{nwUnicode.U_THNSP}{pF}"
|
||||
else:
|
||||
return "%3.0f%s%s" % (theVal, nwUnicode.U_THNSP, pF)
|
||||
return f"{theVal:3.0f}{nwUnicode.U_THNSP}{pF}"
|
||||
|
||||
return "%d" % theInt
|
||||
return str(theInt)
|
||||
|
||||
def formatTimeStamp(theTime, fileSafe=False):
|
||||
"""Take a number (on the format returned by time.time()) and convert
|
||||
@@ -170,11 +170,12 @@ def formatTimeStamp(theTime, fileSafe=False):
|
||||
return datetime.fromtimestamp(theTime).strftime(nwConst.tStampFmt)
|
||||
|
||||
def formatTime(tS):
|
||||
"""Format the time spent in 00:00:00 format.
|
||||
"""Format a time in seconds in HH:MM:SS format or d-HH:MM:SS format
|
||||
if a full day or longer.
|
||||
"""
|
||||
if isinstance(tS, int):
|
||||
if tS >= 86400:
|
||||
return f"{tS//86400:d}-{tS//3600%24:02d}:{tS%3600//60:02d}:{tS%60:02d}"
|
||||
return f"{tS//86400:d}-{tS%86400//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
|
||||
else:
|
||||
return f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
|
||||
return "ERROR"
|
||||
|
||||
+1
-1
@@ -911,7 +911,7 @@ class Config:
|
||||
def _packList(self, inData):
|
||||
"""Pack a list of items into a comma separated string.
|
||||
"""
|
||||
return ", ".join(str(inVal) for inVal in inData)
|
||||
return ", ".join([str(inVal) for inVal in inData])
|
||||
|
||||
def _parseLine(self, cnfParse, cnfSec, cnfName, cnfType, cnfDefault):
|
||||
"""Parse a line and return the correct datatype.
|
||||
|
||||
+30
-20
@@ -29,7 +29,7 @@ import logging
|
||||
|
||||
from lxml import etree
|
||||
|
||||
from nw.common import checkInt
|
||||
from nw.common import checkInt, isHandle
|
||||
from nw.constants import nwItemType, nwItemClass, nwItemLayout
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -104,27 +104,37 @@ class NWItem():
|
||||
if "parent" in xItem.attrib:
|
||||
self.itemParent = xItem.attrib["parent"]
|
||||
|
||||
setMap = {
|
||||
"name" : self.setName,
|
||||
"order" : self.setOrder,
|
||||
"type" : self.setType,
|
||||
"class" : self.setClass,
|
||||
"layout" : self.setLayout,
|
||||
"status" : self.setStatus,
|
||||
"expanded" : self.setExpanded,
|
||||
"exported" : self.setExported,
|
||||
"charCount" : self.setCharCount,
|
||||
"wordCount" : self.setWordCount,
|
||||
"paraCount" : self.setParaCount,
|
||||
"cursorPos" : self.setCursorPos,
|
||||
}
|
||||
retStatus = True
|
||||
for xValue in xItem:
|
||||
if xValue.tag in setMap:
|
||||
setMap[xValue.tag](xValue.text)
|
||||
if xValue.tag == "name":
|
||||
self.setName(xValue.text)
|
||||
elif xValue.tag == "order":
|
||||
self.setOrder(xValue.text)
|
||||
elif xValue.tag == "type":
|
||||
self.setType(xValue.text)
|
||||
elif xValue.tag == "class":
|
||||
self.setClass(xValue.text)
|
||||
elif xValue.tag == "layout":
|
||||
self.setLayout(xValue.text)
|
||||
elif xValue.tag == "status":
|
||||
self.setStatus(xValue.text)
|
||||
elif xValue.tag == "expanded":
|
||||
self.setExpanded(xValue.text)
|
||||
elif xValue.tag == "exported":
|
||||
self.setExported(xValue.text)
|
||||
elif xValue.tag == "charCount":
|
||||
self.setCharCount(xValue.text)
|
||||
elif xValue.tag == "wordCount":
|
||||
self.setWordCount(xValue.text)
|
||||
elif xValue.tag == "paraCount":
|
||||
self.setParaCount(xValue.text)
|
||||
elif xValue.tag == "cursorPos":
|
||||
self.setCursorPos(xValue.text)
|
||||
else:
|
||||
logger.error("Unknown tag '%s'" % xValue.tag)
|
||||
retStatus = False
|
||||
|
||||
return True
|
||||
return retStatus
|
||||
|
||||
@staticmethod
|
||||
def _subPack(xParent, name, attrib=None, text=None, none=True):
|
||||
@@ -153,7 +163,7 @@ class NWItem():
|
||||
"""Set the item handle, and ensure it is valid.
|
||||
"""
|
||||
if isinstance(theHandle, str):
|
||||
if len(theHandle) == 13:
|
||||
if isHandle(theHandle):
|
||||
self.itemHandle = theHandle
|
||||
else:
|
||||
self.itemHandle = None
|
||||
@@ -167,7 +177,7 @@ class NWItem():
|
||||
if theParent is None:
|
||||
self.itemParent = None
|
||||
elif isinstance(theParent, str):
|
||||
if len(theParent) == 13:
|
||||
if isHandle(theParent):
|
||||
self.itemParent = theParent
|
||||
else:
|
||||
self.itemParent = None
|
||||
|
||||
+1
-1
@@ -252,7 +252,7 @@ def testItemXMLPackUnpack(nwDummy):
|
||||
xDummy = etree.SubElement(nwXML, "item", attrib={"handle": "0123456789abc"})
|
||||
xParam = etree.SubElement(xDummy, "invalid")
|
||||
xParam.text = "stuff"
|
||||
assert theItem.unpackXML(xDummy) # Passes, but not saved
|
||||
assert not theItem.unpackXML(xDummy)
|
||||
|
||||
# Pack Valid Item
|
||||
xDummy = etree.SubElement(nwXML, "group")
|
||||
|
||||
Reference in New Issue
Block a user