Improve project tree iterator

This commit is contained in:
Veronica Berglyd Olsen
2022-11-15 21:49:15 +01:00
parent 7ce2fd29a6
commit a15d25370e
3 changed files with 18 additions and 37 deletions
+1 -3
View File
@@ -104,7 +104,7 @@ class NWIndex:
""" """
self.clearIndex() self.clearIndex()
for nwItem in self._project.tree: for nwItem in self._project.tree:
if nwItem is not None and nwItem.isFileType(): if nwItem.isFileType():
tHandle = nwItem.itemHandle tHandle = nwItem.itemHandle
theDoc = self._project.storage.getDocument(tHandle) theDoc = self._project.storage.getDocument(tHandle)
self.scanText(tHandle, theDoc.readDocument() or "") self.scanText(tHandle, theDoc.readDocument() or "")
@@ -794,8 +794,6 @@ class ItemIndex:
a given root handle, or for all if root handle is None. a given root handle, or for all if root handle is None.
""" """
for tItem in self._project.tree: for tItem in self._project.tree:
if tItem is None:
continue
if tItem.isNoteLayout(): if tItem.isNoteLayout():
continue continue
if skipExcl and not tItem.isActive: if skipExcl and not tItem.isActive:
+16 -33
View File
@@ -50,7 +50,6 @@ class NWTree:
self._treeRoots = {} # The root items of the tree self._treeRoots = {} # The root items of the tree
self._trashRoot = None # The handle of the trash root folder self._trashRoot = None # The handle of the trash root folder
self._archRoot = None # The handle of the archive root folder self._archRoot = None # The handle of the archive root folder
self._theIndex = 0 # The current iterator index
self._treeChanged = False # True if tree structure has changed self._treeChanged = False # True if tree structure has changed
return return
@@ -62,12 +61,11 @@ class NWTree:
def clear(self): def clear(self):
"""Clear the item tree entirely. """Clear the item tree entirely.
""" """
self._projTree = {} self._projTree = {}
self._treeOrder = [] self._treeOrder = []
self._treeRoots = {} self._treeRoots = {}
self._trashRoot = None self._trashRoot = None
self._archRoot = None self._archRoot = None
self._theIndex = 0
self._treeChanged = False self._treeChanged = False
return return
@@ -278,7 +276,7 @@ class NWTree:
""" """
for tHandle in self._treeOrder: for tHandle in self._treeOrder:
nwItem = self.__getitem__(tHandle) nwItem = self.__getitem__(tHandle)
if nwItem is not None and nwItem.isRootType(): if isinstance(nwItem, NWItem) and nwItem.isRootType():
if itemClass is None or nwItem.itemClass == itemClass: if itemClass is None or nwItem.itemClass == itemClass:
yield tHandle, nwItem yield tHandle, nwItem
return return
@@ -365,22 +363,18 @@ class NWTree:
return True return True
## ##
# Meta Methods # Special Methods
## ##
def __len__(self): def __len__(self):
"""Return the length counter. Does not check that it is correct! """The number of items in the project.
""" """
return len(self._treeOrder) return len(self._treeOrder)
def __bool__(self): def __bool__(self):
"""Returns True if the tree has any entries. """True if there are any items in the project.
""" """
return len(self._treeOrder) > 0 return bool(self._treeOrder)
##
# Item Access Methods
##
def __getitem__(self, tHandle): def __getitem__(self, tHandle):
"""Return a project item based on its handle. Returns None if """Return a project item based on its handle. Returns None if
@@ -417,25 +411,14 @@ class NWTree:
""" """
return tHandle in self._treeOrder return tHandle in self._treeOrder
##
# Iterator Methods
##
def __iter__(self): def __iter__(self):
"""Initiates the iterator. """Iterate through project items.
""" """
self._theIndex = 0 for tHandle in self._treeOrder:
return self tItem = self._projTree.get(tHandle)
if isinstance(tItem, NWItem):
def __next__(self): yield tItem
"""Returns the item from the next entry in the _treeOrder list. return
"""
if self._theIndex < len(self._treeOrder):
theItem = self.__getitem__(self._treeOrder[self._theIndex])
self._theIndex += 1
return theItem
else:
raise StopIteration
## ##
# Internal Functions # Internal Functions
+1 -1
View File
@@ -628,7 +628,7 @@ class GuiMain(QMainWindow):
fHandle = None # The first file handle we encounter fHandle = None # The first file handle we encounter
foundIt = False # We've found tHandle, pick the next we see foundIt = False # We've found tHandle, pick the next we see
for tItem in self.theProject.tree: for tItem in self.theProject.tree:
if tItem is None or not tItem.isFileType(): if not tItem.isFileType():
continue continue
if fHandle is None: if fHandle is None:
fHandle = tItem.itemHandle fHandle = tItem.itemHandle