Downloader has been generalized, now network_utils is update_utils

This commit is contained in:
Marco Dalla Tiezza
2024-06-13 01:05:53 +02:00
parent 79891899ce
commit 52c4fbcce9
5 changed files with 206 additions and 32 deletions

View File

@@ -7,7 +7,7 @@ from artemis.utils.constants import Constants, Messages
from artemis.utils.sys_utils import open_directory, make_tar, unpack_tar
from artemis.utils.sql_utils import ArtemisDatabase, ArtemisSignal
from artemis.utils.path_utils import DATA_DIR
from artemis.utils.network_utils import NetworkManager
from artemis.utils.update_utils import UpdateManager
from artemis.utils.generic_utils import generate_filter_query
from artemis.utils.path_utils import normalize_dialog_path
from artemis.utils.config_utils import CONFIGURE_QT
@@ -62,13 +62,12 @@ class UIArtemis(QObject):
# Creating istances for other windows
self.preferences = UIPreferences(self)
self.dbmanager = UIdbmanager(self)
self.downloader = UIDownloader(self)
self.spaceweather = UIspaceweather(self)
self.docmanager = UIdocumentsmanager(self)
self.sigeditor = UIsignaleditor(self)
self.cateditor = UIcategoryeditor(self)
self.network_manager = NetworkManager(self)
self.update_manager = UpdateManager(self)
self.autoload_db()
@@ -217,15 +216,19 @@ class UIArtemis(QObject):
def check_update_db(self):
""" User manual check for updates db updates
"""
self.network_manager.show_popup = True
self.network_manager.check_updates()
self.update_manager.check_updates(True)
def start_download_db(self):
""" Show the downloader and start the download of the sigid db
"""
self.downloader.show_ui.emit()
self.downloader.on_start()
self.downloader = UIDownloader(self)
self.downloader.finished.connect(self.update_manager.post_download_db)
self.downloader.on_start(
self.update_manager.remote_db_url,
self.update_manager.remote_db_size,
DATA_DIR
)
def dialog_download_db(self, message_type, title, message):

View File

@@ -2,14 +2,12 @@ from PySide6.QtQml import QQmlApplicationEngine
from PySide6.QtCore import QObject, Slot, Signal, QUrl, QSaveFile, QDir, QIODevice
from PySide6.QtNetwork import QNetworkReply, QNetworkRequest, QNetworkAccessManager
from artemis.utils.config_utils import *
from artemis.utils.sys_utils import delete_file, delete_dir, match_hash, unpack_tar
from artemis.utils.constants import Messages
from artemis.utils.path_utils import DATA_DIR
class UIDownloader(QObject):
# Python > QML Signals
finished = Signal()
show_ui = Signal()
close_ui = Signal()
update_progress_bar = Signal(int, int)
@@ -25,6 +23,9 @@ class UIDownloader(QObject):
self._engine.load('qrc:/ui/Downloader.qml')
self._window = self._engine.rootObjects()[0]
self.file_url = None
self.file_size = None
self._connect()
@@ -39,20 +40,22 @@ class UIDownloader(QObject):
self.update_status.connect(self._window.updateStatus)
@Slot()
def on_start(self):
def on_start(self, url, filesize, save_path):
""" Start the download of the DB taking the needed url and size from
the attributes of the UpdatesController class
"""
url_file = QUrl(self._parent.network_manager.remote_db_url)
dest_path = QDir(DATA_DIR)
self.dest_file = dest_path.filePath(url_file.fileName())
self.show_ui.emit()
self.file_url = QUrl(url)
self.file_size = filesize
dest_path = QDir(save_path)
self.dest_file = dest_path.filePath(self.file_url.fileName())
self.file = QSaveFile(self.dest_file)
if self.file.open(QIODevice.WriteOnly):
# Start a GET HTTP request
self.manager = QNetworkAccessManager(self)
self.reply = self.manager.get(QNetworkRequest(url_file))
self.reply = self.manager.get(QNetworkRequest(self.file_url))
self.reply.downloadProgress.connect(self.on_progress)
self.reply.finished.connect(self.on_finished)
self.reply.readyRead.connect(self.on_ready_read)
@@ -74,8 +77,6 @@ class UIDownloader(QObject):
if self.file:
self.file.cancelWriting()
self.close_ui.emit()
@Slot()
def on_ready_read(self):
@@ -95,25 +96,19 @@ class UIDownloader(QObject):
if self.file:
self.file.commit()
if self.reply.error() == QNetworkReply.NoError:
self.finished.emit()
self.update_status.emit("Checking DB integrity (SHA-256)")
if match_hash(self.dest_file, self._parent.network_manager.remote_db_hash):
self.update_status.emit("Unpacking archive...")
delete_dir(DATA_DIR / 'SigID')
unpack_tar(self.dest_file, DATA_DIR / 'SigID')
delete_file(self.dest_file)
self._parent.load_db('SigID')
self.close_ui.emit()
self.close_ui.emit()
@Slot(int, int)
def on_progress(self, bytesReceived: int):
""" Update progress bar and label
"""
total_bytes = self._parent.network_manager.remote_db_size
self.update_status.emit("{:.1f} Mb / {:.1f} Mb".format(bytesReceived/10**6, total_bytes/10**6))
self.update_progress_bar.emit(bytesReceived, total_bytes)
self.update_status.emit("{:.1f} Mb / {:.1f} Mb".format(bytesReceived/10**6, self.file_size/10**6))
self.update_progress_bar.emit(bytesReceived, self.file_size)
@Slot(QNetworkReply.NetworkError)