-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdump_command_args.py
129 lines (115 loc) · 3.91 KB
/
dump_command_args.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# python3.7
"""Dumps available arguments of all commands (configurations).
This file parses the arguments of all commands provided in `configs/` and dump
the results as a json file. Each parsed argument includes the name, argument
type, default value, and the help message (description). The dumped file looks
like
{
"command_1": {
"type": "object",
"properties": {
"arg_group_1": {
"type": "object",
"properties": {
"arg_1": {
"type": # int / float / bool / str / json-string /
# index-string
"is_recommended": # true / false
"default":
"description":
},
"arg_2": {
"type":
"is_recommended":
"default":
"description":
}
}
},
"arg_group_2": {
"type": "object",
"properties": {
"arg_3": {
"type":
"is_recommended":
"default":
"description":
},
"arg_4": {
"type":
"is_recommended":
"default":
"description":
}
}
}
}
},
"command_2": {
"type": "object",
"properties: {
"arg_group_1": {
"type": "object",
"properties": {
"arg_1": {
"type":
"is_recommended":
"default":
"description":
}
}
}
}
}
}
"""
import sys
import json
from configs import CONFIG_POOL
PARAM_TYPE_TO_VALUE_TYPE = {
'IntegerParamType': 'int',
'FloatParamType': 'float',
'BooleanParamType': 'bool',
'StringParamType': 'str',
'IndexParamType': 'index-string',
'JsonParamType': 'json-string'
}
def parse_args_from_config(config):
"""Parses available arguments from a configuration class.
Args:
config: The configuration class to parse arguments from, which is
defined in `configs/`. This class is supposed to derive from
`BaseConfig` defined in `configs/base_config.py`.
"""
def _dummy_func():
"""A dummy function used to parse decorator."""
args = dict()
func = config.add_options_to_command(config.get_options())(_dummy_func)
recommended_opts = config.get_recommended_options()
for opt in reversed(func.__click_params__):
if opt.group.title not in args:
args[opt.group.title] = dict(type='object', properties=dict())
args[opt.group.title]['properties'][opt.name] = dict(
type=PARAM_TYPE_TO_VALUE_TYPE[opt.type.__class__.__name__],
default=opt.default,
is_recommended=opt.name in recommended_opts,
description=opt.help
)
return args
def dump(configs, save_path):
"""Dumps available arguments from given configurations to target file.
Args:
configs: A list of configurations, each of which should be a
class derived from `BaseConfig` defined in `configs/base_config.py`.
save_path: The path to save the dumped results.
"""
args = dict()
for config in configs:
args[config.name] = dict(type='object',
properties=parse_args_from_config(config))
with open(save_path, 'w') as f:
json.dump(args, f, indent=4)
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.exit(f'Usage: python {sys.argv[0]} SAVE_PATH')
dump(CONFIG_POOL, sys.argv[1])