Improve a few functions in common and item

This commit is contained in:
Veronica K. B. Olsen
2020-10-24 17:54:02 +02:00
parent c0ad631039
commit 8d31428a9d
4 changed files with 39 additions and 28 deletions
+7 -6
View File
@@ -152,13 +152,13 @@ def formatInt(theInt):
theVal /= 1000.0 theVal /= 1000.0
if theVal < 1000.0: if theVal < 1000.0:
if theVal < 10.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: elif theVal < 100.0:
return "%4.1f%s%s" % (theVal, nwUnicode.U_THNSP, pF) return f"{theVal:4.1f}{nwUnicode.U_THNSP}{pF}"
else: 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): def formatTimeStamp(theTime, fileSafe=False):
"""Take a number (on the format returned by time.time()) and convert """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) return datetime.fromtimestamp(theTime).strftime(nwConst.tStampFmt)
def formatTime(tS): 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 isinstance(tS, int):
if tS >= 86400: 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: else:
return f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}" return f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
return "ERROR" return "ERROR"
+1 -1
View File
@@ -911,7 +911,7 @@ class Config:
def _packList(self, inData): def _packList(self, inData):
"""Pack a list of items into a comma separated string. """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): def _parseLine(self, cnfParse, cnfSec, cnfName, cnfType, cnfDefault):
"""Parse a line and return the correct datatype. """Parse a line and return the correct datatype.
+30 -20
View File
@@ -29,7 +29,7 @@ import logging
from lxml import etree from lxml import etree
from nw.common import checkInt from nw.common import checkInt, isHandle
from nw.constants import nwItemType, nwItemClass, nwItemLayout from nw.constants import nwItemType, nwItemClass, nwItemLayout
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -104,27 +104,37 @@ class NWItem():
if "parent" in xItem.attrib: if "parent" in xItem.attrib:
self.itemParent = xItem.attrib["parent"] self.itemParent = xItem.attrib["parent"]
setMap = { retStatus = True
"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,
}
for xValue in xItem: for xValue in xItem:
if xValue.tag in setMap: if xValue.tag == "name":
setMap[xValue.tag](xValue.text) 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: else:
logger.error("Unknown tag '%s'" % xValue.tag) logger.error("Unknown tag '%s'" % xValue.tag)
retStatus = False
return True return retStatus
@staticmethod @staticmethod
def _subPack(xParent, name, attrib=None, text=None, none=True): 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. """Set the item handle, and ensure it is valid.
""" """
if isinstance(theHandle, str): if isinstance(theHandle, str):
if len(theHandle) == 13: if isHandle(theHandle):
self.itemHandle = theHandle self.itemHandle = theHandle
else: else:
self.itemHandle = None self.itemHandle = None
@@ -167,7 +177,7 @@ class NWItem():
if theParent is None: if theParent is None:
self.itemParent = None self.itemParent = None
elif isinstance(theParent, str): elif isinstance(theParent, str):
if len(theParent) == 13: if isHandle(theParent):
self.itemParent = theParent self.itemParent = theParent
else: else:
self.itemParent = None self.itemParent = None
+1 -1
View File
@@ -252,7 +252,7 @@ def testItemXMLPackUnpack(nwDummy):
xDummy = etree.SubElement(nwXML, "item", attrib={"handle": "0123456789abc"}) xDummy = etree.SubElement(nwXML, "item", attrib={"handle": "0123456789abc"})
xParam = etree.SubElement(xDummy, "invalid") xParam = etree.SubElement(xDummy, "invalid")
xParam.text = "stuff" xParam.text = "stuff"
assert theItem.unpackXML(xDummy) # Passes, but not saved assert not theItem.unpackXML(xDummy)
# Pack Valid Item # Pack Valid Item
xDummy = etree.SubElement(nwXML, "group") xDummy = etree.SubElement(nwXML, "group")