Reduced the index to a single dictionary

This commit is contained in:
Veronica K. B. Olsen
2019-05-28 22:18:37 +02:00
parent 8d871c0252
commit c273198e75
3 changed files with 57 additions and 35 deletions
+1 -1
View File
@@ -18,7 +18,7 @@ class nwFiles():
PROJ_FILE = "nwProject.nwx" PROJ_FILE = "nwProject.nwx"
PROJ_DICT = "wordlist.txt" PROJ_DICT = "wordlist.txt"
SESS_INFO = "sessionInfo.log" SESS_INFO = "sessionInfo.log"
INDEX_FILE = "tagsindex.json" INDEX_FILE = "tagsIndex.json"
# END Class nwFiles # END Class nwFiles
+10 -5
View File
@@ -210,6 +210,7 @@ class GuiMain(QMainWindow):
if saveOK: if saveOK:
self.theProject.closeProject() self.theProject.closeProject()
self.tagIndex.clearIndex()
self.clearGUI() self.clearGUI()
self.hasProject = False self.hasProject = False
@@ -232,6 +233,9 @@ class GuiMain(QMainWindow):
if not self.theProject.openProject(projFile): if not self.theProject.openProject(projFile):
return False return False
# Load the tag index
self.tagIndex.loadIndex()
# Update GUI # Update GUI
self._setWindowTitle(self.theProject.projName) self._setWindowTitle(self.theProject.projName)
self.rebuildTree() self.rebuildTree()
@@ -286,19 +290,20 @@ class GuiMain(QMainWindow):
self.docEditor.changeWidth() self.docEditor.changeWidth()
self.docEditor.setFocus() self.docEditor.setFocus()
self.theProject.setLastEdited(tHandle) self.theProject.setLastEdited(tHandle)
self.tagIndex.scanFile(tHandle)
return True return True
def saveDocument(self): def saveDocument(self):
if self.theDocument.theItem is not None: if self.theDocument.theItem is not None:
docText = self.docEditor.getText() docText = self.docEditor.getText()
cursPos = self.docEditor.getCursorPosition() cursPos = self.docEditor.getCursorPosition()
self.theDocument.theItem.setCharCount(self.docEditor.charCount) theItem = self.theDocument.theItem
self.theDocument.theItem.setWordCount(self.docEditor.wordCount) theItem.setCharCount(self.docEditor.charCount)
self.theDocument.theItem.setParaCount(self.docEditor.paraCount) theItem.setWordCount(self.docEditor.wordCount)
self.theDocument.theItem.setCursorPos(cursPos) theItem.setParaCount(self.docEditor.paraCount)
theItem.setCursorPos(cursPos)
self.theDocument.saveDocument(docText) self.theDocument.saveDocument(docText)
self.docEditor.setDocumentChanged(False) self.docEditor.setDocumentChanged(False)
self.tagIndex.scanText(theItem.itemHandle, docText)
return True return True
def viewDocument(self, tHandle=None): def viewDocument(self, tHandle=None):
+46 -29
View File
@@ -25,7 +25,7 @@ logger = logging.getLogger(__name__)
class NWIndex(): class NWIndex():
VALID_KEYS = [ VALID_KEYS = [
"todo","tag","pov","char","plot","time","location", "todo","tag","pov","chars","plot","time","location",
"object","custom","scene","chapter","part" "object","custom","scene","chapter","part"
] ]
@@ -38,27 +38,42 @@ class NWIndex():
# Indices # Indices
self.itemIndex = {} self.itemIndex = {}
self.tagIndex = {}
self.keyIndex = {}
for aKey in self.VALID_KEYS:
self.keyIndex[aKey] = []
return return
def clearIndex(self):
self.itemIndex = {}
return
def loadIndex(self):
indexFile = path.join(self.theProject.projMeta,nwFiles.INDEX_FILE)
if path.isfile(indexFile):
logger.debug("Loading index file")
try:
with open(indexFile,mode="r") as inFile:
theJson = inFile.read()
self.itemIndex = json.loads(theJson)
except Exception as e:
logger.error("Failed to load index file")
logger.error(str(e))
return False
return True
return False
def saveIndex(self): def saveIndex(self):
indexFile = path.join(self.theProject.projMeta,nwFiles.INDEX_FILE) indexFile = path.join(self.theProject.projMeta,nwFiles.INDEX_FILE)
logger.debug("Saving index file")
if self.mainConf.debugInfo: if self.mainConf.debugInfo:
nIndent = 2 nIndent = 2
else: else:
nIndent = None nIndent = None
try: try:
with open(indexFile,mode="w+") as outFile: with open(indexFile,mode="w+") as outFile:
outFile.write(json.dumps({ outFile.write(json.dumps(self.itemIndex, indent=nIndent))
"itemIndex" : self.itemIndex,
"tagIndex" : self.tagIndex,
"keyIndex" : self.keyIndex
}, indent=nIndent))
except Exception as e: except Exception as e:
logger.error("Failed to save index file") logger.error("Failed to save index file")
logger.error(str(e)) logger.error(str(e))
@@ -68,16 +83,29 @@ class NWIndex():
def scanFile(self, tHandle): def scanFile(self, tHandle):
theDocument = NWDoc(self.theProject, self.theParent)
theText = theDocument.openDocument(tHandle, False)
theItem = self.theProject.getItem(tHandle) theItem = self.theProject.getItem(tHandle)
if theItem is None: if theItem is None:
return False return False
if theItem.itemType != nwItemType.FILE: if theItem.itemType != nwItemType.FILE:
return False return False
theDocument = NWDoc(self.theProject, self.theParent)
theText = theDocument.openDocument(tHandle, False)
self.scanText(tHandle, theText)
return
def scanText(self, tHandle, theText):
theItem = self.theProject.getItem(tHandle)
if theItem is None:
return False
if theItem.itemType != nwItemType.FILE:
return False
logger.debug("Indexing item with handle %s" % tHandle)
self.itemIndex[tHandle] = {} self.itemIndex[tHandle] = {}
nLine = 0 nLine = 0
@@ -88,16 +116,6 @@ class NWIndex():
if nChar > 0 and aLine[0] == "@": if nChar > 0 and aLine[0] == "@":
self.indexThis(tHandle, aLine, nLine, theItem) self.indexThis(tHandle, aLine, nLine, theItem)
# Generate reverse index
for aKey in self.VALID_KEYS:
if aKey in self.itemIndex[tHandle]:
if len(self.itemIndex[tHandle][aKey]) > 0:
if tHandle not in self.keyIndex[aKey]:
self.keyIndex[aKey].append(tHandle)
else:
if tHandle in self.keyIndex[aKey]:
self.keyIndex[aKey].remove(tHandle)
return True return True
def indexThis(self, tHandle, aLine, nLine, theItem): def indexThis(self, tHandle, aLine, nLine, theItem):
@@ -108,22 +126,21 @@ class NWIndex():
return False return False
aKey = aLine[1:nPos].strip().lower() aKey = aLine[1:nPos].strip().lower()
tVal = aLine[nPos+1:].strip() tVal = aLine[nPos+1:].strip().lower()
if aKey not in self.VALID_KEYS: if aKey not in self.VALID_KEYS:
logger.verbose("Not a valid key '%s'" % aKey)
return False return False
logger.verbose("Found valid key '%s'" % aKey)
if aKey == "todo": if aKey == "todo":
self._addItem(tHandle, aKey, nLine, tVal) self._addItem(tHandle, aKey, nLine, tVal)
elif aKey == "tag": elif aKey == "tag":
if tVal.find(",") >- 0: if tVal.find(",") >- 0:
return False return False
self._addItem(tHandle, aKey, nLine, tVal) self._addItem(tHandle, aKey, nLine, tVal)
self.tagIndex[tVal] = [nLine, tHandle, theItem.itemClass.name]
else: else:
kVal = tVal.split(",") kVal = tVal.split(",")
cVal = [] cVal = [aVal.strip() for aVal in kVal]
for aVal in kVal:
cVal.append(aVal.strip().lower())
if len(cVal) > 0: if len(cVal) > 0:
self._addItem(tHandle, aKey, nLine, cVal) self._addItem(tHandle, aKey, nLine, cVal)
else: else: