From 3a25e7332bce6e223b39549f7f5a52668bd1a911 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 13 Feb 2020 19:03:21 +0100 Subject: [PATCH] Change the way the project file is saved --- nw/guimain.py | 6 ++-- nw/project/project.py | 65 +++++++++++++------------------------------ 2 files changed, 22 insertions(+), 49 deletions(-) diff --git a/nw/guimain.py b/nw/guimain.py index a1b88781..b1fdf031 100644 --- a/nw/guimain.py +++ b/nw/guimain.py @@ -330,7 +330,7 @@ class GuiMain(QMainWindow): return True - def saveProject(self, isAuto=False): + def saveProject(self): """Save the current project. """ if not self.hasProject: @@ -344,7 +344,7 @@ class GuiMain(QMainWindow): return False self.treeView.saveTreeOrder() - self.theProject.saveProject(isAuto) + self.theProject.saveProject() self.theIndex.saveIndex() self.mainMenu.updateRecentProjects() @@ -840,7 +840,7 @@ class GuiMain(QMainWindow): if (self.hasProject and self.theProject.projChanged and self.theProject.projPath is not None): logger.debug("Autosaving project") - self.saveProject(isAuto=True) + self.saveProject() return def _autoSaveDocument(self): diff --git a/nw/project/project.py b/nw/project/project.py index e6167eb9..a8960de2 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -13,7 +13,7 @@ import logging import nw -from os import path, mkdir, listdir, unlink +from os import path, mkdir, listdir, unlink, rename from shutil import copyfile from lxml import etree from hashlib import sha256 @@ -180,6 +180,11 @@ class NWProject(): return def openProject(self, fileName): + """Open the project file provided, or if doesn't exist, assume + it is a folder, and look for the file within it. If successful, + parse the XML of the file and populate the project variables and + build the tree of project items. + """ if not path.isfile(fileName): fileName = path.join(fileName, nwFiles.PROJ_FILE) @@ -288,7 +293,7 @@ class NWProject(): return True - def saveProject(self, isAuto=False): + def saveProject(self): if self.projPath is None: self.makeAlert("Project path not set, cannot save.", nwAlert.ERROR) @@ -303,10 +308,6 @@ class NWProject(): logger.debug("Saving project: %s" % self.projPath) - # Save a copy of the current file, just in case - if not isAuto: - self._maintainPrevious() - # Root element and project details logger.debug("Writing project meta") nwXML = etree.Element("novelWriterXML",attrib={ @@ -345,9 +346,11 @@ class NWProject(): self.projTree[tHandle].packXML(xContent) # Write the xml tree to file - saveFile = path.join(self.projPath,self.projFile) + tempFile = path.join(self.projPath, self.projFile+"~") + saveFile = path.join(self.projPath, self.projFile) + backFile = path.join(self.projPath, self.projFile[:-3]+"bak") try: - with open(saveFile,mode="wb") as outFile: + with open(tempFile, mode="wb") as outFile: outFile.write(etree.tostring( nwXML, pretty_print = True, @@ -358,6 +361,14 @@ class NWProject(): self.makeAlert(["Failed to save project.",str(e)], nwAlert.ERROR) return False + # If we're here, the file was successfully saved, + # so let's sort out the temps and backups + if path.isfile(backFile): + unlink(backFile) + if path.isfile(saveFile): + rename(saveFile, backFile) + rename(tempFile, saveFile) + self.mainConf.setRecent(self.projPath) self.theParent.setStatus("Saved Project: %s" % self.projName) self.setProjectChanged(False) @@ -740,42 +751,4 @@ class NWProject(): itemHandle = self._makeHandle(addSeed+"!") return itemHandle - def _maintainPrevious(self): - """This function will take the current project file and copy it - into the project cache folder with an incremental file extension - added. These serve as a backup in case the xml file gets - corrupted. - """ - - countFile = path.join(self.projCache, nwFiles.PROJ_COUNT) - projCount = 0 - - if path.isfile(countFile): - try: - with open(countFile, mode="r") as inFile: - projCount = int(inFile.read())+1 - except: - projCount = 0 - - if projCount > 9: - projCount = 0 - - projBackup = "%s.%d" % (nwFiles.PROJ_FILE, projCount) - - try: - copyfile( - path.join(self.projPath, self.projFile), - path.join(self.projCache, projBackup) - ) - except: - logger.error("Failed to write to file %s" % projBackup) - - try: - with open(countFile, mode="w") as outFile: - outFile.write(str(projCount)) - except: - logger.error("Failed to write to file %s" % countFile) - - return - # END Class NWProject