From e4d160ee0b9a840a4904be7912f68975172ee15c Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 2 Jan 2021 21:37:46 +0100 Subject: [PATCH] Make novel structure function in index class an iterable function instead --- nw/core/index.py | 16 +++++++--------- nw/gui/noveltree.py | 31 ++++++++----------------------- nw/gui/outline.py | 27 +++++++-------------------- tests/test_core_index.py | 27 +++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 56 deletions(-) diff --git a/nw/core/index.py b/nw/core/index.py index d56429d3..d1bc7449 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -549,12 +549,11 @@ class NWIndex(): # Extract Data ## - def getNovelStructure(self, skipExcluded=True): - """Builds a list of all titles in the novel, in the correct - order as they appear in the tree view and in the respective - document files, but skipping all note files. + def novelStructure(self, skipExcluded=True): + """Iterate over all titles in the novel, in the correct order as + they appear in the tree view and in the respective document + files, but skipping all note files. """ - theStructure = [] for tItem in self.theProject.projTree: if tItem is not None: if not tItem.isExported and skipExcluded: @@ -562,10 +561,9 @@ class NWIndex(): 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 + for sTitle in sorted(self.novelIndex[tHandle]): + tKey = "%s:%s" % (tHandle, sTitle) + yield tKey, tHandle, sTitle, self.novelIndex[tHandle][sTitle] def getCounts(self, tHandle, sTitle=None): """Returns the counts for a file, or a section of a file diff --git a/nw/gui/noveltree.py b/nw/gui/noveltree.py index 163889d9..b04339e8 100644 --- a/nw/gui/noveltree.py +++ b/nw/gui/noveltree.py @@ -34,6 +34,7 @@ from PyQt5.QtCore import Qt, QSize from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem, QAbstractItemView from nw.constants import nwKeyWords +from nw.common import checkInt logger = logging.getLogger(__name__) @@ -209,10 +210,7 @@ class GuiNovelTree(QTreeWidget): """ theData = tItem.data(self.C_TITLE, Qt.UserRole) tHandle = theData[0] - try: - tLine = int(theData[1]) - except Exception: - tLine = 1 + tLine = checkInt(theData[1], 1) logger.verbose("User selected entry with handle %s on line %s" % (tHandle, tLine)) self.theParent.openDocument(tHandle, tLine=tLine-1, doScroll=True) @@ -239,23 +237,12 @@ class GuiNovelTree(QTreeWidget): """ self.clearTree() - for titleKey in self.theIndex.getNovelStructure(skipExcluded=True): + for tKey, tHandle, sTitle, novIdx in self.theIndex.novelStructure(skipExcluded=True): - if len(titleKey) < 16: - continue - - tHandle = titleKey[:13] - sTitle = titleKey[14:] - - if tHandle not in self.theIndex.novelIndex: - continue - if sTitle not in self.theIndex.novelIndex[tHandle]: - continue - - tLevel = self.theIndex.novelIndex[tHandle][sTitle]["level"] - tItem = self._createTreeItem(tHandle, sTitle, tLevel, titleKey) - self._treeMap[titleKey] = tItem + tItem = self._createTreeItem(tHandle, sTitle, tKey, novIdx) + self._treeMap[tKey] = tItem + tLevel = novIdx["level"] if tLevel == "H1": currTitle = tItem self.addTopLevelItem(tItem) @@ -292,13 +279,11 @@ class GuiNovelTree(QTreeWidget): return - def _createTreeItem(self, tHandle, sTitle, tLevel, titleKey): + def _createTreeItem(self, tHandle, sTitle, titleKey, novIdx): """Populate a tree item with all the column values. """ - novIdx = self.theIndex.novelIndex[tHandle][sTitle] - newItem = QTreeWidgetItem() - hIcon = "doc_%s" % tLevel.lower() + hIcon = "doc_%s" % novIdx["level"].lower() theData = (tHandle, sTitle[1:].lstrip("0"), titleKey) wC = int(novIdx["wCount"]) diff --git a/nw/gui/outline.py b/nw/gui/outline.py index c2ab5fa5..fce5c6c9 100644 --- a/nw/gui/outline.py +++ b/nw/gui/outline.py @@ -375,23 +375,12 @@ class GuiOutline(QTreeWidget): currChapter = None currScene = None - for titleKey in self.theIndex.getNovelStructure(skipExcluded=True): + for tKey, tHandle, sTitle, novIdx in self.theIndex.novelStructure(skipExcluded=True): - if len(titleKey) < 16: - continue - - tHandle = titleKey[:13] - sTitle = titleKey[14:] - - if tHandle not in self.theIndex.novelIndex: - continue - if sTitle not in self.theIndex.novelIndex[tHandle]: - continue - - tLevel = self.theIndex.novelIndex[tHandle][sTitle]["level"] - tItem = self._createTreeItem(tHandle, sTitle, tLevel) - self.treeMap[titleKey] = tItem + tItem = self._createTreeItem(tHandle, sTitle, novIdx) + self.treeMap[tKey] = tItem + tLevel = novIdx["level"] if tLevel == "H1": currTitle = tItem self.addTopLevelItem(tItem) @@ -428,14 +417,12 @@ class GuiOutline(QTreeWidget): return - def _createTreeItem(self, tHandle, sTitle, tLevel): + def _createTreeItem(self, tHandle, sTitle, novIdx): """Populate a tree item with all the column values. """ - nwItem = self.theProject.projTree[tHandle] - novIdx = self.theIndex.novelIndex[tHandle][sTitle] - + nwItem = self.theProject.projTree[tHandle] newItem = QTreeWidgetItem() - hIcon = "doc_%s" % tLevel.lower() + hIcon = "doc_%s" % novIdx["level"].lower() cC = int(novIdx["cCount"]) wC = int(novIdx["wCount"]) diff --git a/tests/test_core_index.py b/tests/test_core_index.py index 8fd22d73..d08cbd49 100644 --- a/tests/test_core_index.py +++ b/tests/test_core_index.py @@ -441,13 +441,32 @@ def testCoreIndex_ExtractData(nwMinimal, dummyGUI): )) # The novel structure should contain the pointer to the novel file header - assert theIndex.getNovelStructure() == ["%s:T000001" % nHandle] + theKeys = [] + for aKey, _, _, _ in theIndex.novelStructure(): + theKeys.append(aKey) + + assert theKeys == ["%s:T000001" % nHandle] # Check that excluded files can be skipped theProject.projTree[nHandle].setExported(False) - assert theIndex.getNovelStructure(skipExcluded=False) == ["%s:T000001" % nHandle] - assert theIndex.getNovelStructure(skipExcluded=True) == [] - assert theIndex.getNovelStructure() == [] + + theKeys = [] + for aKey, _, _, _ in theIndex.novelStructure(skipExcluded=False): + theKeys.append(aKey) + + assert theKeys == ["%s:T000001" % nHandle] + + theKeys = [] + for aKey, _, _, _ in theIndex.novelStructure(skipExcluded=True): + theKeys.append(aKey) + + assert theKeys == [] + + theKeys = [] + for aKey, _, _, _ in theIndex.novelStructure(): + theKeys.append(aKey) + + assert theKeys == [] # The novel file should have the correct counts cC, wC, pC = theIndex.getCounts(nHandle)