From c28a246d238402c9196013a2254ae2eb227526bd Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 13 Feb 2020 19:16:29 +0100 Subject: [PATCH] Make document saving consistent with how project file is saved --- nw/project/document.py | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/nw/project/document.py b/nw/project/document.py index e2a46d65..9339a4cd 100644 --- a/nw/project/document.py +++ b/nw/project/document.py @@ -100,25 +100,23 @@ class NWDoc(): mkdir(dataPath) logger.debug("Created folder %s" % dataPath) - docTemp = path.join(dataPath,docFile[:-3]+"tmp") - docBack = path.join(dataPath,docFile[:-3]+"bak") - - if path.isfile(docTemp): - unlink(docTemp) - if path.isfile(docBack): - rename(docBack,docTemp) - if path.isfile(docPath): - rename(docPath,docBack) + docTemp = path.join(dataPath, docFile+"~") + docBack = path.join(dataPath, docFile[:-3]+"bak") try: - with open(docPath,mode="w",encoding="utf8") as outFile: + with open(docTemp,mode="w",encoding="utf8") as outFile: outFile.write(docText) except Exception as e: self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR) return False - if path.isfile(docTemp): - unlink(docTemp) + # If we're here, the file was successfully saved, + # so let's sort out the temps and backups + if path.isfile(docBack): + unlink(docBack) + if path.isfile(docPath): + rename(docPath, docBack) + rename(docTemp, docPath) self.theParent.statusBar.setStatus("Saved Document: %s" % self.theItem.itemName) @@ -132,8 +130,8 @@ class NWDoc(): dataPath = path.join(self.theProject.projPath, docDir) chkList = [] chkList.append(path.join(dataPath, docFile)) - chkList.append(path.join(dataPath,docFile[:-3]+"tmp")) - chkList.append(path.join(dataPath,docFile[:-3]+"bak")) + chkList.append(path.join(dataPath, docFile+"~")) + chkList.append(path.join(dataPath, docFile[:-3]+"bak")) for chkFile in chkList: if path.isfile(chkFile): try: @@ -147,7 +145,7 @@ class NWDoc(): @staticmethod def assemblePath(tHandle, docExt): if tHandle is None: - return None + return None, None docDir = "data_"+tHandle[0] docFile = tHandle[1:13]+"_"+docExt return docDir, docFile