From 4e1f7a8a77cd274f88cb62c0f2d9d18e549f034e Mon Sep 17 00:00:00 2001 From: lzzy12 Date: Wed, 25 Dec 2019 16:17:02 +0530 Subject: [PATCH] Small optimisations all around Signed-off-by: lzzy12 --- bot/helper/ext_utils/bot_utils.py | 23 +-------------- .../status_utils/aria_download_status.py | 28 +++++++++---------- .../{download_status.py => status.py} | 6 +++- .../mirror_utils/status_utils/tar_status.py | 9 ++++-- .../status_utils/upload_status.py | 7 +++-- .../mirror_utils/upload_utils/gdriveTools.py | 2 +- 6 files changed, 33 insertions(+), 42 deletions(-) rename bot/helper/mirror_utils/status_utils/{download_status.py => status.py} (84%) diff --git a/bot/helper/ext_utils/bot_utils.py b/bot/helper/ext_utils/bot_utils.py index cce79af..ee4e03b 100644 --- a/bot/helper/ext_utils/bot_utils.py +++ b/bot/helper/ext_utils/bot_utils.py @@ -55,21 +55,8 @@ def get_readable_file_size(size_in_bytes) -> str: return 'File too large' -def get_download(message_id): - with download_dict_lock: - return download_dict[message_id].download() - - -def get_download_status_list(): - with download_dict_lock: - return list(download_dict.values()) - - def get_progress_bar_string(status): - if status.status() == MirrorStatus.STATUS_UPLOADING: - completed = status.obj.uploaded_bytes / 8 - else: - completed = status.download().completed_length / 8 + completed = status.processed_bytes() / 8 total = status.size_raw() / 8 if total == 0: p = 0 @@ -94,14 +81,6 @@ def get_download_index(_list, gid): index += 1 -def get_download_str(): - result = "" - with download_dict_lock: - for status in list(download_dict.values()): - result += (status.progress() + status.speed() + status.status()) - return result - - def get_readable_message(): with download_dict_lock: msg = "" diff --git a/bot/helper/mirror_utils/status_utils/aria_download_status.py b/bot/helper/mirror_utils/status_utils/aria_download_status.py index ba0948e..bfe81f1 100644 --- a/bot/helper/mirror_utils/status_utils/aria_download_status.py +++ b/bot/helper/mirror_utils/status_utils/aria_download_status.py @@ -1,13 +1,13 @@ from bot import aria2, DOWNLOAD_DIR -from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus, get_readable_time -from .download_status import DownloadStatus +from bot.helper.ext_utils.bot_utils import MirrorStatus +from .status import Status def get_download(gid): return aria2.get_download(gid) -class AriaDownloadStatus(DownloadStatus): +class AriaDownloadStatus(Status): def __init__(self, gid, listener): super().__init__() @@ -38,31 +38,31 @@ class AriaDownloadStatus(DownloadStatus): """ return self.download().total_length + def processed_bytes(self): + return self.download().completed_length + def speed(self): - self.__update() - return self.__download.download_speed_string() + return self.download().download_speed_string() def name(self): - self.__update() - return self.__download.name + return self.download().name def path(self): return f"{DOWNLOAD_DIR}{self.__uid}" def size(self): - return self.__download.total_length_string() + return self.download().total_length_string() def eta(self): - self.__update() - return self.__download.eta_string() + return self.download().eta_string() def status(self): - self.__update() - if self.download().is_waiting: + download = self.download() + if download.is_waiting: status = MirrorStatus.STATUS_WAITING - elif self.download().is_paused: + elif download.is_paused: status = MirrorStatus.STATUS_CANCELLED - elif self.__download.has_failed: + elif download.has_failed: status = MirrorStatus.STATUS_FAILED else: status = MirrorStatus.STATUS_DOWNLOADING diff --git a/bot/helper/mirror_utils/status_utils/download_status.py b/bot/helper/mirror_utils/status_utils/status.py similarity index 84% rename from bot/helper/mirror_utils/status_utils/download_status.py rename to bot/helper/mirror_utils/status_utils/status.py index 4ebc214..8e42d13 100644 --- a/bot/helper/mirror_utils/status_utils/download_status.py +++ b/bot/helper/mirror_utils/status_utils/status.py @@ -1,7 +1,7 @@ # Generic status class. All other status classes must inherit this class -class DownloadStatus: +class Status: def progress(self): """ @@ -34,3 +34,7 @@ class DownloadStatus: """:return String describing what is the object of this class will be tracking (upload/download/something else) """ raise NotImplementedError + + def processed_bytes(self): + """:return The size of file that has been processed (downloaded/uploaded/archived)""" + raise NotImplementedError diff --git a/bot/helper/mirror_utils/status_utils/tar_status.py b/bot/helper/mirror_utils/status_utils/tar_status.py index 5a21c3a..efc0895 100644 --- a/bot/helper/mirror_utils/status_utils/tar_status.py +++ b/bot/helper/mirror_utils/status_utils/tar_status.py @@ -1,13 +1,16 @@ -from .download_status import DownloadStatus +from .status import Status from bot.helper.ext_utils.bot_utils import get_readable_file_size, MirrorStatus -class TarStatus(DownloadStatus): +class TarStatus(Status): def __init__(self, name, path, size): self.__name = name self.__path = path self.__size = size + # The progress of Tar function cannot be tracked. So we just return dummy values. + # If this is possible in future,we should implement it + def progress(self): return '0' @@ -29,3 +32,5 @@ class TarStatus(DownloadStatus): def status(self): return MirrorStatus.STATUS_ARCHIVING + def processed_bytes(self): + return 0 diff --git a/bot/helper/mirror_utils/status_utils/upload_status.py b/bot/helper/mirror_utils/status_utils/upload_status.py index df96a80..a7e7c60 100644 --- a/bot/helper/mirror_utils/status_utils/upload_status.py +++ b/bot/helper/mirror_utils/status_utils/upload_status.py @@ -1,9 +1,9 @@ -from .download_status import DownloadStatus +from .status import Status from bot.helper.ext_utils.bot_utils import MirrorStatus, get_readable_file_size, get_readable_time from bot import DOWNLOAD_DIR -class UploadStatus(DownloadStatus): +class UploadStatus(Status): def __init__(self, obj, size, uid): self.obj = obj self.__size = size @@ -12,6 +12,9 @@ class UploadStatus(DownloadStatus): def path(self): return f"{DOWNLOAD_DIR}{self.uid}" + def processed_bytes(self): + return self.obj.uploaded_bytes + def size_raw(self): return self.__size diff --git a/bot/helper/mirror_utils/upload_utils/gdriveTools.py b/bot/helper/mirror_utils/upload_utils/gdriveTools.py index 7125235..ccf4c3e 100644 --- a/bot/helper/mirror_utils/upload_utils/gdriveTools.py +++ b/bot/helper/mirror_utils/upload_utils/gdriveTools.py @@ -242,7 +242,7 @@ class GoogleDriveHelper: f" (folder)" if INDEX_URL is not None: url = f'{INDEX_URL}/{file.get("name")}/' - msg += f'| Index URL' + msg += f' | Index URL' else: msg += f"⁍ {file.get('name')} ({get_readable_file_size(int(file.get('size')))})"