Fix index tests

This commit is contained in:
Veronica Berglyd Olsen
2023-09-16 10:10:55 +02:00
parent bf592fa706
commit 29518a33f2
3 changed files with 430 additions and 384 deletions
+29 -30
View File
@@ -240,50 +240,50 @@ class NWIndex:
# Index Building
##
def scanText(self, tHandle: str, theText: str) -> bool:
def scanText(self, tHandle: str, text: str) -> bool:
"""Scan a piece of text associated with a handle. This will
update the indices accordingly. This function takes the handle
and text as separate inputs as we want to primarily scan the
files before we save them, in which case we already have the
text.
"""
theItem = self._project.tree[tHandle]
if theItem is None:
tItem = self._project.tree[tHandle]
if tItem is None:
logger.info("Not indexing unknown item '%s'", tHandle)
return False
if not theItem.isFileType():
if not tItem.isFileType():
logger.info("Not indexing non-file item '%s'", tHandle)
return False
# Keep a record of existing tags, and create a new item entry
itemTags = dict.fromkeys(self._itemIndex.allItemTags(tHandle), False)
self._itemIndex.add(tHandle, theItem)
self._itemIndex.add(tHandle, tItem)
# Run word counter for the whole text
cC, wC, pC = countWords(theText)
theItem.setCharCount(cC)
theItem.setWordCount(wC)
theItem.setParaCount(pC)
cC, wC, pC = countWords(text)
tItem.setCharCount(cC)
tItem.setWordCount(wC)
tItem.setParaCount(pC)
# If the file's meta data is missing, or the file is out of the
# main project, we don't index the content
if theItem.itemLayout == nwItemLayout.NO_LAYOUT:
if tItem.itemLayout == nwItemLayout.NO_LAYOUT:
logger.info("Not indexing no-layout item '%s'", tHandle)
return False
if theItem.itemParent is None:
if tItem.itemParent is None:
logger.info("Not indexing orphaned item '%s'", tHandle)
return False
logger.debug("Indexing item with handle '%s'", tHandle)
if theItem.isInactiveClass():
self._scanInactive(theItem, theText)
if tItem.isInactiveClass():
self._scanInactive(tItem, text)
else:
self._scanActive(tHandle, theItem, theText, itemTags)
self._scanActive(tHandle, tItem, text, itemTags)
# Update timestamps for index changes
nowTime = time()
self._indexChange = nowTime
self._rootChange[theItem.itemRoot] = nowTime
self._rootChange[tItem.itemRoot] = nowTime
return True
@@ -479,9 +479,8 @@ class NWIndex:
# If we're still here, we check that the references exist
refKey = nwKeyWords.KEY_CLASS[tBits[0]].name
for n in range(1, nBits):
tagKey = tBits[n].lower()
if tagKey in self._tagsIndex:
isGood[n] = self._tagsIndex.tagClass(tagKey) == refKey
if tBits[n] in self._tagsIndex:
isGood[n] = self._tagsIndex.tagClass(tBits[n]) == refKey
return isGood
@@ -641,30 +640,30 @@ class TagsIndex:
__slots__ = ("_tags")
def __init__(self):
def __init__(self) -> None:
self._tags: dict[str, dict] = {}
return
def __contains__(self, tagKey):
return tagKey in self._tags
def __contains__(self, tagKey: str) -> bool:
return tagKey.lower() in self._tags
def __delitem__(self, tagKey):
self._tags.pop(tagKey, None)
def __delitem__(self, tagKey: str) -> None:
self._tags.pop(tagKey.lower(), None)
return
def __getitem__(self, tagKey):
return self._tags.get(tagKey, None)
def __getitem__(self, tagKey: str) -> dict | None:
return self._tags.get(tagKey.lower(), None)
##
# Methods
##
def clear(self):
def clear(self) -> None:
"""Clear the index."""
self._tags = {}
return
def add(self, tagKey: str, tHandle: str, sTitle: str, itemClass: nwItemClass):
def add(self, tagKey: str, tHandle: str, sTitle: str, itemClass: nwItemClass) -> None:
"""Add a key to the index and set all values."""
self._tags[tagKey.lower()] = {
"name": tagKey, "handle": tHandle, "heading": sTitle, "class": itemClass.name
@@ -673,7 +672,7 @@ class TagsIndex:
def tagName(self, tagKey: str) -> str:
"""Get the display name of a given tag."""
return self._tags.get(tagKey.lower(), {}).get("name", None)
return self._tags.get(tagKey.lower(), {}).get("name", "")
def tagHandle(self, tagKey: str) -> str:
"""Get the handle of a given tag."""
@@ -695,7 +694,7 @@ class TagsIndex:
"""Pack all the data of the tags into a single dictionary."""
return self._tags
def unpackData(self, data: dict):
def unpackData(self, data: dict) -> None:
"""Iterate through the tagsIndex loaded from cache and check
that it's valid.
"""
@@ -714,7 +713,7 @@ class TagsIndex:
raise KeyError("A tagIndex item is missing a heading entry")
if "class" not in tagData:
raise KeyError("A tagIndex item is missing a class entry")
if tagData["name"].lower() != tagKey.lower():
if tagData["name"].lower() != tagKey:
raise ValueError("tagsIndex name must match key")
if not isHandle(tagData["handle"]):
raise ValueError("tagsIndex handle must be a handle")
@@ -1,8 +1,8 @@
{
"novelWriter.tagsIndex": {
"Bod": {"handle": "4c4f28287af27", "heading": "T0001", "class": "CHARACTER"},
"Main": {"handle": "2426c6f0ca922", "heading": "T0001", "class": "PLOT"},
"Europe": {"handle": "04468803b92e1", "heading": "T0001", "class": "WORLD"}
"bod": {"name": "Bod", "handle": "4c4f28287af27", "heading": "T0001", "class": "CHARACTER"},
"main": {"name": "Main", "handle": "2426c6f0ca922", "heading": "T0001", "class": "PLOT"},
"europe": {"name": "Europe", "handle": "04468803b92e1", "heading": "T0001", "class": "WORLD"}
},
"novelWriter.itemIndex": {
"7a992350f3eb6": {
@@ -30,7 +30,7 @@
"T0001": {"level": "H2", "title": "Chapter One", "line": 1, "tag": "", "cCount": 419, "wCount": 67, "pCount": 1, "synopsis": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque at aliquam quam."}
},
"references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"}
"T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
}
},
"88243afbe5ed8": {
@@ -39,7 +39,7 @@
"T0002": {"level": "H4", "title": "Scene One, Section Two", "line": 13, "tag": "", "cCount": 1561, "wCount": 230, "pCount": 2, "synopsis": ""}
},
"references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"}
"T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
}
},
"f96ec11c6a3da": {
@@ -48,7 +48,7 @@
"T0002": {"level": "H4", "title": "Scene Two, Section Two", "line": 15, "tag": "", "cCount": 2009, "wCount": 301, "pCount": 3, "synopsis": ""}
},
"references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"}
"T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
}
},
"846352075de7d": {
@@ -61,7 +61,7 @@
"T0001": {"level": "H2", "title": "Chapter Two", "line": 1, "tag": "", "cCount": 477, "wCount": 70, "pCount": 1, "synopsis": "Curabitur a elit posuere, varius ex et, convallis neque. Phasellus sagittis pharetra sem vitae dapibus. Curabitur varius lorem non pulvinar congue."}
},
"references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"}
"T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
}
},
"eb103bc70c90c": {
@@ -69,7 +69,7 @@
"T0001": {"level": "H3", "title": "Scene Three", "line": 1, "tag": "", "cCount": 3006, "wCount": 439, "pCount": 4, "synopsis": "Aenean ut libero ut lectus porttitor rhoncus vel et massa. Nam pretium, nibh et varius vehicula, urna metus blandit eros, euismod pharetra diam diam et libero. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos."}
},
"references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"}
"T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
}
},
"f8c0562e50f1b": {
@@ -77,7 +77,7 @@
"T0001": {"level": "H3", "title": "Scene Four", "line": 1, "tag": "", "cCount": 3839, "wCount": 563, "pCount": 6, "synopsis": "Nam tempor blandit magna laoreet aliquet. Vestibulum auctor posuere leo, ac gravida nisi rhoncus varius. Aenean posuere dolor vitae condimentum volutpat. Donec egestas volutpat risus, quis luctus justo."}
},
"references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"}
"T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
}
},
"47666c91c7ccf": {
@@ -85,25 +85,25 @@
"T0001": {"level": "H3", "title": "Scene Five", "line": 1, "tag": "", "cCount": 3644, "wCount": 543, "pCount": 5, "synopsis": "Praesent eget est porta, dictum ante in, egestas risus. Mauris risus mauris, consequat aliquam mauris et, feugiat iaculis ipsum. Aliquam arcu ipsum, fermentum ut arcu sed, lobortis euismod sem. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus."}
},
"references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"}
"T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
}
},
"4c4f28287af27": {
"headings": {
"T0001": {"level": "H1", "title": "Nobody Owens", "line": 1, "tag": "Bod", "cCount": 1864, "wCount": 284, "pCount": 3, "synopsis": ""}
"T0001": {"level": "H1", "title": "Nobody Owens", "line": 1, "tag": "bod", "cCount": 1864, "wCount": 284, "pCount": 3, "synopsis": ""}
},
"references": {
"T0001": {"Main": "@plot"}
"T0001": {"main": "@plot"}
}
},
"2426c6f0ca922": {
"headings": {
"T0001": {"level": "H1", "title": "Main Plot", "line": 1, "tag": "Main", "cCount": 1369, "wCount": 195, "pCount": 2, "synopsis": ""}
"T0001": {"level": "H1", "title": "Main Plot", "line": 1, "tag": "main", "cCount": 1369, "wCount": 195, "pCount": 2, "synopsis": ""}
}
},
"04468803b92e1": {
"headings": {
"T0001": {"level": "H1", "title": "Ancient Europe", "line": 1, "tag": "Europe", "cCount": 1770, "wCount": 259, "pCount": 3, "synopsis": ""}
"T0001": {"level": "H1", "title": "Ancient Europe", "line": 1, "tag": "europe", "cCount": 1770, "wCount": 259, "pCount": 3, "synopsis": ""}
}
}
}
File diff suppressed because it is too large Load Diff