Skip to content

Commit

Permalink
refactor: Add back -all commands with deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Aug 7, 2019
1 parent e0ded18 commit 939402f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scripts/templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ Commands:
{% for command in commands %}
- [`{{ command.name }}`](#{{ command.name }}){% endfor %}

**Warning:** commands ending with `-all` are deprecated. Please use their equivalent with the `-a` or `--all` option
(e.g. instead of `pause-all`, use `pause -a`). These commands will be removed in version 0.5.0.

{% for command in commands %}
### `{{ command.name }}`
```
Expand Down
73 changes: 73 additions & 0 deletions src/aria2p/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import argparse
import json
import sys
import warnings

import requests
from loguru import logger
Expand Down Expand Up @@ -62,8 +63,11 @@ def main(args=None):
"add-torrent": subcommand_add_torrent,
"add-metalink": subcommand_add_metalink,
"pause": subcommand_pause,
"pause-all": subcommand_pause_all,
"resume": subcommand_resume,
"resume-all": subcommand_resume_all,
"remove": subcommand_remove,
"remove-all": subcommand_remove_all,
"purge": subcommand_purge,
"autopurge": subcommand_autopurge,
}
Expand All @@ -86,6 +90,12 @@ def check_args(parser, args):
subparser[args.subcommand].error("the following arguments are required: gids or --all")
elif args.do_all and args.gids:
subparser[args.subcommand].error("argument -a/--all: not allowed with arguments gids")
elif args.subcommand.endswith("-all"):
warnings.warn(
f"Subcommand '{args.subcommand}' is deprecated in favor of '{args.subcommand[:-4]} --all'.\n"
f"It will be removed in version 0.5.0, please update your scripts/code.",
DeprecationWarning
)


def get_parser():
Expand Down Expand Up @@ -133,9 +143,12 @@ def subparser(command, text, **kwargs):
subparser("autopurge", "Automatically purge completed/removed/failed downloads.", aliases=["autoclear"])
call_parser = subparser("call", "Call a remote method through the JSON-RPC client.")
pause_parser = subparser("pause", "Pause downloads.")
pause_all_parser = subparser("pause-all", "Pause all downloads.")
purge_parser = subparser("purge", "Purge downloads.", aliases=["clear"])
remove_parser = subparser("remove", "Remove downloads.", aliases=["rm"])
remove_all_parser = subparser("remove-all", "Remove all downloads.")
resume_parser = subparser("resume", "Resume downloads.")
subparser("resume-all", "Resume all downloads.")
subparser("show", "Show the download progression.")

# ========= CALL PARSER ========= #
Expand Down Expand Up @@ -174,6 +187,11 @@ def subparser(command, text, **kwargs):
"-f", "--force", dest="force", action="store_true", help="Pause without contacting servers first."
)

# ========= PAUSE ALL PARSER ========= #
pause_all_parser.add_argument(
"-f", "--force", dest="force", action="store_true", help="Pause without contacting servers first."
)

# ========= RESUME PARSER ========= #
resume_parser.add_argument("gids", nargs="*", help="The GIDs of the downloads to resume.")
resume_parser.add_argument("-a", "--all", action="store_true", dest="do_all", help="Resume all the downloads.")
Expand All @@ -185,6 +203,11 @@ def subparser(command, text, **kwargs):
"-f", "--force", dest="force", action="store_true", help="Remove without contacting servers first."
)

# ========= REMOVE ALL PARSER ========= #
remove_all_parser.add_argument(
"-f", "--force", dest="force", action="store_true", help="Remove without contacting servers first."
)

# ========= PURGE PARSER ========= #
purge_parser.add_argument("gids", nargs="*", help="The GIDs of the downloads to purge.")
purge_parser.add_argument("-a", "--all", action="store_true", dest="do_all", help="Purge all the downloads.")
Expand Down Expand Up @@ -393,6 +416,23 @@ def subcommand_pause(api: API, gids=None, do_all=False, force=False):
return 1


# ============ PAUSE ALL SUBCOMMAND ============ #
def subcommand_pause_all(api: API, force=False):
"""
Pause all subcommand.
Args:
api (API): the API instance to use.
force (bool): force pause or not (see API.pause_all).
Returns:
int: 0 if all success, 1 if one failure.
"""
if api.pause_all(force=force):
return 0
return 1


# ============ RESUME SUBCOMMAND ============ #
def subcommand_resume(api: API, gids=None, do_all=False):
"""
Expand Down Expand Up @@ -421,6 +461,22 @@ def subcommand_resume(api: API, gids=None, do_all=False):
return 1


# ============ RESUME ALL SUBCOMMAND ============ #
def subcommand_resume_all(api: API):
"""
Resume all subcommand.
Args:
api (API): the API instance to use.
Returns:
int: 0 if all success, 1 if one failure.
"""
if api.resume_all():
return 0
return 1


# ============ REMOVE SUBCOMMAND ============ #
def subcommand_remove(api: API, gids=None, do_all=False, force=False):
"""
Expand Down Expand Up @@ -450,6 +506,23 @@ def subcommand_remove(api: API, gids=None, do_all=False, force=False):
return 1


# ============ REMOVE ALL SUBCOMMAND ============ #
def subcommand_remove_all(api: API, force=False):
"""
Remove all subcommand.
Args:
api (API): the API instance to use.
force (bool): force pause or not (see API.remove_all).
Returns:
int: 0 if all success, 1 if one failure.
"""
if api.remove_all(force=force):
return 0
return 1


# ============ PURGE SUBCOMMAND ============ #
def subcommand_purge(api: API, gids=None, do_all=False):
"""
Expand Down

0 comments on commit 939402f

Please sign in to comment.