From f358d5462f797d30dce2260273d8aeee5b6d32ca Mon Sep 17 00:00:00 2001 From: Vxrtrauter Date: Sat, 11 Oct 2025 23:52:25 +0200 Subject: [PATCH] refractor files and improve aria2 process killing, needs improvement --- core/interface/gui.py | 9 +++++++-- core/network/aria2_integration.py | 2 +- core/utils/data/state.py | 1 + core/utils/general/shutdown.py | 16 ++++++++++++++++ core/utils/{ => general}/wrappers.py | 0 core/utils/network/download.py | 2 +- main.py | 20 +++++--------------- 7 files changed, 31 insertions(+), 19 deletions(-) create mode 100644 core/utils/general/shutdown.py rename core/utils/{ => general}/wrappers.py (100%) diff --git a/core/interface/gui.py b/core/interface/gui.py index 0a08059..efa62dd 100644 --- a/core/interface/gui.py +++ b/core/interface/gui.py @@ -12,12 +12,13 @@ from PySide6.QtWidgets import ( QTabWidget, QProgressBar, ) -from PySide6.QtGui import QIcon, QAction +from PySide6.QtGui import QIcon, QAction, QCloseEvent import darkdetect import threading -from core.utils.wrappers import run_thread +from core.utils.general.wrappers import run_thread from core.utils.data.state import state from core.utils.network.download import download_selected +from core.utils.general.shutdown import closehelper from core.interface.utils.tabhelper import create_tab from core.interface.utils.searchhelper import return_pressed from core.interface.dialogs.settings import settings_dialog @@ -108,6 +109,10 @@ class MainWindow(QtWidgets.QMainWindow, QWidget): self.progress_timer.timeout.connect(lambda: run_thread(threading.Thread(target=self.update_progress))) self.progress_timer.start(1000) + def closeEvent(self, event: QCloseEvent): + closehelper() + event.accept() + def set_tracker(self, _): state.tracker = self.tracker_list.currentText() diff --git a/core/network/aria2_integration.py b/core/network/aria2_integration.py index d585ac4..aeb27d8 100644 --- a/core/network/aria2_integration.py +++ b/core/network/aria2_integration.py @@ -58,6 +58,6 @@ def send_notification(shutdown_event): message = f"{download.name} has finished downloading.", timeout = 4 ) - except Exception: + except Exception as e: pass time.sleep(5) diff --git a/core/utils/data/state.py b/core/utils/data/state.py index f685d20..425d521 100644 --- a/core/utils/data/state.py +++ b/core/utils/data/state.py @@ -15,6 +15,7 @@ class AppState: speed_limit: int = 0 aria2process: Optional[Any] = None aria2: Any = None + aria2p: Any = None aria2_threads: int = 4 state = AppState(posts=[], post_titles=[], post_urls=[], post_author=[], download_path="") diff --git a/core/utils/general/shutdown.py b/core/utils/general/shutdown.py new file mode 100644 index 0000000..b6de125 --- /dev/null +++ b/core/utils/general/shutdown.py @@ -0,0 +1,16 @@ +from core.utils.data.state import state +import threading + + +shutdown_event = threading.Event() + +def kill_aria2server(): + if state.aria2process: + state.aria2process.kill() + if state.debug: + print("\nKilled Aria2") + + +def closehelper(): + shutdown_event.set() + kill_aria2server() \ No newline at end of file diff --git a/core/utils/wrappers.py b/core/utils/general/wrappers.py similarity index 100% rename from core/utils/wrappers.py rename to core/utils/general/wrappers.py diff --git a/core/utils/network/download.py b/core/utils/network/download.py index 39b05c8..a24b2cc 100644 --- a/core/utils/network/download.py +++ b/core/utils/network/download.py @@ -3,7 +3,7 @@ from core.utils.data.tracker import get_item_url from core.utils.data.tracker import get_magnet_link from core.network.aria2_wrapper import start_client from core.network.aria2_wrapper import add_magnet -from core.utils.wrappers import run_thread +from core.utils.general.wrappers import run_thread import threading diff --git a/main.py b/main.py index b5eaf1b..71aaf37 100644 --- a/main.py +++ b/main.py @@ -2,19 +2,20 @@ from core.interface.gui import MainWindow from core.utils.data.state import state from core.network.aria2_integration import aria2server from core.network.aria2_integration import send_notification -from core.utils.wrappers import run_thread -import threading +from core.utils.general.shutdown import kill_aria2server, closehelper, shutdown_event +from core.utils.general.wrappers import run_thread from core.utils.config.config import read_config from PySide6 import QtWidgets import qdarktheme import darkdetect +import threading import signal import atexit import sys # Debug Output state.debug = True -shutdown_event = threading.Event() + def run_gui(): app = QtWidgets.QApplication([]) @@ -30,19 +31,8 @@ def run_aria2server(): aria2process = aria2server() return aria2process - -def kill_aria2server(): - if state.aria2process: - state.aria2process.kill() - if state.debug: - print("\nKilled Aria2") - - def keyboardinterrupthandler(signum, frame): - shutdown_event.set() - kill_aria2server() - sys.exit(0) - + closehelper() if __name__ == "__main__": read_config()