6
6
import requests
7
7
8
8
from PySide6 .QtCore import QObject , QCoreApplication , Signal , Property
9
+ from PySide6 .QtWidgets import QMessageBox
10
+
9
11
10
12
from pupgui2 .datastructures import Launcher
13
+ from pupgui2 .networkutil import download_file
11
14
from pupgui2 .util import extract_tar , extract_tar_zst , get_launcher_from_installdir
12
15
from pupgui2 .util import build_headers_with_authorization , fetch_project_release_data , fetch_project_releases
13
16
@@ -24,6 +27,7 @@ class CtInstaller(QObject):
24
27
25
28
p_download_progress_percent = 0
26
29
download_progress_percent = Signal (int )
30
+ message_box_message = Signal ((str , str , QMessageBox .Icon ))
27
31
28
32
def __init__ (self , main_window = None ):
29
33
super (CtInstaller , self ).__init__ ()
@@ -48,35 +52,29 @@ def __set_download_progress_percent(self, value : int):
48
52
self .p_download_progress_percent = value
49
53
self .download_progress_percent .emit (value )
50
54
51
- def __download (self , url , destination ) :
55
+ def __download (self , url : str , destination : str ) -> bool :
52
56
"""
53
57
Download files from url to destination
54
58
Return Type: bool
55
59
"""
60
+
56
61
try :
57
- file = self .rs .get (url , stream = True )
58
- except OSError :
59
- return False
62
+ return download_file (
63
+ url = url ,
64
+ destination = os .path .expanduser (destination ),
65
+ progress_callback = self .__set_download_progress_percent ,
66
+ download_cancelled = self .download_canceled ,
67
+ )
68
+ except Exception as e :
69
+ print (f"Failed to download tool { CT_NAME } - Reason: { e } " )
70
+
71
+ self .message_box_message .emit (
72
+ self .tr ("Download Error!" ),
73
+ self .tr ("Failed to download tool '{CT_NAME}'!\n \n Reason: {EXCEPTION}" .format (CT_NAME = CT_NAME , EXCEPTION = e )),
74
+ QMessageBox .Icon .Warning
75
+ )
60
76
61
- self .__set_download_progress_percent (1 ) # 1 download started
62
- f_size = int (file .headers .get ('content-length' ))
63
- c_count = int (f_size / self .BUFFER_SIZE )
64
- c_current = 1
65
- destination = os .path .expanduser (destination )
66
- os .makedirs (os .path .dirname (destination ), exist_ok = True )
67
- with open (destination , 'wb' ) as dest :
68
- for chunk in file .iter_content (chunk_size = self .BUFFER_SIZE ):
69
- if self .download_canceled :
70
- self .download_canceled = False
71
- self .__set_download_progress_percent (- 2 ) # -2 download canceled
72
- return False
73
- if chunk :
74
- dest .write (chunk )
75
- dest .flush ()
76
- self .__set_download_progress_percent (int (min (c_current / c_count * 98.0 , 98.0 ))) # 1-98, 100 after extract
77
- c_current += 1
78
- self .__set_download_progress_percent (99 ) # 99 download complete
79
- return True
77
+ return False
80
78
81
79
def __fetch_github_data (self , tag ):
82
80
"""
0 commit comments