From 981dcc015f8baef5b3d2f0230b27376f482442fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sat, 12 Oct 2019 17:00:40 +0200 Subject: [PATCH] feat(api): Add option to remove files as well when removing downloads --- src/aria2p/api.py | 27 ++++++++++++++++++++++++++- src/aria2p/downloads.py | 4 ++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/aria2p/api.py b/src/aria2p/api.py index d5ecdc1..56abee2 100644 --- a/src/aria2p/api.py +++ b/src/aria2p/api.py @@ -313,13 +313,14 @@ def move_to_bottom(self, download): """ return self.client.change_position(download.gid, 0, "POS_END") - def remove(self, downloads, force=False): + def remove(self, downloads, force=False, files=False): """ Remove the given downloads from the list. Args: downloads (list of :class:`~aria2p.downloads.Download`): the list of downloads to remove. force (bool): whether to force the removal or not. + files (bool): whether to remove downloads files as well. Returns: list of bool: Success or failure of the operation for each given download. @@ -353,6 +354,8 @@ def remove(self, downloads, force=False): except ClientException as error2: logger.debug(f"Failed to remove download result {removed_gid}") logger.opt(exception=True).trace(error2) + if files: + self.remove_files([download], force=force) return result @@ -564,6 +567,28 @@ def get_stats(self): """ return Stats(self.client.get_global_stat()) + @staticmethod + def remove_files(downloads, force=False): + """ + Remove downloaded files. + + Args: + downloads (list of :class:`~aria2p.downloads.Download`): the list of downloads for which to remove files. + force (bool): whether to remove files even if download is not complete. + + Returns: + list of bool: Success or failure of the operation for each given download. + """ + results = [] + for download in downloads: + if download.is_complete or force: + for path in download.root_files_paths: + shutil.rmtree(str(path)) + results.append(True) + else: + results.append(False) + return results + @staticmethod def move_files(downloads, to_directory, force=False): """ diff --git a/src/aria2p/downloads.py b/src/aria2p/downloads.py index c87bd0a..de53ed6 100644 --- a/src/aria2p/downloads.py +++ b/src/aria2p/downloads.py @@ -765,7 +765,7 @@ def move_to_bottom(self): """ return self.api.move_to_bottom(self) - def remove(self, force=False): + def remove(self, force=False, files=False): """ Remove the download from the queue (even if active). @@ -775,7 +775,7 @@ def remove(self, force=False): Raises: ClientException: when removal failed. """ - result = self.api.remove([self], force=force)[0] + result = self.api.remove([self], force=force, files=files)[0] if not result: raise result return result