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/**/wordlist.txt
sample/**/*.bak
sample/**/*.json
# PyTest
tests/temp
+3 -2
View File
@@ -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
+1
View File
@@ -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
+5 -4
View File
@@ -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
+4 -1
View File
@@ -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):
+47 -16
View File
@@ -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
-5
View File
@@ -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