issue126-backup #198
@ -24,20 +24,27 @@ app = App(['Team Rocket FTW!'])
|
||||
from gui_qt.main.mainwindow import NMRMainWindow
|
||||
from gui_qt.lib.backup import BackupManager
|
||||
|
||||
|
||||
def do_autosave():
|
||||
success = mplQt.autosave()
|
||||
if success:
|
||||
backuping.update_last_save()
|
||||
|
||||
|
||||
backuping = BackupManager()
|
||||
pid = os.getpid()
|
||||
|
||||
files = backuping.search_unsaved()
|
||||
print(files)
|
||||
print(sys.executable, sys.argv)
|
||||
|
||||
pid = os.getpid()
|
||||
bck_name = backuping.create_entry(pid)
|
||||
mplQt = NMRMainWindow(bck_file=bck_name)
|
||||
|
||||
print(sys.executable, sys.argv)
|
||||
timer = QtCore.QTimer()
|
||||
do_autosave()
|
||||
|
||||
timer.timeout.connect(mplQt._autosave)
|
||||
timer.timeout.connect(backuping.update_last_save)
|
||||
timer = QtCore.QTimer()
|
||||
timer.timeout.connect(do_autosave)
|
||||
timer.start(3 * 1000)
|
||||
|
||||
app.aboutToQuit.connect(backuping.close)
|
||||
|
@ -39,20 +39,28 @@ class BackupManager(QtCore.QObject):
|
||||
pid = self._pid
|
||||
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
cursor = con.execute('DELETE FROM sessions WHERE pid = ?;', (pid,))
|
||||
cursor = con.cursor()
|
||||
|
||||
res = cursor.execute('DELETE FROM sessions WHERE pid = ?;', (pid,))
|
||||
con.commit()
|
||||
|
||||
self.delete_db_if_empty()
|
||||
|
||||
def delete_db_if_empty(self):
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
cursor = con.cursor()
|
||||
res = cursor.execute('SELECT COUNT(sessions.pid) FROM sessions GROUP BY sessions.pid;')
|
||||
con.commit()
|
||||
|
||||
remaining_processes = res.fetchone()
|
||||
con.close()
|
||||
|
||||
if remaining_processes is None:
|
||||
self.delete_db()
|
||||
|
||||
def delete_db(self):
|
||||
Path(DB_FILE).unlink()
|
||||
Path(DB_FILE).unlink()
|
||||
|
||||
def close(self):
|
||||
self.remove_entry()
|
||||
self.delete_db_if_empty()
|
||||
|
||||
def search_unsaved(self):
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
@ -70,7 +78,9 @@ class BackupManager(QtCore.QObject):
|
||||
except ProcessLookupError:
|
||||
missing_processes.append((pid, fname))
|
||||
|
||||
return missing_processes
|
||||
if missing_processes:
|
||||
msg = QtWidgets.QMessageBox()
|
||||
msg.exec()
|
||||
|
||||
def update_last_save(self):
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
|
@ -2,6 +2,7 @@ from __future__ import annotations
|
||||
|
||||
import datetime
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
@ -54,7 +55,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
|
||||
|
||||
self.management = UpperManagement(self)
|
||||
|
||||
|
||||
self.fitlimitvalues = [None, None]
|
||||
self.fitpreview = []
|
||||
self._fit_plot_id = None
|
||||
@ -76,23 +76,14 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
|
||||
self._init_gui()
|
||||
self._init_signals()
|
||||
|
||||
if os.getenv('APPIMAGE') is not None:
|
||||
if Updater.get_update_information(os.getenv('APPIMAGE'))[0]:
|
||||
self.look_for_update()
|
||||
|
||||
# self.check_for_backup()
|
||||
|
||||
self.__timer = QtCore.QTimer()
|
||||
self.__backup_path = config_paths() / f'{datetime.datetime.now().strftime("%Y-%m-%d_%H%M%S")}.nmr'
|
||||
self.__timer.start(3*60*1000) # every three minutes
|
||||
self.__timer.timeout.connect(self._autosave)
|
||||
|
||||
self.fit_timer = QtCore.QTimer()
|
||||
self.fit_timer.setInterval(500)
|
||||
self.fit_timer.timeout.connect(
|
||||
lambda: self.status.setText(f'Fit running... ({self.management.fitter.step} evaluations)')
|
||||
)
|
||||
|
||||
self.__backup_path = pathlib.Path(bck_file)
|
||||
|
||||
def _init_gui(self):
|
||||
self.setupUi(self)
|
||||
make_action_icons(self)
|
||||
@ -110,8 +101,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
|
||||
self.fitlim_button.setIcon(get_icon('fit_region'))
|
||||
self.toolBar_fit.addWidget(self.fitlim_button)
|
||||
|
||||
# self.area.dragEnterEvent = self.dragEnterEvent
|
||||
|
||||
while self.tabWidget.count() > 2:
|
||||
self.tabWidget.removeTab(self.tabWidget.count()-1)
|
||||
|
||||
@ -166,6 +155,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
|
||||
self.menuData.insertAction(self.actionRedo, self.actionUndo)
|
||||
|
||||
self.action_save_fit_parameter.triggered.connect(self.save_fit_parameter)
|
||||
# noinspection PyUnresolvedReferences
|
||||
self.ac_group2.triggered.connect(self.change_fit_limits)
|
||||
|
||||
self.t1action.triggered.connect(lambda: self._show_tab('t1_temp'))
|
||||
@ -1093,11 +1083,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
|
||||
def close(self):
|
||||
write_state({'History': {'recent path': str(self.path)}})
|
||||
|
||||
# remove backup file when closing
|
||||
self.__backup_path.unlink(missing_ok=True)
|
||||
|
||||
print('Close me', self.sender())
|
||||
|
||||
super().close()
|
||||
|
||||
def read_state(self):
|
||||
@ -1121,7 +1106,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
|
||||
|
||||
QLog(parent=self).show()
|
||||
|
||||
def _autosave(self):
|
||||
def autosave(self) -> bool:
|
||||
# TODO better separate thread may it takes some time to save
|
||||
self.status.setText('Autosave...')
|
||||
success = NMRWriter(self.management.graphs, self.management.data).export(self.__backup_path.with_suffix('.nmr.0'))
|
||||
@ -1130,6 +1115,8 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow):
|
||||
|
||||
self.status.setText('')
|
||||
|
||||
return success
|
||||
|
||||
@QtCore.pyqtSlot(name='on_actionCreate_starter_triggered')
|
||||
def create_starter(self):
|
||||
make_starter(os.getenv('APPIMAGE'))
|
||||
|
Loading…
Reference in New Issue
Block a user