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 # 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 """Scan a piece of text associated with a handle. This will
update the indices accordingly. This function takes the handle update the indices accordingly. This function takes the handle
and text as separate inputs as we want to primarily scan the and text as separate inputs as we want to primarily scan the
files before we save them, in which case we already have the files before we save them, in which case we already have the
text. text.
""" """
theItem = self._project.tree[tHandle] tItem = self._project.tree[tHandle]
if theItem is None: if tItem is None:
logger.info("Not indexing unknown item '%s'", tHandle) logger.info("Not indexing unknown item '%s'", tHandle)
return False return False
if not theItem.isFileType(): if not tItem.isFileType():
logger.info("Not indexing non-file item '%s'", tHandle) logger.info("Not indexing non-file item '%s'", tHandle)
return False return False
# Keep a record of existing tags, and create a new item entry # Keep a record of existing tags, and create a new item entry
itemTags = dict.fromkeys(self._itemIndex.allItemTags(tHandle), False) 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 # Run word counter for the whole text
cC, wC, pC = countWords(theText) cC, wC, pC = countWords(text)
theItem.setCharCount(cC) tItem.setCharCount(cC)
theItem.setWordCount(wC) tItem.setWordCount(wC)
theItem.setParaCount(pC) tItem.setParaCount(pC)
# If the file's meta data is missing, or the file is out of the # If the file's meta data is missing, or the file is out of the
# main project, we don't index the content # 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) logger.info("Not indexing no-layout item '%s'", tHandle)
return False return False
if theItem.itemParent is None: if tItem.itemParent is None:
logger.info("Not indexing orphaned item '%s'", tHandle) logger.info("Not indexing orphaned item '%s'", tHandle)
return False return False
logger.debug("Indexing item with handle '%s'", tHandle) logger.debug("Indexing item with handle '%s'", tHandle)
if theItem.isInactiveClass(): if tItem.isInactiveClass():
self._scanInactive(theItem, theText) self._scanInactive(tItem, text)
else: else:
self._scanActive(tHandle, theItem, theText, itemTags) self._scanActive(tHandle, tItem, text, itemTags)
# Update timestamps for index changes # Update timestamps for index changes
nowTime = time() nowTime = time()
self._indexChange = nowTime self._indexChange = nowTime
self._rootChange[theItem.itemRoot] = nowTime self._rootChange[tItem.itemRoot] = nowTime
return True return True
@@ -479,9 +479,8 @@ class NWIndex:
# If we're still here, we check that the references exist # If we're still here, we check that the references exist
refKey = nwKeyWords.KEY_CLASS[tBits[0]].name refKey = nwKeyWords.KEY_CLASS[tBits[0]].name
for n in range(1, nBits): for n in range(1, nBits):
tagKey = tBits[n].lower() if tBits[n] in self._tagsIndex:
if tagKey in self._tagsIndex: isGood[n] = self._tagsIndex.tagClass(tBits[n]) == refKey
isGood[n] = self._tagsIndex.tagClass(tagKey) == refKey
return isGood return isGood
@@ -641,30 +640,30 @@ class TagsIndex:
__slots__ = ("_tags") __slots__ = ("_tags")
def __init__(self): def __init__(self) -> None:
self._tags: dict[str, dict] = {} self._tags: dict[str, dict] = {}
return return
def __contains__(self, tagKey): def __contains__(self, tagKey: str) -> bool:
return tagKey in self._tags return tagKey.lower() in self._tags
def __delitem__(self, tagKey): def __delitem__(self, tagKey: str) -> None:
self._tags.pop(tagKey, None) self._tags.pop(tagKey.lower(), None)
return return
def __getitem__(self, tagKey): def __getitem__(self, tagKey: str) -> dict | None:
return self._tags.get(tagKey, None) return self._tags.get(tagKey.lower(), None)
## ##
# Methods # Methods
## ##
def clear(self): def clear(self) -> None:
"""Clear the index.""" """Clear the index."""
self._tags = {} self._tags = {}
return 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.""" """Add a key to the index and set all values."""
self._tags[tagKey.lower()] = { self._tags[tagKey.lower()] = {
"name": tagKey, "handle": tHandle, "heading": sTitle, "class": itemClass.name "name": tagKey, "handle": tHandle, "heading": sTitle, "class": itemClass.name
@@ -673,7 +672,7 @@ class TagsIndex:
def tagName(self, tagKey: str) -> str: def tagName(self, tagKey: str) -> str:
"""Get the display name of a given tag.""" """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: def tagHandle(self, tagKey: str) -> str:
"""Get the handle of a given tag.""" """Get the handle of a given tag."""
@@ -695,7 +694,7 @@ class TagsIndex:
"""Pack all the data of the tags into a single dictionary.""" """Pack all the data of the tags into a single dictionary."""
return self._tags return self._tags
def unpackData(self, data: dict): def unpackData(self, data: dict) -> None:
"""Iterate through the tagsIndex loaded from cache and check """Iterate through the tagsIndex loaded from cache and check
that it's valid. that it's valid.
""" """
@@ -714,7 +713,7 @@ class TagsIndex:
raise KeyError("A tagIndex item is missing a heading entry") raise KeyError("A tagIndex item is missing a heading entry")
if "class" not in tagData: if "class" not in tagData:
raise KeyError("A tagIndex item is missing a class entry") 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") raise ValueError("tagsIndex name must match key")
if not isHandle(tagData["handle"]): if not isHandle(tagData["handle"]):
raise ValueError("tagsIndex handle must be a handle") raise ValueError("tagsIndex handle must be a handle")
@@ -1,8 +1,8 @@
{ {
"novelWriter.tagsIndex": { "novelWriter.tagsIndex": {
"Bod": {"handle": "4c4f28287af27", "heading": "T0001", "class": "CHARACTER"}, "bod": {"name": "Bod", "handle": "4c4f28287af27", "heading": "T0001", "class": "CHARACTER"},
"Main": {"handle": "2426c6f0ca922", "heading": "T0001", "class": "PLOT"}, "main": {"name": "Main", "handle": "2426c6f0ca922", "heading": "T0001", "class": "PLOT"},
"Europe": {"handle": "04468803b92e1", "heading": "T0001", "class": "WORLD"} "europe": {"name": "Europe", "handle": "04468803b92e1", "heading": "T0001", "class": "WORLD"}
}, },
"novelWriter.itemIndex": { "novelWriter.itemIndex": {
"7a992350f3eb6": { "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."} "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": { "references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"} "T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
} }
}, },
"88243afbe5ed8": { "88243afbe5ed8": {
@@ -39,7 +39,7 @@
"T0002": {"level": "H4", "title": "Scene One, Section Two", "line": 13, "tag": "", "cCount": 1561, "wCount": 230, "pCount": 2, "synopsis": ""} "T0002": {"level": "H4", "title": "Scene One, Section Two", "line": 13, "tag": "", "cCount": 1561, "wCount": 230, "pCount": 2, "synopsis": ""}
}, },
"references": { "references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"} "T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
} }
}, },
"f96ec11c6a3da": { "f96ec11c6a3da": {
@@ -48,7 +48,7 @@
"T0002": {"level": "H4", "title": "Scene Two, Section Two", "line": 15, "tag": "", "cCount": 2009, "wCount": 301, "pCount": 3, "synopsis": ""} "T0002": {"level": "H4", "title": "Scene Two, Section Two", "line": 15, "tag": "", "cCount": 2009, "wCount": 301, "pCount": 3, "synopsis": ""}
}, },
"references": { "references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"} "T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
} }
}, },
"846352075de7d": { "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."} "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": { "references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"} "T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
} }
}, },
"eb103bc70c90c": { "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."} "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": { "references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"} "T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
} }
}, },
"f8c0562e50f1b": { "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."} "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": { "references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"} "T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
} }
}, },
"47666c91c7ccf": { "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."} "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": { "references": {
"T0001": {"Bod": "@pov", "Main": "@plot", "Europe": "@location"} "T0001": {"bod": "@pov", "main": "@plot", "europe": "@location"}
} }
}, },
"4c4f28287af27": { "4c4f28287af27": {
"headings": { "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": { "references": {
"T0001": {"Main": "@plot"} "T0001": {"main": "@plot"}
} }
}, },
"2426c6f0ca922": { "2426c6f0ca922": {
"headings": { "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": { "04468803b92e1": {
"headings": { "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