Fixed some potential issues with getting the trash folder handle, and made some minor improvements to index class methods

This commit is contained in:
Veronica K. B. Olsen
2020-09-30 21:50:59 +02:00
parent d382e70d72
commit 6fc5dad938
3 changed files with 27 additions and 28 deletions
+18 -26
View File
@@ -279,8 +279,8 @@ class NWIndex():
if theItem.itemLayout == nwItemLayout.NO_LAYOUT: if theItem.itemLayout == nwItemLayout.NO_LAYOUT:
logger.error("Not indexing no-layout item %s" % tHandle) logger.error("Not indexing no-layout item %s" % tHandle)
return False return False
if theRoot is None: if theItem.parHandle is None:
logger.error("Not indexing homeless item %s" % tHandle) logger.error("Not indexing orphaned item %s" % tHandle)
return False return False
# Run word counter for the whole text # Run word counter for the whole text
@@ -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 theItem.parHandle == self.theProject.projTree.trashRoot(): if self.theProject.projTree.isTrashRoot(theItem.parHandle):
logger.error("Not indexing trash item %s" % tHandle) logger.error("Not indexing trash item %s" % tHandle)
return False return False
if theRoot.itemClass == nwItemClass.ARCHIVE: if theRoot.itemClass == nwItemClass.ARCHIVE:
@@ -459,16 +459,12 @@ class NWIndex():
"""Validate and save the information about a reference to a tag """Validate and save the information about a reference to a tag
in another file. in another file.
""" """
isValid, theBits, thePos = self.scanThis(aLine) isValid, theBits, _ = self.scanThis(aLine)
if not isValid or len(theBits) == 0: if not isValid or len(theBits) == 0:
return False return False
sTitle = "T%06d" % nTitle sTitle = "T%06d" % nTitle
if sTitle not in self.refIndex[tHandle]: if sTitle in self.refIndex[tHandle] and theBits[0] != nwKeyWords.TAG_KEY:
logger.error("Cannot save tags to file %s, no title %s" % (tHandle, sTitle))
return False
if theBits[0] != nwKeyWords.TAG_KEY:
for aVal in theBits[1:]: for aVal in theBits[1:]:
self.refIndex[tHandle][sTitle]["tags"].append([nLine, theBits[0], aVal]) self.refIndex[tHandle][sTitle]["tags"].append([nLine, theBits[0], aVal])
@@ -574,15 +570,14 @@ class NWIndex():
""" """
theStructure = [] theStructure = []
for tItem in self.theProject.projTree: for tItem in self.theProject.projTree:
if tItem is None: if tItem is not None:
continue if not tItem.isExported and skipExcluded:
if not tItem.isExported and skipExcluded: continue
continue tHandle = tItem.itemHandle
tHandle = tItem.itemHandle if tHandle not in self.novelIndex:
if tHandle not in self.novelIndex: continue
continue for sTitle in sorted(self.novelIndex[tHandle].keys()):
for sTitle in sorted(self.novelIndex[tHandle].keys()): theStructure.append("%s:%s" % (tHandle, sTitle))
theStructure.append("%s:%s" % (tHandle, sTitle))
return theStructure return theStructure
@@ -624,14 +619,11 @@ class NWIndex():
if tHandle not in self.refIndex: if tHandle not in self.refIndex:
return theRefs return theRefs
try: for refTitle in self.refIndex[tHandle]:
for refTitle in self.refIndex[tHandle]: theTags = self.refIndex[tHandle][refTitle].get("tags", None)
for nLine, tKey, tTag in self.refIndex[tHandle][refTitle]["tags"]: for aTag in theTags:
if sTitle is None or sTitle == refTitle: if len(aTag) == 3 and (sTitle is None or sTitle == refTitle):
theRefs[tKey].append(tTag) theRefs[aTag[1]].append(aTag[2])
except Exception as e:
logger.error("Failed to generate reference list")
logger.error(str(e))
return theRefs return theRefs
+7
View File
@@ -213,6 +213,13 @@ class NWTree():
return self._trashRoot return self._trashRoot
return None 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): def archiveRoot(self):
"""Returns the handle of the archive folder, or None if there """Returns the handle of the archive folder, or None if there
isn't one. isn't one.
+2 -2
View File
@@ -209,7 +209,7 @@ class GuiProjectTree(QTreeWidget):
) )
return False return False
if pHandle == self.theProject.projTree.trashRoot(): if self.theProject.projTree.isTrashRoot(pHandle):
self.makeAlert( self.makeAlert(
"Cannot add new files or folders to the %s folder." % ( "Cannot add new files or folders to the %s folder." % (
nwLabels.CLASS_NAME[nwItemClass.TRASH] nwLabels.CLASS_NAME[nwItemClass.TRASH]
@@ -411,7 +411,7 @@ class GuiProjectTree(QTreeWidget):
return False return False
pHandle = nwItemS.parHandle 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 # 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.
doPermanent = False doPermanent = False