Skip to content

Commit

Permalink
feat(api): Add option to remove files as well when removing downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Oct 12, 2019
1 parent 2b2c021 commit 981dcc0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
27 changes: 26 additions & 1 deletion src/aria2p/api.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/aria2p/downloads.py
Expand Up @@ -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).
Expand All @@ -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
Expand Down

0 comments on commit 981dcc0

Please sign in to comment.