Make document saving consistent with how project file is saved

This commit is contained in:
Veronica K. B. Olsen
2020-02-13 19:16:29 +01:00
parent ca7617ad07
commit c28a246d23
+13 -15
View File
@@ -100,25 +100,23 @@ class NWDoc():
mkdir(dataPath) mkdir(dataPath)
logger.debug("Created folder %s" % dataPath) logger.debug("Created folder %s" % dataPath)
docTemp = path.join(dataPath,docFile[:-3]+"tmp") docTemp = path.join(dataPath, docFile+"~")
docBack = path.join(dataPath,docFile[:-3]+"bak") 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)
try: try:
with open(docPath,mode="w",encoding="utf8") as outFile: with open(docTemp,mode="w",encoding="utf8") as outFile:
outFile.write(docText) outFile.write(docText)
except Exception as e: except Exception as e:
self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR) self.makeAlert(["Could not save document.",str(e)], nwAlert.ERROR)
return False return False
if path.isfile(docTemp): # If we're here, the file was successfully saved,
unlink(docTemp) # 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) self.theParent.statusBar.setStatus("Saved Document: %s" % self.theItem.itemName)
@@ -132,8 +130,8 @@ class NWDoc():
dataPath = path.join(self.theProject.projPath, docDir) dataPath = path.join(self.theProject.projPath, docDir)
chkList = [] chkList = []
chkList.append(path.join(dataPath, docFile)) chkList.append(path.join(dataPath, docFile))
chkList.append(path.join(dataPath,docFile[:-3]+"tmp")) chkList.append(path.join(dataPath, docFile+"~"))
chkList.append(path.join(dataPath,docFile[:-3]+"bak")) chkList.append(path.join(dataPath, docFile[:-3]+"bak"))
for chkFile in chkList: for chkFile in chkList:
if path.isfile(chkFile): if path.isfile(chkFile):
try: try:
@@ -147,7 +145,7 @@ class NWDoc():
@staticmethod @staticmethod
def assemblePath(tHandle, docExt): def assemblePath(tHandle, docExt):
if tHandle is None: if tHandle is None:
return None return None, None
docDir = "data_"+tHandle[0] docDir = "data_"+tHandle[0]
docFile = tHandle[1:13]+"_"+docExt docFile = tHandle[1:13]+"_"+docExt
return docDir, docFile return docDir, docFile