mirror of
https://github.com/dortania/OpenCore-Legacy-Patcher.git
synced 2026-06-18 21:30:00 +10:00
network_handler.py: Set file path in Object init
This commit is contained in:
@@ -1784,8 +1784,8 @@ class wx_python_gui:
|
|||||||
wx.GetApp().Yield()
|
wx.GetApp().Yield()
|
||||||
|
|
||||||
|
|
||||||
ia_download = network_handler.DownloadObject(app_dict['Link'])
|
ia_download = network_handler.DownloadObject(app_dict['Link'], self.constants.payload_path / "InstallAssistant.pkg")
|
||||||
ia_download.download(self.constants.payload_path / "InstallAssistant.pkg")
|
ia_download.download()
|
||||||
|
|
||||||
while ia_download.is_active():
|
while ia_download.is_active():
|
||||||
wx.GetApp().Yield()
|
wx.GetApp().Yield()
|
||||||
@@ -2280,8 +2280,8 @@ class wx_python_gui:
|
|||||||
path = self.constants.installer_pkg_path
|
path = self.constants.installer_pkg_path
|
||||||
|
|
||||||
|
|
||||||
autopkg_download = network_handler.DownloadObject(link)
|
autopkg_download = network_handler.DownloadObject(link, path)
|
||||||
autopkg_download.download(path, display_progress=False)
|
autopkg_download.download()
|
||||||
|
|
||||||
while autopkg_download.is_active():
|
while autopkg_download.is_active():
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|||||||
@@ -47,8 +47,8 @@ class DownloadObject:
|
|||||||
Object for downloading files from the network
|
Object for downloading files from the network
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
>>> download_object = DownloadObject(url)
|
>>> download_object = DownloadObject(url, path)
|
||||||
>>> download_object.download(path, display_progress=True)
|
>>> download_object.download(display_progress=True)
|
||||||
|
|
||||||
>>> if download_object.is_active():
|
>>> if download_object.is_active():
|
||||||
>>> print(download_object.get_percent())
|
>>> print(download_object.get_percent())
|
||||||
@@ -60,12 +60,14 @@ class DownloadObject:
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, url:str):
|
def __init__(self, url: str, path: str):
|
||||||
self.url: str = url
|
self.url: str = url
|
||||||
self.status: str = "Inactive"
|
self.status: str = "Inactive"
|
||||||
self.error_msg: str = ""
|
self.error_msg: str = ""
|
||||||
self.filename: str = self._get_filename()
|
self.filename: str = self._get_filename()
|
||||||
|
|
||||||
|
self.filepath: Path = Path(path)
|
||||||
|
|
||||||
self.total_file_size: float = 0.0
|
self.total_file_size: float = 0.0
|
||||||
self.downloaded_file_size: float = 0.0
|
self.downloaded_file_size: float = 0.0
|
||||||
self.start_time: float = time.time()
|
self.start_time: float = time.time()
|
||||||
@@ -85,7 +87,7 @@ class DownloadObject:
|
|||||||
self.stop()
|
self.stop()
|
||||||
|
|
||||||
|
|
||||||
def download(self, path: str, display_progress: bool =False):
|
def download(self, display_progress: bool = False, spawn_thread: bool = True):
|
||||||
"""
|
"""
|
||||||
Download the file
|
Download the file
|
||||||
|
|
||||||
@@ -93,17 +95,20 @@ class DownloadObject:
|
|||||||
Note sleep is disabled while the download is active
|
Note sleep is disabled while the download is active
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
path (str): Path to save the file to
|
|
||||||
display_progress (bool): Display progress in console
|
display_progress (bool): Display progress in console
|
||||||
|
spawn_thread (bool): Spawn a thread to download the file, otherwise download in the current thread
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if self.active_thread:
|
|
||||||
return
|
|
||||||
self.status = "Downloading"
|
self.status = "Downloading"
|
||||||
logging.info(f"Starting download: {self.filename}")
|
logging.info(f"Starting download: {self.filename}")
|
||||||
self.active_thread = threading.Thread(target=self._download, args=(path,display_progress,))
|
if spawn_thread:
|
||||||
self.active_thread.start()
|
if self.active_thread:
|
||||||
|
logging.error("Download already in progress")
|
||||||
|
return
|
||||||
|
self.active_thread = threading.Thread(target=self._download, args=(display_progress,))
|
||||||
|
self.active_thread.start()
|
||||||
|
else:
|
||||||
|
self._download(display_progress)
|
||||||
|
|
||||||
|
|
||||||
def _get_filename(self):
|
def _get_filename(self):
|
||||||
@@ -136,7 +141,7 @@ class DownloadObject:
|
|||||||
self.total_file_size = 0.0
|
self.total_file_size = 0.0
|
||||||
|
|
||||||
|
|
||||||
def _prepare_working_directory(self, path:str):
|
def _prepare_working_directory(self, path: Path):
|
||||||
"""
|
"""
|
||||||
Delete the file if it already exists
|
Delete the file if it already exists
|
||||||
|
|
||||||
@@ -165,14 +170,14 @@ class DownloadObject:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def _download(self, path, display_progress=False):
|
def _download(self, display_progress: bool = False):
|
||||||
"""
|
"""
|
||||||
Download the file
|
Download the file
|
||||||
|
|
||||||
Parameters:
|
Libraries should invoke download() instead of this method
|
||||||
path (str): Path to save the file to
|
|
||||||
display_progress (bool): Display progress in console
|
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
display_progress (bool): Display progress in console
|
||||||
"""
|
"""
|
||||||
|
|
||||||
utilities.disable_sleep_while_running()
|
utilities.disable_sleep_while_running()
|
||||||
@@ -181,12 +186,12 @@ class DownloadObject:
|
|||||||
if not self.has_network:
|
if not self.has_network:
|
||||||
raise Exception("No network connection")
|
raise Exception("No network connection")
|
||||||
|
|
||||||
if self._prepare_working_directory(path) is False:
|
if self._prepare_working_directory(self.filepath) is False:
|
||||||
raise Exception(self.error_msg)
|
raise Exception(self.error_msg)
|
||||||
|
|
||||||
response = SESSION.get(self.url, stream=True)
|
response = SESSION.get(self.url, stream=True)
|
||||||
|
|
||||||
with open(path, 'wb') as file:
|
with open(self.filepath, 'wb') as file:
|
||||||
for i, chunk in enumerate(response.iter_content(1024 * 1024 * 4)):
|
for i, chunk in enumerate(response.iter_content(1024 * 1024 * 4)):
|
||||||
if self.should_stop:
|
if self.should_stop:
|
||||||
raise Exception("Download stopped")
|
raise Exception("Download stopped")
|
||||||
|
|||||||
Reference in New Issue
Block a user