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
+30 -20
View File
@@ -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