-
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy path__main__.py
More file actions
78 lines (65 loc) · 2.1 KB
/
__main__.py
File metadata and controls
78 lines (65 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import argparse
import sys
from importlib.metadata import entry_points
from typing import Dict
from taskiq import __version__
from taskiq.abc.cmd import TaskiqCMD
def main() -> None: # pragma: no cover
"""
Main entrypoint of the taskiq.
This function collects all python entrypoints
and assembles a final argument parser.
All found entrypoints are used as subcommands.
All arguments are passed to them as it was a normal
call.
"""
found_plugins = len(entry_points().select(group="taskiq_cli"))
parser = argparse.ArgumentParser(
description=f"""
CLI for taskiq. Distributed task queue.
This is a meta CLI. It searches for installed plugins
using python entrypoints
and passes all arguments to them.
We found {found_plugins} installed plugins.
""",
)
parser.add_argument(
"-V",
"--version",
dest="version",
action="store_true",
help="print current taskiq version and exit",
)
subcommands: Dict[str, TaskiqCMD] = {}
subparsers = parser.add_subparsers(
title="Available subcommands",
metavar="",
dest="subcommand",
)
for entrypoint in entry_points().select(group="taskiq_cli"):
try:
cmd_class = entrypoint.load()
except ImportError as exc:
print(f"Could not load {entrypoint.value}. Cause: {exc}") # noqa: T201
continue
if issubclass(cmd_class, TaskiqCMD):
subparsers.add_parser(
entrypoint.name,
help=cmd_class.short_help,
add_help=False,
)
subcommands[entrypoint.name] = cmd_class()
args, _ = parser.parse_known_args()
if args.version:
print(__version__) # noqa: T201
return
if args.subcommand is None:
parser.print_help()
return
command = subcommands[args.subcommand]
sys.argv.pop(0)
status = command.exec(sys.argv[1:])
if status is not None:
exit(status) # noqa: PLR1722
if __name__ == "__main__": # pragma: no cover
main()