Code cleanup in core classes and rename of parHandle to itemParent in NWItem class

This commit is contained in:
Veronica K. B. Olsen
2020-10-15 19:08:35 +02:00
parent d973f28755
commit 1923a55443
11 changed files with 47 additions and 41 deletions
+7 -4
View File
@@ -67,7 +67,9 @@ class NWDoc():
def openDocument(self, tHandle, showStatus=True, isOrphan=False): def openDocument(self, tHandle, showStatus=True, isOrphan=False):
"""Open a document from handle, capturing potential file system """Open a document from handle, capturing potential file system
errors and parse meta data. errors and parse meta data. If the document doesn't exist on
disk, return an empty string. If something went wrong, return
None.
""" """
if not isHandle(tHandle): if not isHandle(tHandle):
return None return None
@@ -125,8 +127,8 @@ class NWDoc():
return theText return theText
def saveDocument(self, docText): def saveDocument(self, docText):
"""Save the document via temp file in case of save failure, and """Save the document. The file is saved via a temp file in case
in any case keep a backup of the file. of save failure. Returns True if successful, False if not.
""" """
if self._docHandle is None: if self._docHandle is None:
return False return False
@@ -139,6 +141,7 @@ class NWDoc():
docPath = os.path.join(self.theProject.projContent, docFile) docPath = os.path.join(self.theProject.projContent, docFile)
docTemp = os.path.join(self.theProject.projContent, docFile+"~") docTemp = os.path.join(self.theProject.projContent, docFile+"~")
# DocMeta line
if self._theItem is None: if self._theItem is None:
docMeta = "" docMeta = ""
else: else:
@@ -171,7 +174,7 @@ class NWDoc():
return True return True
def deleteDocument(self, tHandle): def deleteDocument(self, tHandle):
"""Permanently delete a document source file and its backups """Permanently delete a document source file and related files
from the project data folder. from the project data folder.
""" """
if not isHandle(tHandle): if not isHandle(tHandle):
+3 -3
View File
@@ -279,7 +279,7 @@ class NWIndex():
if theItem.itemLayout == nwItemLayout.NO_LAYOUT: if theItem.itemLayout == nwItemLayout.NO_LAYOUT:
logger.info("Not indexing no-layout item %s" % tHandle) logger.info("Not indexing no-layout item %s" % tHandle)
return False return False
if theItem.parHandle is None: if theItem.itemParent is None:
logger.info("Not indexing orphaned item %s" % tHandle) logger.info("Not indexing orphaned item %s" % tHandle)
return False return False
@@ -288,7 +288,7 @@ class NWIndex():
self.textCounts[tHandle] = [cC, wC, pC] self.textCounts[tHandle] = [cC, wC, pC]
# If the file is archived or trashed, we don't index the file itself # If the file is archived or trashed, we don't index the file itself
if self.theProject.projTree.isTrashRoot(theItem.parHandle): if self.theProject.projTree.isTrashRoot(theItem.itemParent):
logger.info("Not indexing trash item %s" % tHandle) logger.info("Not indexing trash item %s" % tHandle)
return False return False
if theRoot.itemClass == nwItemClass.ARCHIVE: if theRoot.itemClass == nwItemClass.ARCHIVE:
@@ -583,7 +583,7 @@ class NWIndex():
def getCounts(self, tHandle, sTitle=None): def getCounts(self, tHandle, sTitle=None):
"""Returns the counts for a file, or a section of a file """Returns the counts for a file, or a section of a file
starting at title nTitle. starting at title sTitle if it is provided.
""" """
cC = 0 cC = 0
wC = 0 wC = 0
+11 -8
View File
@@ -42,7 +42,7 @@ class NWItem():
self.itemName = "" self.itemName = ""
self.itemHandle = None self.itemHandle = None
self.parHandle = None self.itemParent = None
self.itemOrder = None self.itemOrder = None
self.itemType = nwItemType.NO_TYPE self.itemType = nwItemType.NO_TYPE
self.itemClass = nwItemClass.NO_CLASS self.itemClass = nwItemClass.NO_CLASS
@@ -70,7 +70,7 @@ class NWItem():
xPack = etree.SubElement(xParent, "item", attrib={ xPack = etree.SubElement(xParent, "item", attrib={
"handle" : str(self.itemHandle), "handle" : str(self.itemHandle),
"order" : str(self.itemOrder), "order" : str(self.itemOrder),
"parent" : str(self.parHandle), "parent" : str(self.itemParent),
}) })
self._subPack(xPack, "name", text=str(self.itemName)) self._subPack(xPack, "name", text=str(self.itemName))
self._subPack(xPack, "type", text=str(self.itemType.name)) self._subPack(xPack, "type", text=str(self.itemType.name))
@@ -85,6 +85,7 @@ class NWItem():
self._subPack(xPack, "cursorPos", text=str(self.cursorPos), none=False) self._subPack(xPack, "cursorPos", text=str(self.cursorPos), none=False)
else: else:
self._subPack(xPack, "expanded", text=str(self.isExpanded)) self._subPack(xPack, "expanded", text=str(self.isExpanded))
return return
def unpackXML(self, xItem): def unpackXML(self, xItem):
@@ -101,7 +102,7 @@ class NWItem():
return False return False
if "parent" in xItem.attrib: if "parent" in xItem.attrib:
self.parHandle = xItem.attrib["parent"] self.itemParent = xItem.attrib["parent"]
setMap = { setMap = {
"name" : self.setName, "name" : self.setName,
@@ -131,9 +132,11 @@ class NWItem():
""" """
if not none and (text is None or text == "None"): if not none and (text is None or text == "None"):
return None return None
xSub = etree.SubElement(xParent, name, attrib=attrib) xAttr = {} if attrib is None else attrib
xSub = etree.SubElement(xParent, name, attrib=xAttr)
if text is not None: if text is not None:
xSub.text = text xSub.text = text
return return
## ##
@@ -162,14 +165,14 @@ class NWItem():
"""Set the parent handle, and ensure that it is valid. """Set the parent handle, and ensure that it is valid.
""" """
if theParent is None: if theParent is None:
self.parHandle = None self.itemParent = None
elif isinstance(theParent, str): elif isinstance(theParent, str):
if len(theParent) == 13: if len(theParent) == 13:
self.parHandle = theParent self.itemParent = theParent
else: else:
self.parHandle = None self.itemParent = None
else: else:
self.parHandle = None self.itemParent = None
return return
def setOrder(self, theOrder): def setOrder(self, theOrder):
+3 -3
View File
@@ -1139,16 +1139,16 @@ class NWProject():
# Technically a bug since treeOrder is built from the # Technically a bug since treeOrder is built from the
# same data as projTree # same data as projTree
continue continue
elif tItem.parHandle is None: elif tItem.itemParent is None:
# Item is a root, or already been identified as an # Item is a root, or already been identified as an
# orphaned item # orphaned item
sentItems.append(tHandle) sentItems.append(tHandle)
yield tItem yield tItem
elif tItem.parHandle in sentItems: elif tItem.itemParent in sentItems:
# Item's parent has been sent, so all is fine # Item's parent has been sent, so all is fine
sentItems.append(tHandle) sentItems.append(tHandle)
yield tItem yield tItem
elif tItem.parHandle in iterItems: elif tItem.itemParent in iterItems:
# Item's parent exists, but hasn't been sent yet, so add # Item's parent exists, but hasn't been sent yet, so add
# it again to the end # it again to the end
logger.warning("Item %s found before its parent" % tHandle) logger.warning("Item %s found before its parent" % tHandle)
+5 -5
View File
@@ -134,7 +134,7 @@ class NWTree():
for xItem in xContent: for xItem in xContent:
nwItem = NWItem(self.theProject) nwItem = NWItem(self.theProject)
if nwItem.unpackXML(xItem): if nwItem.unpackXML(xItem):
self.append(nwItem.itemHandle, nwItem.parHandle, nwItem) self.append(nwItem.itemHandle, nwItem.itemParent, nwItem)
nwItem.saveInitialCount() nwItem.saveInitialCount()
return True return True
@@ -261,10 +261,10 @@ class NWTree():
tItem = self.__getitem__(tHandle) tItem = self.__getitem__(tHandle)
if tItem is not None: if tItem is not None:
for i in range(nwConst.maxDepth + 1): for i in range(nwConst.maxDepth + 1):
if tItem.parHandle is None: if tItem.itemParent is None:
return tItem return tItem
else: else:
tHandle = tItem.parHandle tHandle = tItem.itemParent
tItem = self.__getitem__(tHandle) tItem = self.__getitem__(tHandle)
return None return None
@@ -279,10 +279,10 @@ class NWTree():
if tItem is not None: if tItem is not None:
tTree.append(tHandle) tTree.append(tHandle)
for i in range(nwConst.maxDepth + 1): for i in range(nwConst.maxDepth + 1):
if tItem.parHandle is None: if tItem.itemParent is None:
return tTree return tTree
else: else:
tHandle = tItem.parHandle tHandle = tItem.itemParent
tItem = self.__getitem__(tHandle) tItem = self.__getitem__(tHandle)
if tItem is None: if tItem is None:
return tTree return tTree
+2 -2
View File
@@ -684,8 +684,8 @@ class GuiBuildNovel(QDialog):
isNone |= theItem.itemLayout == nwItemLayout.NO_LAYOUT isNone |= theItem.itemLayout == nwItemLayout.NO_LAYOUT
isNone |= theItem.itemClass == nwItemClass.NO_CLASS isNone |= theItem.itemClass == nwItemClass.NO_CLASS
isNone |= theItem.itemClass == nwItemClass.TRASH isNone |= theItem.itemClass == nwItemClass.TRASH
isNone |= theItem.parHandle == self.theProject.projTree.trashRoot() isNone |= theItem.itemParent == self.theProject.projTree.trashRoot()
isNone |= theItem.parHandle is None isNone |= theItem.itemParent is None
isNote = theItem.itemLayout == nwItemLayout.NOTE isNote = theItem.itemLayout == nwItemLayout.NOTE
isNovel = not isNone and not isNote isNovel = not isNone and not isNote
+1 -1
View File
@@ -127,7 +127,7 @@ class GuiDocMerge(QDialog):
), nwAlert.ERROR) ), nwAlert.ERROR)
return return
nHandle = self.theProject.newFile(srcItem.itemName, srcItem.itemClass, srcItem.parHandle) nHandle = self.theProject.newFile(srcItem.itemName, srcItem.itemClass, srcItem.itemParent)
newItem = self.theProject.projTree[nHandle] newItem = self.theProject.projTree[nHandle]
newItem.setStatus(srcItem.itemStatus) newItem.setStatus(srcItem.itemStatus)
+2 -2
View File
@@ -154,7 +154,7 @@ class GuiDocSplit(QDialog):
return return
# Check that another folder can be created # Check that another folder can be created
parTree = self.theProject.projTree.getItemPath(srcItem.parHandle) parTree = self.theProject.projTree.getItemPath(srcItem.itemParent)
if len(parTree) >= nwConst.maxDepth - 1: if len(parTree) >= nwConst.maxDepth - 1:
self.theParent.makeAlert(( self.theParent.makeAlert((
"Cannot add new folder for the document split. " "Cannot add new folder for the document split. "
@@ -176,7 +176,7 @@ class GuiDocSplit(QDialog):
# Create the folder # Create the folder
fHandle = self.theProject.newFolder( fHandle = self.theProject.newFolder(
srcItem.itemName, srcItem.itemClass, srcItem.parHandle srcItem.itemName, srcItem.itemClass, srcItem.itemParent
) )
self.theParent.treeView.revealNewTreeItem(fHandle) self.theParent.treeView.revealNewTreeItem(fHandle)
logger.verbose("Creating folder %s" % fHandle) logger.verbose("Creating folder %s" % fHandle)
+6 -6
View File
@@ -219,7 +219,7 @@ class GuiProjectTree(QTreeWidget):
pItem = self.theProject.projTree[pHandle] pItem = self.theProject.projTree[pHandle]
if pItem.itemType == nwItemType.FILE: if pItem.itemType == nwItemType.FILE:
nHandle = pHandle nHandle = pHandle
pHandle = pItem.parHandle pHandle = pItem.itemParent
# If we again have no home, give up # If we again have no home, give up
if pHandle is None: if pHandle is None:
@@ -270,7 +270,7 @@ class GuiProjectTree(QTreeWidget):
""" """
nwItem = self.theProject.projTree[tHandle] nwItem = self.theProject.projTree[tHandle]
trItem = self._addTreeItem(nwItem, nHandle) trItem = self._addTreeItem(nwItem, nHandle)
pHandle = nwItem.parHandle pHandle = nwItem.itemParent
if pHandle is not None and pHandle in self.theMap: if pHandle is not None and pHandle in self.theMap:
self.theMap[pHandle].setExpanded(True) self.theMap[pHandle].setExpanded(True)
self.clearSelection() self.clearSelection()
@@ -430,7 +430,7 @@ class GuiProjectTree(QTreeWidget):
logger.error("Could not delete item") logger.error("Could not delete item")
return False return False
pHandle = nwItemS.parHandle pHandle = nwItemS.itemParent
if self.theProject.projTree.isTrashRoot(pHandle): if self.theProject.projTree.isTrashRoot(pHandle):
# If the file is in the trash folder already, as the # If the file is in the trash folder already, as the
# user if they want to permanently delete the file. # user if they want to permanently delete the file.
@@ -815,7 +815,7 @@ class GuiProjectTree(QTreeWidget):
project tree. project tree.
""" """
tHandle = nwItem.itemHandle tHandle = nwItem.itemHandle
pHandle = nwItem.parHandle pHandle = nwItem.itemParent
tClass = nwItem.itemClass tClass = nwItem.itemClass
newItem = QTreeWidgetItem([""]*4) newItem = QTreeWidgetItem([""]*4)
@@ -1022,11 +1022,11 @@ class GuiProjectTreeMenu(QMenu):
trashHandle = self.theTree.theProject.projTree.trashRoot() trashHandle = self.theTree.theProject.projTree.trashRoot()
inTrash = theItem.parHandle == trashHandle and trashHandle is not None inTrash = theItem.itemParent == trashHandle and trashHandle is not None
isTrash = theItem.itemHandle == trashHandle and trashHandle is not None isTrash = theItem.itemHandle == trashHandle and trashHandle is not None
isFile = theItem.itemType == nwItemType.FILE isFile = theItem.itemType == nwItemType.FILE
isArch = theRoot.itemClass == nwItemClass.ARCHIVE isArch = theRoot.itemClass == nwItemClass.ARCHIVE
isOrph = isFile and theItem.parHandle is None isOrph = isFile and theItem.itemParent is None
showOpen = isFile showOpen = isFile
showView = isFile showView = isFile
+5 -5
View File
@@ -31,13 +31,13 @@ def testItemSettersSimple(nwDummy):
# Parent # Parent
theItem.setParent(None) theItem.setParent(None)
assert theItem.parHandle is None assert theItem.itemParent is None
theItem.setParent(123) theItem.setParent(123)
assert theItem.parHandle is None assert theItem.itemParent is None
theItem.setParent("0123456789abcdef") theItem.setParent("0123456789abcdef")
assert theItem.parHandle is None assert theItem.itemParent is None
theItem.setParent("0123456789abc") theItem.setParent("0123456789abc")
assert theItem.parHandle == "0123456789abc" assert theItem.itemParent == "0123456789abc"
# Order # Order
theItem.setOrder(None) theItem.setOrder(None)
@@ -227,7 +227,7 @@ def testItemXMLPackUnpack(nwDummy):
# Unpack # Unpack
assert theItem.unpackXML(xContent[0]) assert theItem.unpackXML(xContent[0])
assert theItem.itemHandle == "0123456789abc" assert theItem.itemHandle == "0123456789abc"
assert theItem.parHandle == "0123456789abc" assert theItem.itemParent == "0123456789abc"
assert theItem.itemOrder == 1 assert theItem.itemOrder == 1
assert theItem.isExpanded assert theItem.isExpanded
assert theItem.paraCount == 3 assert theItem.paraCount == 3
+2 -2
View File
@@ -521,7 +521,7 @@ def testProjectOrphanedFiles(nwDummy, nwLipsum):
assert oItem is not None assert oItem is not None
assert oItem.itemName == "Mars" assert oItem.itemName == "Mars"
assert oItem.itemHandle == "636b6aa9b697b" assert oItem.itemHandle == "636b6aa9b697b"
assert oItem.parHandle is None assert oItem.itemParent is None
assert oItem.itemClass == nwItemClass.WORLD assert oItem.itemClass == nwItemClass.WORLD
assert oItem.itemType == nwItemType.FILE assert oItem.itemType == nwItemType.FILE
assert oItem.itemLayout == nwItemLayout.NOTE assert oItem.itemLayout == nwItemLayout.NOTE
@@ -531,7 +531,7 @@ def testProjectOrphanedFiles(nwDummy, nwLipsum):
assert oItem is not None assert oItem is not None
assert oItem.itemName == "Orphaned File 1" assert oItem.itemName == "Orphaned File 1"
assert oItem.itemHandle == "736b6aa9b697b" assert oItem.itemHandle == "736b6aa9b697b"
assert oItem.parHandle is None assert oItem.itemParent is None
assert oItem.itemClass == nwItemClass.NO_CLASS assert oItem.itemClass == nwItemClass.NO_CLASS
assert oItem.itemType == nwItemType.FILE assert oItem.itemType == nwItemType.FILE
assert oItem.itemLayout == nwItemLayout.NO_LAYOUT assert oItem.itemLayout == nwItemLayout.NO_LAYOUT