The index is now built and saved as a json file

This commit is contained in:
Veronica K. B. Olsen
2019-05-28 19:44:14 +02:00
parent e828d9f041
commit 8d871c0252
7 changed files with 61 additions and 28 deletions
+1
View File
@@ -9,6 +9,7 @@ __pycache__
sample/**/cache sample/**/cache
sample/**/wordlist.txt sample/**/wordlist.txt
sample/**/*.bak sample/**/*.bak
sample/**/*.json
# PyTest # PyTest
tests/temp tests/temp
+3 -2
View File
@@ -151,8 +151,9 @@ def main(sysArgs):
debugGUI = True debugGUI = True
# Set Config Options # Set Config Options
CONFIG.showGUI = not testMode CONFIG.showGUI = not testMode
CONFIG.debugGUI = debugGUI CONFIG.debugGUI = debugGUI
CONFIG.debugInfo = debugLevel < logging.INFO
# Set Logging # Set Logging
if showTime: debugStr = timeStr+debugStr if showTime: debugStr = timeStr+debugStr
+1
View File
@@ -37,6 +37,7 @@ class Config:
self.appHandle = nw.__package__.lower() self.appHandle = nw.__package__.lower()
self.showGUI = True self.showGUI = True
self.debugGUI = False self.debugGUI = False
self.debugInfo = False
# Set Paths # Set Paths
self.confPath = None self.confPath = None
+5 -4
View File
@@ -14,10 +14,11 @@ from nw.enum import nwItemClass, nwItemLayout
class nwFiles(): class nwFiles():
APP_ICON = "novelWriter.svg" APP_ICON = "novelWriter.svg"
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"
# END Class nwFiles # END Class nwFiles
+4 -1
View File
@@ -33,6 +33,7 @@ from nw.gui.statusbar import GuiMainStatus
from nw.project.project import NWProject from nw.project.project import NWProject
from nw.project.document import NWDoc from nw.project.document import NWDoc
from nw.project.item import NWItem from nw.project.item import NWItem
from nw.project.index import NWIndex
from nw.convert.tokenizer import Tokenizer from nw.convert.tokenizer import Tokenizer
from nw.convert.tohtml import ToHtml from nw.convert.tohtml import ToHtml
from nw.enum import nwItemType, nwAlert from nw.enum import nwItemType, nwAlert
@@ -50,6 +51,7 @@ class GuiMain(QMainWindow):
self.theTheme = Theme() self.theTheme = Theme()
self.theProject = NWProject(self) self.theProject = NWProject(self)
self.theDocument = NWDoc(self.theProject, self) self.theDocument = NWDoc(self.theProject, self)
self.tagIndex = NWIndex(self.theProject, self)
self.hasProject = False self.hasProject = False
self.resize(*self.mainConf.winGeometry) self.resize(*self.mainConf.winGeometry)
@@ -260,6 +262,7 @@ class GuiMain(QMainWindow):
self.treeView.saveTreeOrder() self.treeView.saveTreeOrder()
self.theProject.saveProject() self.theProject.saveProject()
self.tagIndex.saveIndex()
self.mainMenu.updateRecentProjects() self.mainMenu.updateRecentProjects()
return True return True
@@ -283,7 +286,7 @@ class GuiMain(QMainWindow):
self.docEditor.changeWidth() self.docEditor.changeWidth()
self.docEditor.setFocus() self.docEditor.setFocus()
self.theProject.setLastEdited(tHandle) self.theProject.setLastEdited(tHandle)
self.theProject.theIndex.scanFile(tHandle) self.tagIndex.scanFile(tHandle)
return True return True
def saveDocument(self): def saveDocument(self):
+47 -16
View File
@@ -11,10 +11,14 @@
""" """
import logging import logging
import json
import nw import nw
from os import path
from nw.project.document import NWDoc from nw.project.document import NWDoc
from nw.enum import nwItemType from nw.enum import nwItemType
from nw.constants import nwFiles
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -41,6 +45,27 @@ class NWIndex():
return 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): def scanFile(self, tHandle):
theDocument = NWDoc(self.theProject, self.theParent) theDocument = NWDoc(self.theProject, self.theParent)
@@ -54,8 +79,6 @@ class NWIndex():
return False return False
self.itemIndex[tHandle] = {} self.itemIndex[tHandle] = {}
for aKey in self.VALID_KEYS:
self.itemIndex[tHandle][aKey] = []
nLine = 0 nLine = 0
for aLine in theText.splitlines(): for aLine in theText.splitlines():
@@ -65,17 +88,15 @@ 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: for aKey in self.VALID_KEYS:
if len(self.itemIndex[tHandle][aKey]) > 0: if aKey in self.itemIndex[tHandle]:
if tHandle not in self.keyIndex[aKey]: if len(self.itemIndex[tHandle][aKey]) > 0:
self.keyIndex[aKey].append(tHandle) if tHandle not in self.keyIndex[aKey]:
else: self.keyIndex[aKey].append(tHandle)
if tHandle in self.keyIndex[aKey]: else:
self.keyIndex[aKey].remove(tHandle) if tHandle in self.keyIndex[aKey]:
self.keyIndex[aKey].remove(tHandle)
print(self.itemIndex)
print(self.tagIndex)
print(self.keyIndex)
return True return True
@@ -92,22 +113,32 @@ class NWIndex():
return False return False
if aKey == "todo": if aKey == "todo":
self.itemIndex[tHandle]["todo"].append((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.itemIndex[tHandle]["tag"].append((nLine, tVal)) self._addItem(tHandle, aKey, nLine, tVal)
self.tagIndex[tVal] = (nLine, tHandle, theItem.itemClass) self.tagIndex[tVal] = [nLine, tHandle, theItem.itemClass.name]
else: else:
kVal = tVal.split(",") kVal = tVal.split(",")
cVal = [] cVal = []
for aVal in kVal: for aVal in kVal:
cVal.append(aVal.strip().lower()) cVal.append(aVal.strip().lower())
if len(cVal) > 0: if len(cVal) > 0:
self.itemIndex[tHandle][aKey].append((nLine, cVal)) self._addItem(tHandle, aKey, nLine, cVal)
else: else:
return False return False
return True 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 # END Class NWIndex
-5
View File
@@ -21,7 +21,6 @@ from time import time
from nw.project.item import NWItem from nw.project.item import NWItem
from nw.project.status import NWStatus from nw.project.status import NWStatus
from nw.project.index import NWIndex
from nw.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert from nw.enum import nwItemType, nwItemClass, nwItemLayout, nwAlert
from nw.common import checkString, checkBool, checkInt from nw.common import checkString, checkBool, checkInt
from nw.constants import nwFiles from nw.constants import nwFiles
@@ -65,9 +64,6 @@ class NWProject():
self.lastWCount = 0 self.lastWCount = 0
self.currWCount = 0 self.currWCount = 0
# Index
self.theIndex = None
# Set Defaults # Set Defaults
self.clearProject() self.clearProject()
@@ -169,7 +165,6 @@ class NWProject():
self.lastViewed = None self.lastViewed = None
self.lastWCount = 0 self.lastWCount = 0
self.currWCount = 0 self.currWCount = 0
self.theIndex = NWIndex(self, self.theParent)
return return