mirror of
https://github.com/KeksPirates/SoftwareManager.git
synced 2026-08-03 17:39:42 +02:00
refactor: consolidate aria2 process management into state and remove global variables
This commit is contained in:
@@ -37,13 +37,6 @@ def state_debug(setting):
|
||||
state.debug = False
|
||||
|
||||
|
||||
def pass_aria(aria):
|
||||
global aria2process
|
||||
aria2process = aria
|
||||
|
||||
|
||||
|
||||
|
||||
def create_tab(title, searchbar, software_list, tabs):
|
||||
tab = QWidget()
|
||||
layout = QVBoxLayout()
|
||||
@@ -162,7 +155,7 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
|
||||
thread_box = QSpinBox()
|
||||
thread_box.setMinimum(1)
|
||||
thread_box.setMaximum(16)
|
||||
thread_box.setValue(4)
|
||||
thread_box.setValue(state.aria2_threads)
|
||||
|
||||
|
||||
# container for tight space
|
||||
@@ -202,7 +195,7 @@ class MainWindow(QtWidgets.QMainWindow, QWidget):
|
||||
|
||||
save_btn = QPushButton("Save")
|
||||
cancel_btn = QPushButton("Cancel")
|
||||
save_btn.clicked.connect(lambda: save_settings(thread_box.value(), close_settings, text_edit.toPlainText(), aria2process))
|
||||
save_btn.clicked.connect(lambda: save_settings(thread_box.value(), close_settings, text_edit.toPlainText()))
|
||||
|
||||
|
||||
cancel_btn.clicked.connect(dialog.reject)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import aria2p
|
||||
import os
|
||||
import subprocess
|
||||
from core.utils.state import state
|
||||
|
||||
def run_aria2p():
|
||||
global aria2
|
||||
@@ -15,11 +16,7 @@ def run_aria2p():
|
||||
|
||||
return aria2
|
||||
|
||||
t = 4 # default value
|
||||
|
||||
def set_threads(threads):
|
||||
global t
|
||||
t = threads
|
||||
|
||||
|
||||
def aria2server():
|
||||
@@ -32,8 +29,8 @@ def aria2server():
|
||||
"--rpc-listen-all",
|
||||
"--rpc-listen-port=6800",
|
||||
f"--dir={downloads_dir}",
|
||||
"-x", str(t),
|
||||
"-s", str(t),
|
||||
"-x", str(state.aria2_threads),
|
||||
"-s", str(state.aria2_threads),
|
||||
]
|
||||
|
||||
|
||||
|
||||
+15
-16
@@ -1,20 +1,19 @@
|
||||
from core.network.aria2_integration import set_threads
|
||||
from core.utils.state import state
|
||||
|
||||
|
||||
def restart_aria2c(aria2process):
|
||||
import main # had to do this because of circle import :(
|
||||
import signal
|
||||
import atexit
|
||||
main.kill_aria2server(aria2process)
|
||||
aria2process.wait()
|
||||
aria2process = main.run_aria2server()
|
||||
signal.signal(signal.SIGINT, main.keyboardinterrupthandler)
|
||||
atexit.unregister(main.kill_aria2server)
|
||||
atexit.register(main.kill_aria2server, aria2process)
|
||||
def restart_aria2c():
|
||||
import main # had to do this because of circle import :(
|
||||
import signal
|
||||
import atexit
|
||||
main.kill_aria2server()
|
||||
state.aria2process.wait()
|
||||
state.aria2process = main.run_aria2server()
|
||||
signal.signal(signal.SIGINT, main.keyboardinterrupthandler)
|
||||
atexit.unregister(main.kill_aria2server)
|
||||
atexit.register(main.kill_aria2server)
|
||||
|
||||
def save_settings(thread_count, close, apiurl, aria2process):
|
||||
global api_url
|
||||
set_threads(thread_count)
|
||||
api_url = apiurl
|
||||
restart_aria2c(aria2process)
|
||||
def save_settings(thread_count, close, apiurl):
|
||||
state.aria2_threads = thread_count
|
||||
state.api_url = apiurl
|
||||
restart_aria2c()
|
||||
close()
|
||||
@@ -1,4 +1,5 @@
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Any
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -6,6 +7,8 @@ class AppState:
|
||||
debug: bool = False
|
||||
tracker: str = "rutracker"
|
||||
api_url: str = "https://api.michijackson.xyz"
|
||||
aria2process: Optional[Any] = None
|
||||
aria2_threads: int = 4
|
||||
|
||||
|
||||
state = AppState()
|
||||
@@ -1,6 +1,6 @@
|
||||
from core.interface.gui import MainWindow
|
||||
from core.interface.gui import state_debug
|
||||
from core.interface.gui import pass_aria
|
||||
from core.utils.state import state
|
||||
from core.network.aria2_integration import aria2server
|
||||
from PySide6 import QtWidgets
|
||||
import qdarktheme
|
||||
@@ -28,28 +28,25 @@ def run_aria2server():
|
||||
return aria2process
|
||||
|
||||
|
||||
def kill_aria2server(aria2process):
|
||||
if aria2process:
|
||||
aria2process.kill()
|
||||
def kill_aria2server():
|
||||
if state.aria2process:
|
||||
state.aria2process.kill()
|
||||
if debug:
|
||||
print("\nKilled Aria2")
|
||||
|
||||
|
||||
def keyboardinterrupthandler(signum, frame):
|
||||
global aria2process
|
||||
kill_aria2server(aria2process)
|
||||
kill_aria2server()
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if debug:
|
||||
print("Starting Aria2 Server")
|
||||
aria2process = run_aria2server()
|
||||
state.aria2process = run_aria2server()
|
||||
signal.signal(signal.SIGINT, keyboardinterrupthandler)
|
||||
atexit.register(kill_aria2server, aria2process)
|
||||
atexit.register(kill_aria2server)
|
||||
if debug:
|
||||
print("Launching GUI")
|
||||
|
||||
pass_aria(aria2process)
|
||||
run_gui()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user