Merge pull request #483 from vkbo/json_format

JSON File Changes
This commit is contained in:
Veronica K. Berglyd Olsen
2020-10-21 20:32:15 +02:00
committed by GitHub
8 changed files with 19 additions and 27 deletions
+2 -3
View File
@@ -690,8 +690,7 @@ class Config:
if os.path.isfile(cacheFile): if os.path.isfile(cacheFile):
try: try:
with open(cacheFile, mode="r", encoding="utf8") as inFile: with open(cacheFile, mode="r", encoding="utf8") as inFile:
theJson = inFile.read() theData = json.load(inFile)
theData = json.loads(theJson)
for projPath in theData.keys(): for projPath in theData.keys():
theEntry = theData[projPath] theEntry = theData[projPath]
@@ -729,7 +728,7 @@ class Config:
try: try:
with open(cacheTemp, mode="w+", encoding="utf8") as outFile: with open(cacheTemp, mode="w+", encoding="utf8") as outFile:
outFile.write(json.dumps(self.recentProj, indent=2)) json.dump(self.recentProj, outFile, indent=2)
except Exception as e: except Exception as e:
self.hasError = True self.hasError = True
self.errData.append("Could not save recent project cache") self.errData.append("Could not save recent project cache")
+5 -10
View File
@@ -159,8 +159,7 @@ class NWIndex():
logger.debug("Loading index file") logger.debug("Loading index file")
try: try:
with open(indexFile, mode="r", encoding="utf8") as inFile: with open(indexFile, mode="r", encoding="utf8") as inFile:
theJson = inFile.read() theData = json.load(inFile)
theData = json.loads(theJson)
except Exception as e: except Exception as e:
logger.error("Failed to load index file") logger.error("Failed to load index file")
logger.error(str(e)) logger.error(str(e))
@@ -190,23 +189,18 @@ class NWIndex():
"""Save the current index as a json file in the project meta """Save the current index as a json file in the project meta
data folder. data folder.
""" """
indexFile = os.path.join(self.theProject.projMeta, nwFiles.INDEX_FILE)
logger.debug("Saving index file") logger.debug("Saving index file")
if self.mainConf.debugInfo: indexFile = os.path.join(self.theProject.projMeta, nwFiles.INDEX_FILE)
nIndent = 2
else:
nIndent = None
try: try:
with open(indexFile, mode="w+", encoding="utf8") as outFile: with open(indexFile, mode="w+", encoding="utf8") as outFile:
outFile.write(json.dumps({ json.dump({
"tagIndex" : self.tagIndex, "tagIndex" : self.tagIndex,
"refIndex" : self.refIndex, "refIndex" : self.refIndex,
"novelIndex" : self.novelIndex, "novelIndex" : self.novelIndex,
"noteIndex" : self.noteIndex, "noteIndex" : self.noteIndex,
"textCounts" : self.textCounts, "textCounts" : self.textCounts,
}, indent=nIndent)) }, outFile, indent=2)
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))
@@ -218,6 +212,7 @@ class NWIndex():
"""Check that the entries in the index are valid and contain the """Check that the entries in the index are valid and contain the
elements it should. elements it should.
""" """
logger.debug("Checking index")
self.indexBroken = False self.indexBroken = False
try: try:
+2 -3
View File
@@ -109,8 +109,7 @@ class OptionState():
logger.debug("Loading GUI options file") logger.debug("Loading GUI options file")
try: try:
with open(stateFile, mode="r", encoding="utf8") as inFile: with open(stateFile, mode="r", encoding="utf8") as inFile:
theJson = inFile.read() theState = json.load(inFile)
theState = json.loads(theJson)
except Exception as e: except Exception as e:
logger.error("Failed to load GUI options file") logger.error("Failed to load GUI options file")
logger.error(str(e)) logger.error(str(e))
@@ -137,7 +136,7 @@ class OptionState():
try: try:
with open(stateFile, mode="w+", encoding="utf8") as outFile: with open(stateFile, mode="w+", encoding="utf8") as outFile:
outFile.write(json.dumps(self.theState, indent=2)) json.dump(self.theState, outFile, indent=2)
except Exception as e: except Exception as e:
logger.error("Failed to save GUI options file") logger.error("Failed to save GUI options file")
logger.error(str(e)) logger.error(str(e))
+1 -1
View File
@@ -1024,7 +1024,7 @@ class NWProject():
return True return True
def setTreeOrder(self, newOrder): def setTreeOrder(self, newOrder):
"""A list representing the liner/flattened order of project """A list representing the linear/flattened order of project
items in the GUI project tree. The user can rearrange the order items in the GUI project tree. The user can rearrange the order
by drag-and-drop. Forwarded to the NWTree class. by drag-and-drop. Forwarded to the NWTree class.
""" """
+1 -1
View File
@@ -177,7 +177,7 @@ class NWTree():
# Dump the JSON # Dump the JSON
with open(tocJson, mode="w+", encoding="utf8") as outFile: with open(tocJson, mode="w+", encoding="utf8") as outFile:
outFile.write(json.dumps(jsonData, indent=2)) json.dump(jsonData, outFile, indent=2)
except Exception as e: except Exception as e:
logger.error(str(e)) logger.error(str(e))
+1 -1
View File
@@ -362,7 +362,7 @@ class GuiWritingStats(QDialog):
"novelWords": wA, "novelWords": wA,
"noteWords": wB, "noteWords": wB,
}) })
outFile.write(json.dumps(jsonData, indent=2)) json.dump(jsonData, outFile, indent=2)
wSuccess = True wSuccess = True
elif dataFmt == self.FMT_CSV: elif dataFmt == self.FMT_CSV:
+5 -5
View File
@@ -266,7 +266,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp):
jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") jsonStats = os.path.join(nwFuncTemp, "sessionStats.json")
with open(jsonStats, mode="r", encoding="utf-8") as inFile: with open(jsonStats, mode="r", encoding="utf-8") as inFile:
jsonData = json.loads(inFile.read()) jsonData = json.load(inFile)
qtbot.wait(stepDelay) qtbot.wait(stepDelay)
@@ -301,7 +301,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp):
jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") jsonStats = os.path.join(nwFuncTemp, "sessionStats.json")
with open(jsonStats, mode="r", encoding="utf-8") as inFile: with open(jsonStats, mode="r", encoding="utf-8") as inFile:
jsonData = json.loads(inFile.read()) jsonData = json.load(inFile)
assert len(jsonData) == 2 assert len(jsonData) == 2
assert jsonData[1]["length"] >= 14.0 assert jsonData[1]["length"] >= 14.0
@@ -318,7 +318,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp):
jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") jsonStats = os.path.join(nwFuncTemp, "sessionStats.json")
with open(jsonStats, mode="r", encoding="utf-8") as inFile: with open(jsonStats, mode="r", encoding="utf-8") as inFile:
jsonData = json.loads(inFile.read()) jsonData = json.load(inFile)
assert len(jsonData) == 3 assert len(jsonData) == 3
@@ -331,7 +331,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp):
jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") jsonStats = os.path.join(nwFuncTemp, "sessionStats.json")
with open(jsonStats, mode="r", encoding="utf-8") as inFile: with open(jsonStats, mode="r", encoding="utf-8") as inFile:
jsonData = json.loads(inFile.read()) jsonData = json.load(inFile)
assert len(jsonData) == 4 assert len(jsonData) == 4
@@ -343,7 +343,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp):
jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") jsonStats = os.path.join(nwFuncTemp, "sessionStats.json")
with open(jsonStats, mode="r", encoding="utf-8") as inFile: with open(jsonStats, mode="r", encoding="utf-8") as inFile:
jsonData = json.loads(inFile.read()) jsonData = json.load(inFile)
# Check against both 1 and 2 as this can be 2 if test was started just before midnight. # Check against both 1 and 2 as this can be 2 if test was started just before midnight.
# A failed test should in any case produce a 4 # A failed test should in any case produce a 4
+2 -3
View File
@@ -27,7 +27,6 @@ def testIndexBuildCheck(monkeypatch, nwLipsum, nwDummy, nwTempProj, nwRef):
theProject.projTree.setSeed(42) theProject.projTree.setSeed(42)
assert theProject.openProject(nwLipsum) assert theProject.openProject(nwLipsum)
theProject.mainConf.debugInfo = True
monkeypatch.setattr("nw.core.index.time", lambda: 123.4) monkeypatch.setattr("nw.core.index.time", lambda: 123.4)
theIndex = NWIndex(theProject, nwDummy) theIndex = NWIndex(theProject, nwDummy)
@@ -49,7 +48,7 @@ def testIndexBuildCheck(monkeypatch, nwLipsum, nwDummy, nwTempProj, nwRef):
raise Exception raise Exception
# Make the save fail # Make the save fail
monkeypatch.setattr(json, "dumps", doPanic) monkeypatch.setattr(json, "dump", doPanic)
assert not theIndex.saveIndex() assert not theIndex.saveIndex()
# Make the save pass # Make the save pass
@@ -83,7 +82,7 @@ def testIndexBuildCheck(monkeypatch, nwLipsum, nwDummy, nwTempProj, nwRef):
assert not theIndex.textCounts assert not theIndex.textCounts
# Make the load fail # Make the load fail
monkeypatch.setattr(json, "loads", doPanic) monkeypatch.setattr(json, "load", doPanic)
assert not theIndex.loadIndex() assert not theIndex.loadIndex()
# Make the load pass # Make the load pass