From 37c5ecc75551bd76d56679780e4c7de8e59d5bd1 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 21 Oct 2020 20:05:54 +0200 Subject: [PATCH 1/2] Skip intermediate steps when loading/saving json files, and always use 2 space indent --- nw/config.py | 5 ++--- nw/core/index.py | 15 +++++---------- nw/core/options.py | 5 ++--- nw/core/project.py | 2 +- nw/core/tree.py | 2 +- nw/gui/writingstats.py | 2 +- 6 files changed, 12 insertions(+), 19 deletions(-) diff --git a/nw/config.py b/nw/config.py index 65fd4d8a..bc777812 100644 --- a/nw/config.py +++ b/nw/config.py @@ -690,8 +690,7 @@ class Config: if os.path.isfile(cacheFile): try: with open(cacheFile, mode="r", encoding="utf8") as inFile: - theJson = inFile.read() - theData = json.loads(theJson) + theData = json.load(inFile) for projPath in theData.keys(): theEntry = theData[projPath] @@ -729,7 +728,7 @@ class Config: try: 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: self.hasError = True self.errData.append("Could not save recent project cache") diff --git a/nw/core/index.py b/nw/core/index.py index c08562e1..97ad7ba4 100644 --- a/nw/core/index.py +++ b/nw/core/index.py @@ -159,8 +159,7 @@ class NWIndex(): logger.debug("Loading index file") try: with open(indexFile, mode="r", encoding="utf8") as inFile: - theJson = inFile.read() - theData = json.loads(theJson) + theData = json.load(inFile) except Exception as e: logger.error("Failed to load index file") logger.error(str(e)) @@ -190,23 +189,18 @@ class NWIndex(): """Save the current index as a json file in the project meta data folder. """ - indexFile = os.path.join(self.theProject.projMeta, nwFiles.INDEX_FILE) - logger.debug("Saving index file") - if self.mainConf.debugInfo: - nIndent = 2 - else: - nIndent = None + indexFile = os.path.join(self.theProject.projMeta, nwFiles.INDEX_FILE) try: with open(indexFile, mode="w+", encoding="utf8") as outFile: - outFile.write(json.dumps({ + json.dump({ "tagIndex" : self.tagIndex, "refIndex" : self.refIndex, "novelIndex" : self.novelIndex, "noteIndex" : self.noteIndex, "textCounts" : self.textCounts, - }, indent=nIndent)) + }, outFile, indent=2) except Exception as e: logger.error("Failed to save index file") logger.error(str(e)) @@ -218,6 +212,7 @@ class NWIndex(): """Check that the entries in the index are valid and contain the elements it should. """ + logger.debug("Checking index") self.indexBroken = False try: diff --git a/nw/core/options.py b/nw/core/options.py index e808f74c..35358fc0 100644 --- a/nw/core/options.py +++ b/nw/core/options.py @@ -109,8 +109,7 @@ class OptionState(): logger.debug("Loading GUI options file") try: with open(stateFile, mode="r", encoding="utf8") as inFile: - theJson = inFile.read() - theState = json.loads(theJson) + theState = json.load(inFile) except Exception as e: logger.error("Failed to load GUI options file") logger.error(str(e)) @@ -137,7 +136,7 @@ class OptionState(): try: 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: logger.error("Failed to save GUI options file") logger.error(str(e)) diff --git a/nw/core/project.py b/nw/core/project.py index 622bcc38..1bf53151 100644 --- a/nw/core/project.py +++ b/nw/core/project.py @@ -1024,7 +1024,7 @@ class NWProject(): return True 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 by drag-and-drop. Forwarded to the NWTree class. """ diff --git a/nw/core/tree.py b/nw/core/tree.py index b43230a9..f33b0d59 100644 --- a/nw/core/tree.py +++ b/nw/core/tree.py @@ -177,7 +177,7 @@ class NWTree(): # Dump the JSON 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: logger.error(str(e)) diff --git a/nw/gui/writingstats.py b/nw/gui/writingstats.py index ccf1ce18..fa03a278 100644 --- a/nw/gui/writingstats.py +++ b/nw/gui/writingstats.py @@ -362,7 +362,7 @@ class GuiWritingStats(QDialog): "novelWords": wA, "noteWords": wB, }) - outFile.write(json.dumps(jsonData, indent=2)) + json.dump(jsonData, outFile, indent=2) wSuccess = True elif dataFmt == self.FMT_CSV: From 20e44ad7aeb9107f63cbed9ada955777de135840 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Wed, 21 Oct 2020 20:06:08 +0200 Subject: [PATCH 2/2] Update tests --- tests/test_dialogs.py | 10 +++++----- tests/test_index.py | 5 ++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/tests/test_dialogs.py b/tests/test_dialogs.py index abe14b64..e223adae 100644 --- a/tests/test_dialogs.py +++ b/tests/test_dialogs.py @@ -266,7 +266,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp): jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") with open(jsonStats, mode="r", encoding="utf-8") as inFile: - jsonData = json.loads(inFile.read()) + jsonData = json.load(inFile) qtbot.wait(stepDelay) @@ -301,7 +301,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp): jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") with open(jsonStats, mode="r", encoding="utf-8") as inFile: - jsonData = json.loads(inFile.read()) + jsonData = json.load(inFile) assert len(jsonData) == 2 assert jsonData[1]["length"] >= 14.0 @@ -318,7 +318,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp): jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") with open(jsonStats, mode="r", encoding="utf-8") as inFile: - jsonData = json.loads(inFile.read()) + jsonData = json.load(inFile) assert len(jsonData) == 3 @@ -331,7 +331,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp): jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") with open(jsonStats, mode="r", encoding="utf-8") as inFile: - jsonData = json.loads(inFile.read()) + jsonData = json.load(inFile) assert len(jsonData) == 4 @@ -343,7 +343,7 @@ def testWritingStatsExport(qtbot, monkeypatch, yesToAll, nwFuncTemp, nwTemp): jsonStats = os.path.join(nwFuncTemp, "sessionStats.json") 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. # A failed test should in any case produce a 4 diff --git a/tests/test_index.py b/tests/test_index.py index a86fb017..9fabae5b 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -27,7 +27,6 @@ def testIndexBuildCheck(monkeypatch, nwLipsum, nwDummy, nwTempProj, nwRef): theProject.projTree.setSeed(42) assert theProject.openProject(nwLipsum) - theProject.mainConf.debugInfo = True monkeypatch.setattr("nw.core.index.time", lambda: 123.4) theIndex = NWIndex(theProject, nwDummy) @@ -49,7 +48,7 @@ def testIndexBuildCheck(monkeypatch, nwLipsum, nwDummy, nwTempProj, nwRef): raise Exception # Make the save fail - monkeypatch.setattr(json, "dumps", doPanic) + monkeypatch.setattr(json, "dump", doPanic) assert not theIndex.saveIndex() # Make the save pass @@ -83,7 +82,7 @@ def testIndexBuildCheck(monkeypatch, nwLipsum, nwDummy, nwTempProj, nwRef): assert not theIndex.textCounts # Make the load fail - monkeypatch.setattr(json, "loads", doPanic) + monkeypatch.setattr(json, "load", doPanic) assert not theIndex.loadIndex() # Make the load pass