modified the str method of download results, such that it also prints errors now and then

This commit is contained in:
Hellow 2023-04-04 21:06:33 +02:00
parent d9d59f36a0
commit 68e7d7cb3c
2 changed files with 20 additions and 2 deletions

View File

@ -1,6 +1,8 @@
from dataclasses import dataclass, field from dataclasses import dataclass, field
from typing import List from typing import List
from ...utils.shared import SHOW_DOWNLOAD_ERRORS_THRESHOLD
@dataclass @dataclass
class DownloadResult: class DownloadResult:
@ -18,6 +20,10 @@ class DownloadResult:
def success_percentage(self) -> float: def success_percentage(self) -> float:
return self.success / self.total return self.success / self.total
@property
def failure_percentage(self) -> float:
return self.fail / self.total
@property @property
def fatal_error(self) -> bool: def fatal_error(self) -> bool:
return self.error_message is not None return self.error_message is not None
@ -32,7 +38,14 @@ class DownloadResult:
self.fail += other.fail self.fail += other.fail
self._error_message_list.extend(other._error_message_list) self._error_message_list.extend(other._error_message_list)
def __repr__(self): def __str__(self):
if self.fatal_error: if self.fatal_error:
return self.error_message return self.error_message
return f"({int(self.success_percentage*100)}%) {self.fail} from {self.total} downloads failed." head = f"({int(self.success_percentage*100)}%) {self.fail} from {self.total} downloads failed."
if self.failure_percentage <= SHOW_DOWNLOAD_ERRORS_THRESHOLD:
return head
_lines = [head]
_lines.extend(self._error_message_list)
return "\n".join(_lines)

View File

@ -109,3 +109,8 @@ DEFAULT_VALUES = {
# size of the chunks that are streamed # size of the chunks that are streamed
CHUNK_SIZE = 1024 CHUNK_SIZE = 1024
# this is a percentage describing the percentage of failed downloads,
# relative to the total downloads.
# If the percentage goes over this threshold DownloadResult returns the download errors
# in the __str__ method
SHOW_DOWNLOAD_ERRORS_THRESHOLD = 0.3