-
Notifications
You must be signed in to change notification settings - Fork 14
/
dump_command_args.py
120 lines (108 loc) · 3.57 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
# 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": {
"is_recommended": # true / false
"type": # int / float / bool / str / json-string /
# index-string
"default":
"description":
},
"arg_2": {
"is_recommended":
"type":
"default":
"description":
}
}
},
"arg_group_2": {
"type": "object",
"properties": {
"arg_3": {
"is_recommended":
"type":
"default":
"description":
},
"arg_4": {
"is_recommended":
"type":
"default":
"description":
}
}
}
}
},
"command_2": {
"type": "object",
"properties: {
"arg_group_1": {
"type": "object",
"properties": {
"arg_1": {
"is_recommended":
"type":
"default":
"description":
}
}
}
}
}
}
"""
import sys
import json
from configs import CONFIG_POOL
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`.
"""
recommended_opts = config.get_recommended_options()
args = dict()
for opt_group, opts in config.get_options().items():
args[opt_group] = dict(
type='object',
properties=dict()
)
for opt in opts:
arg = config.inspect_option(opt)
args[opt_group]['properties'][arg.name] = dict(
is_recommended=arg.name in recommended_opts,
type=arg.type,
default=arg.default,
description=arg.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])