network_handler.py: Match formatting and check download path space

This commit is contained in:
Mykola Grymalyuk
2023-02-06 08:54:55 -07:00
parent 6081ec0d73
commit ace93b8a3e

View File

@@ -139,10 +139,10 @@ class DownloadObject:
"""
self.status = DownloadStatus.DOWNLOADING
logging.info(f"Starting download: {self.filename}")
logging.info(f"- Starting download: {self.filename}")
if spawn_thread:
if self.active_thread:
logging.error("Download already in progress")
logging.error("- Download already in progress")
return
self.should_checksum = verify_checksum
self.active_thread = threading.Thread(target=self._download, args=(display_progress,))
@@ -201,8 +201,8 @@ class DownloadObject:
else:
raise Exception("Content-Length missing from headers")
except Exception as e:
logging.error(f"Error determining file size {self.url}: {str(e)}")
logging.error("Assuming file size is 0")
logging.error(f"- Error determining file size {self.url}: {str(e)}")
logging.error("- Assuming file size is 0")
self.total_file_size = 0.0
@@ -229,17 +229,17 @@ class DownloadObject:
try:
if Path(path).exists():
logging.info(f"Deleting existing file: {path}")
logging.info(f"- Deleting existing file: {path}")
Path(path).unlink()
return True
if not Path(path).parent.exists():
logging.info(f"Creating directory: {Path(path).parent}")
logging.info(f"- Creating directory: {Path(path).parent}")
Path(path).parent.mkdir(parents=True, exist_ok=True)
available_space = utilities.get_free_space()
available_space = utilities.get_free_space(Path(path).parent)
if self.total_file_size > available_space:
msg = f"Not enough free space to download {self.filename}, need {utilities.human_fmt(self.total_file_size)}, have {utilities.human_fmt(available_space)}"
msg = f"- Not enough free space to download {self.filename}, need {utilities.human_fmt(self.total_file_size)}, have {utilities.human_fmt(available_space)}"
logging.error(msg)
raise Exception(msg)
@@ -247,7 +247,7 @@ class DownloadObject:
self.error = True
self.error_msg = str(e)
self.status = DownloadStatus.ERROR
logging.error(f"Error preparing working directory {path}: {self.error_msg}")
logging.error(f"- Error preparing working directory {path}: {self.error_msg}")
return False
return True
@@ -286,21 +286,21 @@ class DownloadObject:
if display_progress and i % 100:
# Don't use logging here, as we'll be spamming the log file
if self.total_file_size == 0.0:
print(f"Downloaded {utilities.human_fmt(self.downloaded_file_size)} of {self.filename}")
print(f"- Downloaded {utilities.human_fmt(self.downloaded_file_size)} of {self.filename}")
else:
print(f"Downloaded {self.get_percent():.2f}% of {self.filename} ({utilities.human_fmt(self.get_speed())}/s) ({self.get_time_remaining():.2f} seconds remaining)")
print(f"- Downloaded {self.get_percent():.2f}% of {self.filename} ({utilities.human_fmt(self.get_speed())}/s) ({self.get_time_remaining():.2f} seconds remaining)")
self.download_complete = True
logging.info(f"Download complete: {self.filename}")
logging.info("Stats:")
logging.info(f" Downloaded size: {utilities.human_fmt(self.downloaded_file_size)}")
logging.info(f" Time elapsed: {(time.time() - self.start_time):.2f} seconds")
logging.info(f" Speed: {utilities.human_fmt(self.downloaded_file_size / (time.time() - self.start_time))}/s")
logging.info(f" Location: {self.filepath}")
logging.info(f"- Download complete: {self.filename}")
logging.info("- Stats:")
logging.info(f"- Downloaded size: {utilities.human_fmt(self.downloaded_file_size)}")
logging.info(f"- Time elapsed: {(time.time() - self.start_time):.2f} seconds")
logging.info(f"- Speed: {utilities.human_fmt(self.downloaded_file_size / (time.time() - self.start_time))}/s")
logging.info(f"- Location: {self.filepath}")
except Exception as e:
self.error = True
self.error_msg = str(e)
self.status = DownloadStatus.ERROR
logging.error(f"Error downloading {self.url}: {self.error_msg}")
logging.error(f"- Error downloading {self.url}: {self.error_msg}")
self.status = DownloadStatus.COMPLETE
utilities.enable_sleep_after_running()
@@ -315,7 +315,7 @@ class DownloadObject:
"""
if self.total_file_size == 0.0:
logging.error("File size is 0, cannot calculate percent")
logging.error("- File size is 0, cannot calculate percent")
return -1
return self.downloaded_file_size / self.total_file_size * 100
@@ -340,7 +340,7 @@ class DownloadObject:
"""
if self.total_file_size == 0.0:
logging.error("File size is 0, cannot calculate time remaining")
logging.error("- File size is 0, cannot calculate time remaining")
return -1
return (self.total_file_size - self.downloaded_file_size) / self.get_speed()