diff --git a/soitool/database.py b/soitool/database.py index 61bf174a0a50eb1c4dcc697bdefe009dfb7c8fc9..4fad3cee602600555c0d8d277a4a5fb8674dc130 100644 --- a/soitool/database.py +++ b/soitool/database.py @@ -2,7 +2,10 @@ import os import sqlite3 import json +from threading import Thread +from time import sleep from datetime import datetime +from PySide2.QtCore import QTimer import soitool.coder # Set name and path to (future) database @@ -30,11 +33,16 @@ class Database: Connects to existing db if found, creates new db if not. If db is created, tables are created and filled. + + Runs a thread that upadtes Code in CodeBook every 24h since last update. """ def __init__(self): db_exists = os.path.exists(DBPATH) + # For automatic update of codes in CodeBook + self.t = Thread(target=self.update_codebook_auto, daemon=True) + if db_exists: print("Connecting to existing DB.") self.conn = sqlite3.connect(DBPATH) @@ -231,17 +239,13 @@ class Database: def update_codebook_auto(self): """ - Update Codebook if it is less than 10 til next update. - - Returns - ------- - sec_to_next_update : float - Seconds to next update + Update Codebook if it is less than 10 sec til next update. """ - sec_to_next_update = self.seconds_to_next_update() - if sec_to_next_update < 10: - self.update_codebook() - return sec_to_next_update + print("DB thread started") + while True: + if self.seconds_to_next_update() < 10: + self.update_codebook() + sleep(self.seconds_to_next_update()) def add_code_to(self, word, mode="ascii"): """ @@ -283,3 +287,6 @@ class Database: # Insert code to the param word in db stmt = "UPDATE CodeBook SET Code = ? WHERE Word = ?" self.conn.execute(stmt, (code, word)) + + +DB = Database() diff --git a/soitool/main_window.py b/soitool/main_window.py index 275b1e903d801526c709fda2e34788a977dd6fdb..f762b525d318e159a04f3020c0f1ca47eb566b54 100644 --- a/soitool/main_window.py +++ b/soitool/main_window.py @@ -9,6 +9,7 @@ from enum import Enum from PySide2.QtWidgets import QMainWindow, QApplication, QTabWidget, QAction from PySide2.QtGui import QIcon from soitool.soi_workspace_widget import SOIWorkspaceWidget +from soitool.database import DB class ModuleType(Enum): @@ -99,7 +100,7 @@ class MainWindow(QMainWindow): self.setCentralWidget(self.tabs) # Add HV logo - filename = 'media/HVlogo.PNG' + filename = "media/HVlogo.PNG" dirname = os.path.dirname(__file__) filepath = os.path.join(dirname, filename) self.setWindowIcon(QIcon(filepath)) @@ -109,4 +110,5 @@ if __name__ == "__main__": app = QApplication(sys.argv) WINDOW = MainWindow() WINDOW.showMaximized() + DB.t.start() app.exec_() diff --git a/test/test_database.py b/test/test_database.py index 86f0512b0e03f1bc5bc20cb9c9fd5de1e8d7ffd3..2c6b41b3660e3baa9683269011b323958a87e289 100644 --- a/test/test_database.py +++ b/test/test_database.py @@ -204,10 +204,10 @@ class DatabaseTest(unittest.TestCase): def test_update_codebook(self): """Test that the codes get updated.""" # Get number of entries - stmt = "SELECT COUNT(*) FROM CodeBook ORDER BY Word" + stmt = "SELECT COUNT(*) FROM CodeBook" number_of_entries = self.database.conn.execute(stmt).fetchall()[0][0] # Get old and updated word-code combinations - stmt = "SELECT Word, Code FROM CodeBook" + stmt = "SELECT Word, Code FROM CodeBook ORDER BY Word" old = self.database.conn.execute(stmt).fetchall() self.database.update_codebook() updated = self.database.conn.execute(stmt).fetchall()