forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
84 lines (66 loc) · 2.04 KB
/
__main__.py
File metadata and controls
84 lines (66 loc) · 2.04 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
79
80
81
82
83
84
from __future__ import absolute_import
import argparse
import sys
from . import pytest, report
from .errors import UnsupportedToolError, UnsupportedCommandError
# Set this to True to pretty-print the output.
DEBUG=False
#DEBUG=True
TOOLS = {
'pytest': {
'_add_subparser': pytest.add_cli_subparser,
'discover': pytest.discover,
},
}
REPORTERS = {
'discover': report.report_discovered,
}
def parse_args(
argv=sys.argv[1:],
prog=sys.argv[0],
):
"""
Return the subcommand & tool to run, along with its args.
This defines the standard CLI for the different testing frameworks.
"""
parser = argparse.ArgumentParser(
description='Run Python testing operations.',
prog=prog,
)
cmdsubs = parser.add_subparsers(dest='cmd')
# Add "run" and "debug" subcommands when ready.
for cmdname in ['discover']:
sub = cmdsubs.add_parser(cmdname)
subsubs = sub.add_subparsers(dest='tool')
for toolname in sorted(TOOLS):
try:
add_subparser = TOOLS[toolname]['_add_subparser']
except KeyError:
continue
add_subparser(cmdname, toolname, subsubs)
# Parse the args!
args, toolargs = parser.parse_known_args(argv)
ns = vars(args)
cmd = ns.pop('cmd')
if not cmd:
parser.error('missing command')
tool = ns.pop('tool')
if not tool:
parser.error('missing tool')
return tool, cmd, ns, toolargs
def main(toolname, cmdname, subargs, toolargs,
_tools=TOOLS, _reporters=REPORTERS):
try:
tool = _tools[toolname]
except KeyError:
raise UnsupportedToolError(toolname)
try:
run = tool[cmdname]
report_result = _reporters[cmdname]
except KeyError:
raise UnsupportedCommandError(cmdname)
result = run(toolargs, **subargs)
report_result(result, debug=DEBUG)
if __name__ == '__main__':
tool, cmd, subargs, toolargs = parse_args()
main(tool, cmd, subargs, toolargs)