-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
executable file
·55 lines (40 loc) · 1.59 KB
/
config.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
"""helper for configuring"""
from configparser import ConfigParser
import os
import os_wrap
ConfigDirPath = 'config'
class Conf(ConfigParser):
def __init__(self, name):
super().__init__()
self.optionxform = str
self.path = ConfigDirPath+'/'+os.path.basename(name).split('.')[0]+'.conf'
os_wrap.check_create_dir(ConfigDirPath)
if not os.path.exists(self.path) or not os.path.isfile(self.path):
print('Config file '+self.path+' not found. It will be created automatically. ')
self['DEFAULT'] = {'TestSettings': 'YES'}
with open(self.path, "w+") as file:
file.write(f"### Config file for {name}. Created automatically.\n\n")
self.write(file)
self.read(self.path)
def save(self): # save config to file
with open(self.path, "w+") as file:
self.write(file)
def chk_add_key_data(self, key: str, data: dict): # check and add options to keq if necessary
if key not in self:
self[key] = data
self.save()
return
for option in data:
if option not in self[key]:
self[key][option] = data[option]
self.save()
if __name__ == "__main__":
print("test for config module")
config = Conf('testname.py')
config.read(f'{ConfigDirPath}/testname')
print(config)
print('TestSetting:', config['DEFAULT']['TestSettings'])
print('all keys in config:')
for key in config:
print(key)
config.chk_add_key_data('LOGGER', {'dfsd': 'sdfsdf'})