-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.py
46 lines (37 loc) · 1.27 KB
/
cfg.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
import argparse
import utils
# 通过命令行对 appconf.ini 文件中的配置进行修改
#
# 使用方法 python3 cfg.py -u xxx.xxx=yyy
# 例如 python3 cfg.py -u "config.use=google"
# python3 cfg.py -u "mod.namespace=xxx"
def update(update_param):
# 解析参数
key_end_pos = update_param.find('=')
arg_left = update_param[:key_end_pos]
arg_right = update_param[key_end_pos + 1:]
key_arr = arg_left.split('.')
section = key_arr[0]
option = key_arr[1]
value = arg_right
config = utils.read_config('appconf.ini')
config.set(section, option, value)
utils.write_config(config, 'appconf.ini')
def show():
config = utils.read_config('appconf.ini')
for section in config.sections():
print(f"[Section: {section}]")
for option, value in config.items(section):
print(f"{option} = {value}")
print('')
def work(args):
if args.u != '':
update(args.u)
elif args.show:
show()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-u', type=str, default='', help='update config的参数。遵循section.option.value 的写法')
parser.add_argument('-show', action='store_true', help='print config')
args = parser.parse_args()
work(args)