From beb656267cec976402295d2eed0dd44d645c3908 Mon Sep 17 00:00:00 2001 From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com> Date: Thu, 13 Feb 2020 21:22:36 +0100 Subject: [PATCH] If opening a project file fails, try to open the backup file instead --- nw/project/project.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/nw/project/project.py b/nw/project/project.py index 03aacf42..c564a1f6 100644 --- a/nw/project/project.py +++ b/nw/project/project.py @@ -210,8 +210,20 @@ class NWProject(): nwXML = etree.parse(fileName) except Exception as e: self.makeAlert(["Failed to parse project xml.",str(e)], nwAlert.ERROR) - self.clearProject() - return False + + # Trying to open backup file instead + backFile = fileName[:-3]+"bak" + if path.isfile(backFile): + self.makeAlert("Attempting to open backup project file instead.", nwAlert.INFO) + try: + nwXML = etree.parse(backFile) + except Exception as e: + self.makeAlert(["Failed to parse project xml.",str(e)], nwAlert.ERROR) + self.clearProject() + return False + else: + self.clearProject() + return False xRoot = nwXML.getroot() nwxRoot = xRoot.tag @@ -295,6 +307,11 @@ class NWProject(): return True def saveProject(self): + """Save the project main XML file. The saving command itself + uses a temporary filename, and the file is renamed afterwards to + make sure if the save fails, we're not left with a truncated + file. + """ if self.projPath is None: self.makeAlert("Project path not set, cannot save.", nwAlert.ERROR)