From 6fc5dad938c20aacc5ad74e4f6fbdc94d8d3a565 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 30 Sep 2020 21:50:59 +0200 Subject: [PATCH] Fixed some potential issues with getting the trash folder handle, and made some minor improvements to index class methods --- nw/core/index.py | 44 ++++++++++++++++++-------------------------- nw/core/tree.py | 7 +++++++ nw/gui/projtree.py | 4 ++-- 3 files changed, 27 insertions(+), 28 deletions(-) diff --git a/nw/core/index.py b/nw/core/index.py index 06cddf60..8925b07e 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -279,8 +279,8 @@ class NWIndex(): if theItem.itemLayout == nwItemLayout.NO_LAYOUT: logger.error("Not indexing no-layout item %s" % tHandle) return False - if theRoot is None: - logger.error("Not indexing homeless item %s" % tHandle) + if theItem.parHandle is None: + logger.error("Not indexing orphaned item %s" % tHandle) return False # Run word counter for the whole text @@ -288,7 +288,7 @@ class NWIndex(): self.textCounts[tHandle] = [cC, wC, pC] # If the file is archived or trashed, we don't index the file itself - if theItem.parHandle == self.theProject.projTree.trashRoot(): + if self.theProject.projTree.isTrashRoot(theItem.parHandle): logger.error("Not indexing trash item %s" % tHandle) return False if theRoot.itemClass == nwItemClass.ARCHIVE: @@ -459,16 +459,12 @@ class NWIndex(): """Validate and save the information about a reference to a tag in another file. """ - isValid, theBits, thePos = self.scanThis(aLine) + isValid, theBits, _ = self.scanThis(aLine) if not isValid or len(theBits) == 0: return False sTitle = "T%06d" % nTitle - if sTitle not in self.refIndex[tHandle]: - logger.error("Cannot save tags to file %s, no title %s" % (tHandle, sTitle)) - return False - - if theBits[0] != nwKeyWords.TAG_KEY: + if sTitle in self.refIndex[tHandle] and theBits[0] != nwKeyWords.TAG_KEY: for aVal in theBits[1:]: self.refIndex[tHandle][sTitle]["tags"].append([nLine, theBits[0], aVal]) @@ -574,15 +570,14 @@ class NWIndex(): """ theStructure = [] for tItem in self.theProject.projTree: - if tItem is None: - continue - if not tItem.isExported and skipExcluded: - continue - tHandle = tItem.itemHandle - if tHandle not in self.novelIndex: - continue - for sTitle in sorted(self.novelIndex[tHandle].keys()): - theStructure.append("%s:%s" % (tHandle, sTitle)) + if tItem is not None: + if not tItem.isExported and skipExcluded: + continue + tHandle = tItem.itemHandle + if tHandle not in self.novelIndex: + continue + for sTitle in sorted(self.novelIndex[tHandle].keys()): + theStructure.append("%s:%s" % (tHandle, sTitle)) return theStructure @@ -624,14 +619,11 @@ class NWIndex(): if tHandle not in self.refIndex: return theRefs - try: - for refTitle in self.refIndex[tHandle]: - for nLine, tKey, tTag in self.refIndex[tHandle][refTitle]["tags"]: - if sTitle is None or sTitle == refTitle: - theRefs[tKey].append(tTag) - except Exception as e: - logger.error("Failed to generate reference list") - logger.error(str(e)) + for refTitle in self.refIndex[tHandle]: + theTags = self.refIndex[tHandle][refTitle].get("tags", None) + for aTag in theTags: + if len(aTag) == 3 and (sTitle is None or sTitle == refTitle): + theRefs[aTag[1]].append(aTag[2]) return theRefs diff --git a/nw/core/tree.py b/nw/core/tree.py index 9389d2db..c51ae16c 100644 --- a/nw/core/tree.py +++ b/nw/core/tree.py @@ -213,6 +213,13 @@ class NWTree(): return self._trashRoot return None + def isTrashRoot(self, tHandle): + """Check if a handle is the trash folder. + """ + if self._trashRoot is None: + return False + return tHandle == self._trashRoot + def archiveRoot(self): """Returns the handle of the archive folder, or None if there isn't one. diff --git a/nw/gui/projtree.py b/nw/gui/projtree.py index 00b81dac..65c4bb1f 100644 --- a/nw/gui/projtree.py +++ b/nw/gui/projtree.py @@ -209,7 +209,7 @@ class GuiProjectTree(QTreeWidget): ) return False - if pHandle == self.theProject.projTree.trashRoot(): + if self.theProject.projTree.isTrashRoot(pHandle): self.makeAlert( "Cannot add new files or folders to the %s folder." % ( nwLabels.CLASS_NAME[nwItemClass.TRASH] @@ -411,7 +411,7 @@ class GuiProjectTree(QTreeWidget): return False pHandle = nwItemS.parHandle - if pHandle is not None and pHandle == self.theProject.projTree.trashRoot(): + if self.theProject.projTree.isTrashRoot(pHandle): # If the file is in the trash folder already, as the # user if they want to permanently delete the file. doPermanent = False