Skip intermediate steps when loading/saving json files, and always use 2 space indent

This commit is contained in:
Veronica K. B. Olsen
2020-10-21 20:05:54 +02:00
parent f69c37fe91
commit 37c5ecc755
6 changed files with 12 additions and 19 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: