Change the way the project file is saved
This commit is contained in:
+3
-3
@@ -330,7 +330,7 @@ class GuiMain(QMainWindow):
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def saveProject(self, isAuto=False):
|
def saveProject(self):
|
||||||
"""Save the current project.
|
"""Save the current project.
|
||||||
"""
|
"""
|
||||||
if not self.hasProject:
|
if not self.hasProject:
|
||||||
@@ -344,7 +344,7 @@ class GuiMain(QMainWindow):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
self.treeView.saveTreeOrder()
|
self.treeView.saveTreeOrder()
|
||||||
self.theProject.saveProject(isAuto)
|
self.theProject.saveProject()
|
||||||
self.theIndex.saveIndex()
|
self.theIndex.saveIndex()
|
||||||
self.mainMenu.updateRecentProjects()
|
self.mainMenu.updateRecentProjects()
|
||||||
|
|
||||||
@@ -840,7 +840,7 @@ class GuiMain(QMainWindow):
|
|||||||
if (self.hasProject and self.theProject.projChanged and
|
if (self.hasProject and self.theProject.projChanged and
|
||||||
self.theProject.projPath is not None):
|
self.theProject.projPath is not None):
|
||||||
logger.debug("Autosaving project")
|
logger.debug("Autosaving project")
|
||||||
self.saveProject(isAuto=True)
|
self.saveProject()
|
||||||
return
|
return
|
||||||
|
|
||||||
def _autoSaveDocument(self):
|
def _autoSaveDocument(self):
|
||||||
|
|||||||
+19
-46
@@ -13,7 +13,7 @@
|
|||||||
import logging
|
import logging
|
||||||
import nw
|
import nw
|
||||||
|
|
||||||
from os import path, mkdir, listdir, unlink
|
from os import path, mkdir, listdir, unlink, rename
|
||||||
from shutil import copyfile
|
from shutil import copyfile
|
||||||
from lxml import etree
|
from lxml import etree
|
||||||
from hashlib import sha256
|
from hashlib import sha256
|
||||||
@@ -180,6 +180,11 @@ class NWProject():
|
|||||||
return
|
return
|
||||||
|
|
||||||
def openProject(self, fileName):
|
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):
|
if not path.isfile(fileName):
|
||||||
fileName = path.join(fileName, nwFiles.PROJ_FILE)
|
fileName = path.join(fileName, nwFiles.PROJ_FILE)
|
||||||
@@ -288,7 +293,7 @@ class NWProject():
|
|||||||
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def saveProject(self, isAuto=False):
|
def saveProject(self):
|
||||||
|
|
||||||
if self.projPath is None:
|
if self.projPath is None:
|
||||||
self.makeAlert("Project path not set, cannot save.", nwAlert.ERROR)
|
self.makeAlert("Project path not set, cannot save.", nwAlert.ERROR)
|
||||||
@@ -303,10 +308,6 @@ class NWProject():
|
|||||||
|
|
||||||
logger.debug("Saving project: %s" % self.projPath)
|
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
|
# Root element and project details
|
||||||
logger.debug("Writing project meta")
|
logger.debug("Writing project meta")
|
||||||
nwXML = etree.Element("novelWriterXML",attrib={
|
nwXML = etree.Element("novelWriterXML",attrib={
|
||||||
@@ -345,9 +346,11 @@ class NWProject():
|
|||||||
self.projTree[tHandle].packXML(xContent)
|
self.projTree[tHandle].packXML(xContent)
|
||||||
|
|
||||||
# Write the xml tree to file
|
# 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:
|
try:
|
||||||
with open(saveFile,mode="wb") as outFile:
|
with open(tempFile, mode="wb") as outFile:
|
||||||
outFile.write(etree.tostring(
|
outFile.write(etree.tostring(
|
||||||
nwXML,
|
nwXML,
|
||||||
pretty_print = True,
|
pretty_print = True,
|
||||||
@@ -358,6 +361,14 @@ class NWProject():
|
|||||||
self.makeAlert(["Failed to save project.",str(e)], nwAlert.ERROR)
|
self.makeAlert(["Failed to save project.",str(e)], nwAlert.ERROR)
|
||||||
return False
|
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.mainConf.setRecent(self.projPath)
|
||||||
self.theParent.setStatus("Saved Project: %s" % self.projName)
|
self.theParent.setStatus("Saved Project: %s" % self.projName)
|
||||||
self.setProjectChanged(False)
|
self.setProjectChanged(False)
|
||||||
@@ -740,42 +751,4 @@ class NWProject():
|
|||||||
itemHandle = self._makeHandle(addSeed+"!")
|
itemHandle = self._makeHandle(addSeed+"!")
|
||||||
return itemHandle
|
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
|
# END Class NWProject
|
||||||
|
|||||||
Reference in New Issue
Block a user