From 8037b3a31125c697dfc8a41fea06d53f2950e724 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 7 May 2020 00:38:25 +0200 Subject: [PATCH] Some improvements to the NWItem class, and removal of unused imports of it in the code --- nw/gui/elements/doctree.py | 2 +- nw/guimain.py | 2 +- nw/project/item.py | 47 +++++++++++++++++++------------------- 3 files changed, 26 insertions(+), 25 deletions(-) diff --git a/nw/gui/elements/doctree.py b/nw/gui/elements/doctree.py index b6858905..4fd08da8 100644 --- a/nw/gui/elements/doctree.py +++ b/nw/gui/elements/doctree.py @@ -34,7 +34,7 @@ from PyQt5.QtWidgets import ( QTreeWidget, QTreeWidgetItem, QAbstractItemView, QApplication, QMessageBox ) -from nw.project import NWItem, NWDoc +from nw.project import NWDoc from nw.constants import ( nwLabels, nwItemType, nwItemClass, nwItemLayout, nwAlert ) diff --git a/nw/guimain.py b/nw/guimain.py index fb221ba2..47b533d6 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -45,7 +45,7 @@ from nw.gui import ( GuiConfigEditor, GuiProjectEditor, GuiItemEditor, GuiProjectOutline, GuiSessionLogView, GuiDocMerge, GuiDocSplit, GuiProjectLoad ) -from nw.project import NWProject, NWDoc, NWItem, NWIndex, NWBackup +from nw.project import NWProject, NWDoc, NWIndex, NWBackup from nw.tools import countWords from nw.constants import nwFiles, nwItemType, nwAlert diff --git a/nw/project/item.py b/nw/project/item.py index 7843270a..3bd358af 100644 --- a/nw/project/item.py +++ b/nw/project/item.py @@ -57,6 +57,21 @@ class NWItem(): self.paraCount = 0 self.cursorPos = 0 + # Map of Setters + self._setMap = { + "name" : self.setName, + "order" : self.setOrder, + "type" : self.setType, + "class" : self.setClass, + "layout" : self.setLayout, + "status" : self.setStatus, + "expanded" : self.setExpanded, + "charCount" : self.setCharCount, + "wordCount" : self.setWordCount, + "paraCount" : self.setParaCount, + "cursorPos" : self.setCursorPos, + } + return ## @@ -64,6 +79,8 @@ class NWItem(): ## def packXML(self, xParent): + """Packs all the data in the class instance into an XML object. + """ xPack = etree.SubElement(xParent,"item",attrib={ "handle" : str(self.itemHandle), "order" : str(self.itemOrder), @@ -82,7 +99,8 @@ class NWItem(): xSub = self._subPack(xPack,"cursorPos", text=str(self.cursorPos), none=False) return xPack - def _subPack(self, xParent, name, attrib=None, text=None, none=True): + @staticmethod + def _subPack(xParent, name, attrib=None, text=None, none=True): if not none and (text == None or text == "None"): return None xSub = etree.SubElement(xParent,name,attrib=attrib) @@ -95,29 +113,12 @@ class NWItem(): ## def setFromTag(self, tagName, tagValue): + """Set a value from a given tag name rather than call the set + function directly. Useful when setting data read in from XML. + """ logger.verbose("Setting tag '%s' to value '%s'" % (tagName, str(tagValue))) - if tagName == "name": - self.setName(tagValue) - elif tagName == "order": - self.setOrder(tagValue) - elif tagName == "type": - self.setType(tagValue) - elif tagName == "class": - self.setClass(tagValue) - elif tagName == "layout": - self.setLayout(tagValue) - elif tagName == "status": - self.setStatus(tagValue) - elif tagName == "expanded": - self.setExpanded(tagValue) - elif tagName == "charCount": - self.setCharCount(tagValue) - elif tagName == "wordCount": - self.setWordCount(tagValue) - elif tagName == "paraCount": - self.setParaCount(tagValue) - elif tagName == "cursorPos": - self.setCursorPos(tagValue) + if tagName in self._setMap: + self._setMap[tagName](tagValue) else: logger.error("Unknown tag '%s'" % tagName) return