From 68e7d7cb3c7067446b551b2c3e7e7d9581106f4d Mon Sep 17 00:00:00 2001 From: Hellow Date: Tue, 4 Apr 2023 21:06:33 +0200 Subject: [PATCH] modified the str method of download results, such that it also prints errors now and then --- .../pages/support_classes/download_result.py | 17 +++++++++++++++-- src/music_kraken/utils/shared.py | 5 +++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/music_kraken/pages/support_classes/download_result.py b/src/music_kraken/pages/support_classes/download_result.py index 423c916..92889de 100644 --- a/src/music_kraken/pages/support_classes/download_result.py +++ b/src/music_kraken/pages/support_classes/download_result.py @@ -1,6 +1,8 @@ from dataclasses import dataclass, field from typing import List +from ...utils.shared import SHOW_DOWNLOAD_ERRORS_THRESHOLD + @dataclass class DownloadResult: @@ -18,6 +20,10 @@ class DownloadResult: def success_percentage(self) -> float: return self.success / self.total + @property + def failure_percentage(self) -> float: + return self.fail / self.total + @property def fatal_error(self) -> bool: return self.error_message is not None @@ -32,7 +38,14 @@ class DownloadResult: self.fail += other.fail self._error_message_list.extend(other._error_message_list) - def __repr__(self): + def __str__(self): if self.fatal_error: 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) diff --git a/src/music_kraken/utils/shared.py b/src/music_kraken/utils/shared.py index 26ca004..fa02e47 100644 --- a/src/music_kraken/utils/shared.py +++ b/src/music_kraken/utils/shared.py @@ -109,3 +109,8 @@ DEFAULT_VALUES = { # size of the chunks that are streamed 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