The index is now built and saved as a json file
This commit is contained in:
+47
-16
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user