more work
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
import os
|
||||
import sqlite3
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
|
||||
from ..Qt import QtCore
|
||||
from nmreval.configs import config_paths
|
||||
from ..Qt import QtCore, QtWidgets
|
||||
|
||||
DB_FILE = '/autohome/dominik/nmreval/nmreval.db'
|
||||
|
||||
|
||||
class Backup(QtCore.QObject):
|
||||
class BackupManager(QtCore.QObject):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.create_table()
|
||||
@@ -15,18 +18,21 @@ class Backup(QtCore.QObject):
|
||||
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 );"
|
||||
"CREATE TABLE IF NOT EXISTS sessions "
|
||||
"(pid INTEGER NOT NULL, backup_file TEXT NOT NULL, last_save INTEGER);"
|
||||
)
|
||||
|
||||
def create_entry(self, pid: int):
|
||||
backup_path = config_paths() / f'{datetime.now().strftime("%Y-%m-%d_%H%M%S")}.nmr'
|
||||
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
con.execute('INSERT INTO sessions VALUES(?, ?);',
|
||||
(pid, f'/autohome/dominik/nmreval/tmp_{pid}.nmr'))
|
||||
con.execute('INSERT INTO sessions VALUES(?, ?, ?);',
|
||||
(pid, str(backup_path), None))
|
||||
con.commit()
|
||||
con.close()
|
||||
self._pid = pid
|
||||
|
||||
return
|
||||
return backup_path
|
||||
|
||||
def remove_entry(self, pid: int = None):
|
||||
if pid is None:
|
||||
@@ -36,10 +42,15 @@ class Backup(QtCore.QObject):
|
||||
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())
|
||||
|
||||
remaining_processes = res.fetchone()
|
||||
con.close()
|
||||
|
||||
if remaining_processes is None:
|
||||
self.delete_db()
|
||||
|
||||
def delete_db(self):
|
||||
Path(DB_FILE).unlink()
|
||||
|
||||
def close(self):
|
||||
self.remove_entry()
|
||||
|
||||
@@ -61,3 +72,31 @@ class Backup(QtCore.QObject):
|
||||
|
||||
return missing_processes
|
||||
|
||||
def update_last_save(self):
|
||||
con = sqlite3.connect(DB_FILE)
|
||||
con.execute(
|
||||
'UPDATE sessions SET last_save = ? WHERE pid = ?',
|
||||
(datetime.now(), self._pid)
|
||||
)
|
||||
con.commit()
|
||||
con.close()
|
||||
#
|
||||
# def check_for_backup(self):
|
||||
# backup_by_date = sorted(backups, key=lambda x: x[1])
|
||||
# msg = (QtWidgets.QMessageBox()
|
||||
# , 'Backup found',)
|
||||
# f'{len(backups)} backup files in directory {backup_by_date[-1][0].parent} found.\n\n'
|
||||
# f'Latest backup date: {backup_by_date[-1][1]}\n\n'
|
||||
# f'Press Ok to load, Cancel to delete backup, Close to do nothing.',
|
||||
# QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel | QtWidgets.QMessageBox.Close
|
||||
# )
|
||||
#
|
||||
# if msg == QtWidgets.QMessageBox.Ok:
|
||||
# self.management.load_files([str(backup_by_date[-1][0])])
|
||||
# backup_by_date[-1][0].unlink()
|
||||
# elif msg == QtWidgets.QMessageBox.Cancel:
|
||||
# backup_by_date[-1][0].unlink()
|
||||
# else:
|
||||
# pass
|
||||
#
|
||||
|
||||
|
||||
Reference in New Issue
Block a user