From 070509c6918f665a8a20a01e2d4cf179263a9d9a Mon Sep 17 00:00:00 2001 From: Dominik Demuth Date: Thu, 9 Nov 2023 17:05:17 +0100 Subject: [PATCH] do not overwrite old backup until new backup is done --- src/gui_qt/main/mainwindow.py | 5 ++++- src/nmreval/io/sessionwriter.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/gui_qt/main/mainwindow.py b/src/gui_qt/main/mainwindow.py index 19af6cc..0c4c773 100644 --- a/src/gui_qt/main/mainwindow.py +++ b/src/gui_qt/main/mainwindow.py @@ -1095,7 +1095,10 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): def _autosave(self): # TODO better separate thread may it takes some time to save self.status.setText('Autosave...') - NMRWriter(self.management.graphs, self.management.data).export(self.__backup_path) + success = NMRWriter(self.management.graphs, self.management.data).export(self.__backup_path.with_suffix('.nmr.0')) + if success: + self.__backup_path.with_suffix('.nmr.0').rename(self.__backup_path) + self.status.setText('') def check_for_backup(self): diff --git a/src/nmreval/io/sessionwriter.py b/src/nmreval/io/sessionwriter.py index b65d2d8..272290b 100644 --- a/src/nmreval/io/sessionwriter.py +++ b/src/nmreval/io/sessionwriter.py @@ -22,6 +22,11 @@ class NMRWriter: if isinstance(fname, str): fname = Path(fname) - with fname.open(mode='wb') as fh: - fh.write(pack('16s', b'NMREVAL' + bytes(__version__, 'utf8'))) - dump(self._states, fh) + try: + with fname.open(mode='wb') as fh: + fh.write(pack('16s', b'NMREVAL' + bytes(__version__, 'utf8'))) + dump(self._states, fh) + return True + except Exception as e: + print(f'Saving {fname} failed with exception {e.args}') + return False