Clean up handling of root and class info for items

This commit is contained in:
Veronica Berglyd Olsen
2022-04-03 17:45:17 +02:00
parent 37970bd446
commit bdde6a2080
7 changed files with 121 additions and 100 deletions
+2 -2
View File
@@ -227,10 +227,10 @@ class NWIndex():
if theItem.itemParent is None:
logger.info("Not indexing orphaned item '%s'", tHandle)
return False
if self.theProject.projTree.isTrashRoot(theItem.itemParent):
if theItem.itemClass == nwItemClass.TRASH:
logger.debug("Not indexing trash item '%s'", tHandle)
return False
if self.theProject.projTree.getItemClass(tHandle) == nwItemClass.ARCHIVE:
if theItem.itemClass == nwItemClass.ARCHIVE:
logger.debug("Not indexing archived item '%s'", tHandle)
return False
+14 -14
View File
@@ -53,7 +53,7 @@ logger = logging.getLogger(__name__)
class NWProject():
FILE_VERSION = "1.4"
FILE_VERSION = "1.4" # The current project file format version
def __init__(self, theParent):
@@ -129,6 +129,7 @@ class NWProject():
newItem.setClass(rootClass)
newItem.setStatus(0)
self.projTree.append(None, None, newItem)
self.projTree.updateItemData(newItem.itemHandle)
return newItem.itemHandle
def newFolder(self, folderName, folderClass, pHandle):
@@ -140,6 +141,7 @@ class NWProject():
newItem.setClass(folderClass)
newItem.setStatus(0)
self.projTree.append(None, pHandle, newItem)
self.projTree.updateItemData(newItem.itemHandle)
return newItem.itemHandle
def newFile(self, fileName, fileClass, pHandle):
@@ -156,6 +158,7 @@ class NWProject():
newItem.setClass(fileClass)
newItem.setStatus(0)
self.projTree.append(None, pHandle, newItem)
self.projTree.updateItemData(newItem.itemHandle)
return newItem.itemHandle
def trashFolder(self):
@@ -168,6 +171,7 @@ class NWProject():
newItem.setType(nwItemType.TRASH)
newItem.setClass(nwItemClass.TRASH)
self.projTree.append(None, None, newItem)
self.projTree.updateItemData(newItem.itemHandle)
return newItem.itemHandle
return trashHandle
@@ -605,10 +609,15 @@ class NWProject():
self.mainConf.updateRecentCache(self.projPath, self.projName, self.lastWCount, time())
self.mainConf.saveRecentCache()
self.theParent.setStatus(self.tr("Opened Project: {0}").format(self.projName))
# Check the project tree consistency
for tItem in self.projTree:
tHandle = tItem.itemHandle
logger.verbose("Checking item '%s'", tHandle)
if not self.projTree.updateItemData(tHandle):
logger.error("There was a problem item '%s', and it has been removed", tHandle)
del self.projTree[tHandle] # The file will be re-added as orphaned
self._scanProjectFolder()
self._checkProjectTree()
self._loadProjectLocalisation()
self.updateWordCounts()
@@ -617,6 +626,7 @@ class NWProject():
self._writeLockFile()
self.setProjectChanged(False)
self.theParent.setStatus(self.tr("Opened Project: {0}").format(self.projName))
return True
@@ -1341,17 +1351,6 @@ class NWProject():
xEntry.text = aValue
return
def _checkProjectTree(self):
"""Check the project tree and make sure all items have sensible
values.
"""
for tItem in self.projTree:
tHandle = tItem.itemHandle
logger.verbose("Checking item '%s'", tHandle)
if tItem.itemRoot is None:
self.projTree.updateItemRoot(tHandle)
logger.warning("Corrected the root setting of item '%s'", tHandle)
def _scanProjectFolder(self):
"""Scan the project folder and check that the files in it are
also in the project XML file. If they aren't, import them as
@@ -1441,6 +1440,7 @@ class NWProject():
orphItem.setClass(oClass)
orphItem.setLayout(oLayout)
self.projTree.append(oHandle, oParent, orphItem)
self.projTree.updateItemData(orphItem.itemHandle)
if noWhere:
self.theParent.makeAlert(self.tr(
+65 -66
View File
@@ -113,7 +113,6 @@ class NWTree():
self._projTree[tHandle] = nwItem
self._treeOrder.append(tHandle)
self.updateItemRoot(tHandle)
self._setTreeChanged(True)
return True
@@ -208,9 +207,30 @@ class NWTree():
return novelWords, noteWords
##
# Tree Structure Methods
# Tree Item Methods
##
def updateItemData(self, tHandle):
"""Update the root item handle of a given item. Returns True if
a root was found and data updated, otherwise False.
"""
tItem = self.__getitem__(tHandle)
if tItem is None:
return False
iItem = tItem
for _ in range(nwConst.MAX_DEPTH + 1):
if iItem.itemParent is None:
tItem.setRoot(iItem.itemHandle)
tItem.setClass(iItem.itemClass)
return True
else:
iItem = self.__getitem__(iItem.itemParent)
if iItem is None:
return False
return False
def checkType(self, tHandle, itemType):
"""Return true of item exists and is of the specified item type.
"""
@@ -219,70 +239,6 @@ class NWTree():
return False
return tItem.itemType == itemType
def trashRoot(self):
"""Returns the handle of the trash folder, or None if there
isn't one.
"""
if self._trashRoot:
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.
"""
if self._archRoot:
return self._archRoot
return None
def findRoot(self, theClass):
"""Find the root item for a given class.
Note: This returns the first item for class CUSTOM.
"""
for aRoot in self._treeRoots:
tItem = self.__getitem__(aRoot)
if tItem is None:
continue
if theClass == tItem.itemClass:
return tItem.itemHandle
return None
def isRoot(self, tHandle):
"""Check if a handle is a root item.
"""
return tHandle in self._treeRoots
def updateItemRoot(self, tHandle):
"""Update the root item handle of a given item.
"""
tItem = self.__getitem__(tHandle)
iItem = tItem
if iItem is not None:
for _ in range(nwConst.MAX_DEPTH + 1):
if iItem.itemParent is None:
tItem.setRoot(iItem.itemHandle)
return iItem.itemHandle
else:
tHandle = iItem.itemParent
iItem = self.__getitem__(tHandle)
return None
def getItemClass(self, tHandle):
"""Return the class of a given item.
"""
tItem = self.__getitem__(tHandle)
if tItem is not None:
if tItem.itemRoot in self._treeRoots:
return self._treeRoots[tItem.itemRoot].itemClass
return nwItemClass.NO_CLASS
def getItemPath(self, tHandle):
"""Iterate upwards in the tree until we find the item with
parent None, the root item, and return the list of handles.
@@ -305,6 +261,49 @@ class NWTree():
tTree.append(tHandle)
return tTree
##
# Tree Root Methods
##
def isRoot(self, tHandle):
"""Check if a handle is a root item.
"""
return tHandle in self._treeRoots
def isTrashRoot(self, tHandle):
"""Check if a handle is the trash folder.
"""
if self._trashRoot is None:
return False
return tHandle == self._trashRoot
def trashRoot(self):
"""Returns the handle of the trash folder, or None if there
isn't one.
"""
if self._trashRoot:
return self._trashRoot
return None
def archiveRoot(self):
"""Returns the handle of the archive folder, or None if there
isn't one.
"""
if self._archRoot:
return self._archRoot
return None
def findRoot(self, theClass):
"""Find the first root item for a given class.
"""
for aRoot in self._treeRoots:
tItem = self.__getitem__(aRoot)
if tItem is None:
continue
if theClass == tItem.itemClass:
return tItem.itemHandle
return None
##
# Setters
##