diff --git a/.gitignore b/.gitignore index f5ebca0d..47043c35 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ __pycache__ sample/**/cache sample/**/wordlist.txt sample/**/*.bak +sample/**/*.json # PyTest tests/temp diff --git a/nw/__init__.py b/nw/__init__.py index 82fb621c..0015aabd 100644 --- a/nw/__init__.py +++ b/nw/__init__.py @@ -151,8 +151,9 @@ def main(sysArgs): debugGUI = True # Set Config Options - CONFIG.showGUI = not testMode - CONFIG.debugGUI = debugGUI + CONFIG.showGUI = not testMode + CONFIG.debugGUI = debugGUI + CONFIG.debugInfo = debugLevel < logging.INFO # Set Logging if showTime: debugStr = timeStr+debugStr diff --git a/nw/config.py b/nw/config.py index 1627270f..dec3f113 100644 --- a/nw/config.py +++ b/nw/config.py @@ -37,6 +37,7 @@ class Config: self.appHandle = nw.__package__.lower() self.showGUI = True self.debugGUI = False + self.debugInfo = False # Set Paths self.confPath = None diff --git a/nw/constants.py b/nw/constants.py index 478a7f5f..d9629718 100644 --- a/nw/constants.py +++ b/nw/constants.py @@ -14,10 +14,11 @@ from nw.enum import nwItemClass, nwItemLayout class nwFiles(): - APP_ICON = "novelWriter.svg" - PROJ_FILE = "nwProject.nwx" - PROJ_DICT = "wordlist.txt" - SESS_INFO = "sessionInfo.log" + APP_ICON = "novelWriter.svg" + PROJ_FILE = "nwProject.nwx" + PROJ_DICT = "wordlist.txt" + SESS_INFO = "sessionInfo.log" + INDEX_FILE = "tagsindex.json" # END Class nwFiles diff --git a/nw/gui/winmain.py b/nw/gui/winmain.py index 0bb8e315..addc898d 100644 --- a/nw/gui/winmain.py +++ b/nw/gui/winmain.py @@ -33,6 +33,7 @@ from nw.gui.statusbar import GuiMainStatus from nw.project.project import NWProject from nw.project.document import NWDoc from nw.project.item import NWItem +from nw.project.index import NWIndex from nw.convert.tokenizer import Tokenizer from nw.convert.tohtml import ToHtml from nw.enum import nwItemType, nwAlert @@ -50,6 +51,7 @@ class GuiMain(QMainWindow): self.theTheme = Theme() self.theProject = NWProject(self) self.theDocument = NWDoc(self.theProject, self) + self.tagIndex = NWIndex(self.theProject, self) self.hasProject = False self.resize(*self.mainConf.winGeometry) @@ -260,6 +262,7 @@ class GuiMain(QMainWindow): self.treeView.saveTreeOrder() self.theProject.saveProject() + self.tagIndex.saveIndex() self.mainMenu.updateRecentProjects() return True @@ -283,7 +286,7 @@ class GuiMain(QMainWindow): self.docEditor.changeWidth() self.docEditor.setFocus() self.theProject.setLastEdited(tHandle) - self.theProject.theIndex.scanFile(tHandle) + self.tagIndex.scanFile(tHandle) return True def saveDocument(self): diff --git a/nw/project/index.py b/nw/project/index.py index 668c5ade..6cd6d7fc 100644 --- a/nw/project/index.py +++ b/nw/project/index.py @@ -11,10 +11,14 @@ """ import logging +import json import nw +from os import path + from nw.project.document import NWDoc from nw.enum import nwItemType +from nw.constants import nwFiles logger = logging.getLogger(__name__) @@ -41,6 +45,27 @@ class NWIndex(): return + def saveIndex(self): + + indexFile = path.join(self.theProject.projMeta,nwFiles.INDEX_FILE) + if self.mainConf.debugInfo: + nIndent = 2 + else: + nIndent = None + try: + with open(indexFile,mode="w+") as outFile: + outFile.write(json.dumps({ + "itemIndex" : self.itemIndex, + "tagIndex" : self.tagIndex, + "keyIndex" : self.keyIndex + }, indent=nIndent)) + except Exception as e: + logger.error("Failed to save index file") + logger.error(str(e)) + return False + + return True + def scanFile(self, tHandle): theDocument = NWDoc(self.theProject, self.theParent) @@ -54,8 +79,6 @@ class NWIndex(): return False self.itemIndex[tHandle] = {} - for aKey in self.VALID_KEYS: - self.itemIndex[tHandle][aKey] = [] nLine = 0 for aLine in theText.splitlines(): @@ -65,17 +88,15 @@ class NWIndex(): if nChar > 0 and aLine[0] == "@": self.indexThis(tHandle, aLine, nLine, theItem) + # Generate reverse index for aKey in self.VALID_KEYS: - 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) - - print(self.itemIndex) - print(self.tagIndex) - print(self.keyIndex) + 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 @@ -92,22 +113,32 @@ class NWIndex(): return False if aKey == "todo": - self.itemIndex[tHandle]["todo"].append((nLine, tVal)) + self._addItem(tHandle, aKey, nLine, tVal) elif aKey == "tag": if tVal.find(",") >- 0: return False - self.itemIndex[tHandle]["tag"].append((nLine, tVal)) - self.tagIndex[tVal] = (nLine, tHandle, theItem.itemClass) + self._addItem(tHandle, aKey, nLine, tVal) + self.tagIndex[tVal] = [nLine, tHandle, theItem.itemClass.name] else: kVal = tVal.split(",") cVal = [] for aVal in kVal: cVal.append(aVal.strip().lower()) if len(cVal) > 0: - self.itemIndex[tHandle][aKey].append((nLine, cVal)) + self._addItem(tHandle, aKey, nLine, cVal) else: return False return True + ## + # Internal Functions + ## + + def _addItem(self, tHandle, tKey, tLine, tVal): + if tKey not in self.itemIndex[tHandle].keys(): + self.itemIndex[tHandle][tKey] = [] + self.itemIndex[tHandle][tKey].append([tLine, tVal]) + return + # END Class NWIndex diff --git a/nw/project/project.py b/nw/project/project.py index a70a1057..dce45ef3 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -21,7 +21,6 @@ from time import time from nw.project.item import NWItem from nw.project.status import NWStatus -from nw.project.index import NWIndex from nw.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert from nw.common import checkString, checkBool, checkInt from nw.constants import nwFiles @@ -65,9 +64,6 @@ class NWProject(): self.lastWCount = 0 self.currWCount = 0 - # Index - self.theIndex = None - # Set Defaults self.clearProject() @@ -169,7 +165,6 @@ class NWProject(): self.lastViewed = None self.lastWCount = 0 self.currWCount = 0 - self.theIndex = NWIndex(self, self.theParent) return