Use an enum to define the thread status
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
from PyQt5 import uic
|
from PyQt5 import uic
|
||||||
from PyQt5.QtCore import Qt, pyqtSlot
|
from PyQt5.QtCore import Qt, pyqtSlot
|
||||||
from PyQt5.QtWidgets import QWidget, QMessageBox
|
from PyQt5.QtWidgets import QWidget, QMessageBox
|
||||||
from download_thread import DownloadThread
|
from threads import DownloadThread, ThreadStatus
|
||||||
Ui_Download_window, _ = uic.loadUiType("download_db_window.ui")
|
Ui_Download_window, _ = uic.loadUiType("download_db_window.ui")
|
||||||
|
|
||||||
class DownloadWindow(QWidget, Ui_Download_window):
|
class DownloadWindow(QWidget, Ui_Download_window):
|
||||||
@@ -37,18 +37,15 @@ class DownloadWindow(QWidget, Ui_Download_window):
|
|||||||
|
|
||||||
self.download_thread = DownloadThread(db_location, data_folder)
|
self.download_thread = DownloadThread(db_location, data_folder)
|
||||||
self.download_thread.finished.connect(self.wait_close)
|
self.download_thread.finished.connect(self.wait_close)
|
||||||
self.download_thread.no_connection_error.connect(self.show_no_connection_warning)
|
|
||||||
self.download_thread.bad_db_download_error.connect(self.show_bad_download_warning)
|
|
||||||
|
|
||||||
self.cancel_btn.clicked.connect(self.terminate_process)
|
self.cancel_btn.clicked.connect(self.terminate_process)
|
||||||
|
|
||||||
@pyqtSlot()
|
|
||||||
def show_no_connection_warning(self):
|
def show_no_connection_warning(self):
|
||||||
self.bad_db_download_msg.setText(f"Unable to correctly download the database.\nReason: {self.download_thread.reason}")
|
self.bad_db_download_msg.setText(f"""Unable to correctly download the database.
|
||||||
|
Reason: {self.download_thread.reason}""")
|
||||||
self.no_internet_msg.show()
|
self.no_internet_msg.show()
|
||||||
self.everything_ok = False
|
self.everything_ok = False
|
||||||
|
|
||||||
@pyqtSlot()
|
|
||||||
def show_bad_download_warning(self):
|
def show_bad_download_warning(self):
|
||||||
self.bad_db_download_msg.show()
|
self.bad_db_download_msg.show()
|
||||||
self.everything_ok = False
|
self.everything_ok = False
|
||||||
@@ -62,7 +59,13 @@ class DownloadWindow(QWidget, Ui_Download_window):
|
|||||||
|
|
||||||
@pyqtSlot()
|
@pyqtSlot()
|
||||||
def wait_close(self):
|
def wait_close(self):
|
||||||
if self.download_thread.regular_execution:
|
if self.download_thread.status == ThreadStatus.ok:
|
||||||
|
self.close()
|
||||||
|
elif self.download_thread.status == ThreadStatus.no_connection_err:
|
||||||
|
self.show_no_connection_warning()
|
||||||
|
elif self.download_thread.status == ThreadStatus.bad_download_err:
|
||||||
|
self.show_bad_download_warning
|
||||||
|
else:
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
def reject(self):
|
def reject(self):
|
||||||
|
|||||||
4
main.py
4
main.py
@@ -55,7 +55,6 @@ class MyApp(QMainWindow, Ui_MainWindow):
|
|||||||
self.setupUi(self)
|
self.setupUi(self)
|
||||||
self.set_initial_size()
|
self.set_initial_size()
|
||||||
self.download_window = DownloadWindow(self.db_location, self.data_folder)
|
self.download_window = DownloadWindow(self.db_location, self.data_folder)
|
||||||
self.show()
|
|
||||||
self.actionExit.triggered.connect(qApp.quit)
|
self.actionExit.triggered.connect(qApp.quit)
|
||||||
self.action_update_database.triggered.connect(self.download_db)
|
self.action_update_database.triggered.connect(self.download_db)
|
||||||
self.db_version = None
|
self.db_version = None
|
||||||
@@ -289,6 +288,9 @@ class MyApp(QMainWindow, Ui_MainWindow):
|
|||||||
self.description_text,]
|
self.description_text,]
|
||||||
|
|
||||||
self.url_button.clicked.connect(self.go_to_web_page_signal)
|
self.url_button.clicked.connect(self.go_to_web_page_signal)
|
||||||
|
|
||||||
|
self.show()
|
||||||
|
|
||||||
self.load_db()
|
self.load_db()
|
||||||
self.display_signals()
|
self.display_signals()
|
||||||
self.search_bar.textChanged.connect(self.display_signals)
|
self.search_bar.textChanged.connect(self.display_signals)
|
||||||
|
|||||||
@@ -1,24 +1,31 @@
|
|||||||
|
from enum import Enum, auto
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from os import mkdir
|
from os import mkdir
|
||||||
import os.path
|
import os.path
|
||||||
from shutil import rmtree
|
from shutil import rmtree
|
||||||
import urllib3
|
import urllib3
|
||||||
from zipfile import ZipFile
|
from zipfile import ZipFile
|
||||||
from PyQt5.QtCore import QThread, pyqtSignal
|
from PyQt5.QtCore import QThread
|
||||||
import utilities
|
import utilities
|
||||||
|
|
||||||
class DownloadThread(QThread):
|
class ThreadStatus(Enum):
|
||||||
no_connection_error = pyqtSignal()
|
ok = auto()
|
||||||
bad_db_download_error = pyqtSignal()
|
no_connection_err = auto()
|
||||||
bad_file_error = pyqtSignal()
|
no_file_err = auto()
|
||||||
|
bad_download_err = auto()
|
||||||
|
|
||||||
|
class DownloadThread(QThread):
|
||||||
def __init__(self, db_location, path):
|
def __init__(self, db_location, path):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.__db_location = db_location
|
self.__db_location = db_location
|
||||||
self.__path = path
|
self.__path = path
|
||||||
self.regular_execution = True
|
self.__status = None
|
||||||
self.reason = 0
|
self.reason = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def status(self):
|
||||||
|
return self.__status
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.terminate()
|
self.terminate()
|
||||||
self.wait()
|
self.wait()
|
||||||
@@ -29,26 +36,27 @@ class DownloadThread(QThread):
|
|||||||
# db = urllib.request.urlopen(self.__db_location)
|
# db = urllib.request.urlopen(self.__db_location)
|
||||||
# raise urllib.error.URLError('Test')
|
# raise urllib.error.URLError('Test')
|
||||||
except urllib3.exceptions.MaxRetryError: # No internet connection.
|
except urllib3.exceptions.MaxRetryError: # No internet connection.
|
||||||
self.regular_execution = False
|
# self.no_connection_error.emit()
|
||||||
self.no_connection_error.emit()
|
self.__status = ThreadStatus.no_connection_err
|
||||||
return
|
return
|
||||||
if db.status != 200:
|
if db.status != 200:
|
||||||
self.regular_execution = False
|
|
||||||
self.reason = db.reason
|
self.reason = db.reason
|
||||||
self.bad_db_download_error.emit()
|
# self.bad_download_error.emit()
|
||||||
|
self.__status = ThreadStatus.bad_download_err
|
||||||
return
|
return
|
||||||
if not utilities.checksum_ok(db.data, "folder"):
|
if not utilities.checksum_ok(db.data, "folder"):
|
||||||
regular_execution = False
|
# self.bad_download_error.emit()
|
||||||
self.bad_db_download_error.emit()
|
self.__status = ThreadStatus.bad_download_err
|
||||||
return
|
return
|
||||||
if os.path.exists(self.__path):
|
if os.path.exists(self.__path):
|
||||||
rmtree(self.__path)
|
rmtree(self.__path)
|
||||||
try:
|
try:
|
||||||
# data_folder = db.read()
|
# data_folder = db.read()
|
||||||
# data_folder = db.data
|
|
||||||
with ZipFile(BytesIO(db.data)) as zipped:
|
with ZipFile(BytesIO(db.data)) as zipped:
|
||||||
zipped.extractall()
|
zipped.extractall()
|
||||||
except:
|
except:
|
||||||
self.regular_execution = False
|
# self.bad_file_error.emit()
|
||||||
self.bad_file_error.emit()
|
self.__status = ThreadStatus.bad_file_err
|
||||||
return
|
return
|
||||||
|
else:
|
||||||
|
self.__status = ThreadStatus.ok
|
||||||
Reference in New Issue
Block a user