issue126-backup #198
| @@ -2,6 +2,7 @@ | |||||||
|  |  | ||||||
| import sys | import sys | ||||||
| import pathlib | import pathlib | ||||||
|  | import os | ||||||
| sys.path.append(str(pathlib.Path(__file__).absolute().parent.parent / 'src')) | sys.path.append(str(pathlib.Path(__file__).absolute().parent.parent / 'src')) | ||||||
|  |  | ||||||
| from nmreval.configs import check_for_config | from nmreval.configs import check_for_config | ||||||
| @@ -20,6 +21,18 @@ from gui_qt import App | |||||||
| app = App(['Team Rocket FTW!']) | app = App(['Team Rocket FTW!']) | ||||||
|  |  | ||||||
| from gui_qt.main.mainwindow import NMRMainWindow | from gui_qt.main.mainwindow import NMRMainWindow | ||||||
|  | from gui_qt.lib.backup import Backup | ||||||
|  |  | ||||||
|  | backuping = Backup() | ||||||
|  | pid = os.getpid() | ||||||
|  |  | ||||||
|  | files = backuping.search_unsaved() | ||||||
|  | print(files) | ||||||
|  |  | ||||||
|  | bck_name = backuping.create_entry(pid) | ||||||
|  | print(sys.executable, sys.argv) | ||||||
|  |  | ||||||
|  | app.aboutToQuit.connect(backuping.close) | ||||||
|  |  | ||||||
| mplQt = NMRMainWindow() | mplQt = NMRMainWindow() | ||||||
| mplQt.show() | mplQt.show() | ||||||
|   | |||||||
| @@ -1,8 +1,8 @@ | |||||||
| # -*- coding: utf-8 -*- | # -*- coding: utf-8 -*- | ||||||
|  |  | ||||||
| # Form implementation generated from reading ui file 'src/resources/_ui/basewindow.ui' | # Form implementation generated from reading ui file 'resources/_ui/basewindow.ui' | ||||||
| # | # | ||||||
| # Created by: PyQt5 UI code generator 5.15.9 | # Created by: PyQt5 UI code generator 5.15.10 | ||||||
| # | # | ||||||
| # WARNING: Any manual changes made to this file will be lost when pyuic5 is | # WARNING: Any manual changes made to this file will be lost when pyuic5 is | ||||||
| # run again.  Do not edit this file unless you know what you are doing. | # run again.  Do not edit this file unless you know what you are doing. | ||||||
|   | |||||||
							
								
								
									
										63
									
								
								src/gui_qt/lib/backup.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								src/gui_qt/lib/backup.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,63 @@ | |||||||
|  | import os | ||||||
|  | import sqlite3 | ||||||
|  |  | ||||||
|  | from ..Qt import QtCore | ||||||
|  |  | ||||||
|  | DB_FILE = '/autohome/dominik/nmreval/nmreval.db' | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class Backup(QtCore.QObject): | ||||||
|  |     def __init__(self): | ||||||
|  |         super().__init__() | ||||||
|  |         self.create_table() | ||||||
|  |         self._pid = None | ||||||
|  |  | ||||||
|  |     def create_table(self): | ||||||
|  |         con = sqlite3.connect(DB_FILE) | ||||||
|  |         con.execute( | ||||||
|  |             "CREATE TABLE IF NOT EXISTS sessions (pid INTEGER NOT NULL, backup_file TEXT NOT NULL );" | ||||||
|  |         ) | ||||||
|  |  | ||||||
|  |     def create_entry(self, pid: int): | ||||||
|  |         con = sqlite3.connect(DB_FILE) | ||||||
|  |         con.execute('INSERT INTO sessions VALUES(?, ?);', | ||||||
|  |                     (pid, f'/autohome/dominik/nmreval/tmp_{pid}.nmr')) | ||||||
|  |         con.commit() | ||||||
|  |         con.close() | ||||||
|  |         self._pid = pid | ||||||
|  |  | ||||||
|  |         return | ||||||
|  |  | ||||||
|  |     def remove_entry(self, pid: int = None): | ||||||
|  |         if pid is None: | ||||||
|  |             pid = self._pid | ||||||
|  |  | ||||||
|  |         con = sqlite3.connect(DB_FILE) | ||||||
|  |         cursor = con.execute('DELETE FROM sessions WHERE pid = ?;', (pid,)) | ||||||
|  |         res = cursor.execute('SELECT COUNT(sessions.pid) FROM sessions GROUP BY sessions.pid;') | ||||||
|  |         con.commit() | ||||||
|  |         print(res.fetchall()) | ||||||
|  |  | ||||||
|  |         con.close() | ||||||
|  |  | ||||||
|  |     def close(self): | ||||||
|  |         self.remove_entry() | ||||||
|  |  | ||||||
|  |     def search_unsaved(self): | ||||||
|  |         con = sqlite3.connect(DB_FILE) | ||||||
|  |         cursor = con.cursor() | ||||||
|  |         res = cursor.execute('SELECT sessions.pid, sessions.backup_file FROM sessions;') | ||||||
|  |         con.commit() | ||||||
|  |         data = res.fetchall() | ||||||
|  |         con.close() | ||||||
|  |  | ||||||
|  |         missing_processes = [] | ||||||
|  |  | ||||||
|  |         for pid, fname in data: | ||||||
|  |             try: | ||||||
|  |                 os.kill(pid, 0) | ||||||
|  |             except ProcessLookupError: | ||||||
|  |                 missing_processes.append((pid, fname)) | ||||||
|  |  | ||||||
|  |         return missing_processes | ||||||
|  |  | ||||||
| @@ -22,6 +22,7 @@ from ..graphs.graphwindow import QGraphWindow | |||||||
| from ..graphs.movedialog import QMover | from ..graphs.movedialog import QMover | ||||||
| from ..io.fcbatchreader import QFCReader | from ..io.fcbatchreader import QFCReader | ||||||
| from ..io.filedialog import * | from ..io.filedialog import * | ||||||
|  | from ..lib.backup import Backup | ||||||
| from ..lib.iconloading import make_action_icons, get_icon | from ..lib.iconloading import make_action_icons, get_icon | ||||||
| from ..lib.pg_objects import RegionItem | from ..lib.pg_objects import RegionItem | ||||||
| from ..lib.starter import make_starter | from ..lib.starter import make_starter | ||||||
| @@ -54,6 +55,7 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): | |||||||
|  |  | ||||||
|         self.management = UpperManagement(self) |         self.management = UpperManagement(self) | ||||||
|  |  | ||||||
|  |  | ||||||
|         self.fitlimitvalues = [None, None] |         self.fitlimitvalues = [None, None] | ||||||
|         self.fitpreview = [] |         self.fitpreview = [] | ||||||
|         self._fit_plot_id = None |         self._fit_plot_id = None | ||||||
| @@ -89,7 +91,8 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): | |||||||
|         self.fit_timer = QtCore.QTimer() |         self.fit_timer = QtCore.QTimer() | ||||||
|         self.fit_timer.setInterval(500) |         self.fit_timer.setInterval(500) | ||||||
|         self.fit_timer.timeout.connect( |         self.fit_timer.timeout.connect( | ||||||
|             lambda: self.status.setText(f'Fit running... ({self.management.fitter.step} evaluations)')) |             lambda: self.status.setText(f'Fit running... ({self.management.fitter.step} evaluations)') | ||||||
|  |         ) | ||||||
|  |  | ||||||
|     def _init_gui(self): |     def _init_gui(self): | ||||||
|         self.setupUi(self) |         self.setupUi(self) | ||||||
| @@ -235,8 +238,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): | |||||||
|         self.actionNext_window.triggered.connect(lambda: self.area.activateNextSubWindow()) |         self.actionNext_window.triggered.connect(lambda: self.area.activateNextSubWindow()) | ||||||
|         self.actionPrevious.triggered.connect(lambda: self.area.activatePreviousSubWindow()) |         self.actionPrevious.triggered.connect(lambda: self.area.activatePreviousSubWindow()) | ||||||
|  |  | ||||||
|         self.closeSignal.connect(self.close) |  | ||||||
|  |  | ||||||
|         self.action_norm_max.triggered.connect(lambda: self.management.apply('norm', ('max',))) |         self.action_norm_max.triggered.connect(lambda: self.management.apply('norm', ('max',))) | ||||||
|         self.action_norm_max_abs.triggered.connect(lambda: self.management.apply('norm', ('maxabs',))) |         self.action_norm_max_abs.triggered.connect(lambda: self.management.apply('norm', ('maxabs',))) | ||||||
|         self.action_norm_first.triggered.connect(lambda: self.management.apply('norm', ('first',))) |         self.action_norm_first.triggered.connect(lambda: self.management.apply('norm', ('first',))) | ||||||
| @@ -864,10 +865,6 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): | |||||||
|     def item_from_graph(self, item, graph_id): |     def item_from_graph(self, item, graph_id): | ||||||
|         self.management.graphs[graph_id].remove_external(item) |         self.management.graphs[graph_id].remove_external(item) | ||||||
|  |  | ||||||
|     def closeEvent(self, evt): |  | ||||||
|         # self._write_settings() |  | ||||||
|         self.close() |  | ||||||
|  |  | ||||||
|     @QtCore.pyqtSlot(int) |     @QtCore.pyqtSlot(int) | ||||||
|     def request_data(self, idx): |     def request_data(self, idx): | ||||||
|         idd = self.datawidget.get_indexes(idx=idx-1) |         idd = self.datawidget.get_indexes(idx=idx-1) | ||||||
| @@ -1097,6 +1094,8 @@ class NMRMainWindow(QtWidgets.QMainWindow, Ui_BaseWindow): | |||||||
|         # remove backup file when closing |         # remove backup file when closing | ||||||
|         self.__backup_path.unlink(missing_ok=True) |         self.__backup_path.unlink(missing_ok=True) | ||||||
|  |  | ||||||
|  |         print('Close me', self.sender()) | ||||||
|  |  | ||||||
|         super().close() |         super().close() | ||||||
|  |  | ||||||
|     def read_state(self): |     def read_state(self): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user