From 93f3e01365e833ee3490d23ffaed9b606b071dea Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Sat, 2 Nov 2019 11:54:18 +0100 Subject: [PATCH] Added check index function --- nw/project/index.py | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/nw/project/index.py b/nw/project/index.py index 93890664..08b872dc 100644 --- a/nw/project/index.py +++ b/nw/project/index.py @@ -19,6 +19,7 @@ from os import path from nw.project.document import NWDoc from nw.enum import nwItemType, nwItemClass, nwItemLayout from nw.constants import nwFiles +from nw.enum import nwAlert logger = logging.getLogger(__name__) @@ -49,9 +50,10 @@ class NWIndex(): def __init__(self, theProject, theParent): # Internal - self.theProject = theProject - self.theParent = theParent - self.mainConf = self.theParent.mainConf + self.theProject = theProject + self.theParent = theParent + self.mainConf = self.theParent.mainConf + self.indexBroken = False # Indices self.tagIndex = {} @@ -117,6 +119,8 @@ class NWIndex(): if "noteIndex" in theData.keys(): self.noteIndex = theData["noteIndex"] + self.checkIndex() + return True return False @@ -146,6 +150,40 @@ class NWIndex(): return True + def checkIndex(self): + """Check that the entries in the index are valid and contain the elements it should. + """ + + self.indexBroken = False + + for tTag in self.tagIndex: + if len(self.tagIndex[tTag]) != 3: + self.indexBroken = True + + for tHandle in self.refIndex: + for tEntry in self.refIndex[tHandle]: + if len(tEntry) != 4: + self.indexBroken = True + + for tHandle in self.novelIndex: + for tEntry in self.novelIndex[tHandle]: + if len(tEntry) != 4: + self.indexBroken = True + + for tHandle in self.noteIndex: + for tEntry in self.noteIndex[tHandle]: + if len(tEntry) != 4: + self.indexBroken = True + + if self.indexBroken: + self.clearIndex() + self.theParent.makeAlert( + "The project index loaded from cache contains errors. Triggering Rebuild Index.", + nwAlert.WARN + ) + + return + ## # Index Building ##