From 8d31428a9d14473cfdc0b5d0d789d25c70ca61be Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 24 Oct 2020 17:54:02 +0200 Subject: [PATCH] Improve a few functions in common and item --- nw/common.py | 13 ++++++------ nw/config.py | 2 +- nw/core/item.py | 50 +++++++++++++++++++++++++++------------------- tests/test_item.py | 2 +- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/nw/common.py b/nw/common.py index 021fa162..d5563d16 100644 --- a/nw/common.py +++ b/nw/common.py @@ -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" diff --git a/nw/config.py b/nw/config.py index 0aa6cf50..a82c133b 100644 --- a/nw/config.py +++ b/nw/config.py @@ -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. diff --git a/nw/core/item.py b/nw/core/item.py index 944b5f0c..6793af39 100644 --- a/nw/core/item.py +++ b/nw/core/item.py @@ -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 diff --git a/tests/test_item.py b/tests/test_item.py index 936ba185..95aa8470 100644 --- a/tests/test_item.py +++ b/tests/test_item.py @@ -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")