Make novel structure function in index class an iterable function instead

This commit is contained in:
Veronica K. B. Olsen
2021-01-02 21:37:46 +01:00
parent 21d91193a2
commit e4d160ee0b
4 changed files with 45 additions and 56 deletions
+7 -9
View File
@@ -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
+8 -23
View File
@@ -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"])
+7 -20
View File
@@ -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"])
+23 -4
View File
@@ -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)