-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigtoargs.py
More file actions
87 lines (74 loc) · 2.45 KB
/
configtoargs.py
File metadata and controls
87 lines (74 loc) · 2.45 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
85
86
87
import configparser
from time import time
# 读取配置文件并返回字典
def read_config():
configargs = {}
config = configparser.ConfigParser()
config.sections()
config.read('config.ini', encoding="utf-8-sig")
for n in config.sections():
for key in config[n]:
if key == 'seed':
if config[n][key] == '-1':
configargs[key] = time()
else:
configargs[key] = config[n][key]
elif key =='collection':
configargs[key] = [k.strip() for k in config[n][key].split(',') if k.strip() != '']
elif key in ['initialize', 'update', 'clear', 'hideimage', 'debug', 'cache', 'configfile']:
configargs[key] = config[n].getboolean(key)
else:
configargs[key] = config[n][key]
return config, configargs
def write_config(args):
config = configparser.ConfigParser()
config['Basic'] = {
'mode': args.mode,
'time': args.time,
'background': args.background,
'seed': '-1',
}
config['DataBase'] = {
'client': args.client,
'database': args.database,
'collection': ','.join(args.collection),
'initialize': args.initialize,
'update': args.update,
'clear': args.clear,
}
config['Advanced'] = {
'configfile': args.configfile,
'screensize': args.screensize,
'hideimage': args.hideimage,
'targetpath': args.targetpath,
'cache': args.cache,
}
config['Other'] = {
'debug': args.debug,
'version': 'Random Image Viewer v2',
}
with open('config.ini', 'w', encoding='utf8') as configfile:
config.write(configfile)
def check_configfile_option_and_save(args):
config = configparser.ConfigParser()
config.read('config.ini', encoding="utf-8-sig")
if args.configfile:
write_config(args)
elif config['Advanced'].getboolean('configfile'):
config['Advanced']['configfile'] = 'false'
with open('config.ini', 'w', encoding='utf8') as configfile:
config.write(configfile)
# 用于模仿args的类
class Args:
def __init__(self, **kwargs):
self.__dict__.update(kwargs)
# 返回模仿的args
def configtoargs():
_, args = read_config()
args = Args(**args)
return args
if __name__ == '__main__':
config, args = read_config()
a = Args(**args)
print(a.__dict__)
print('Done')