-
Notifications
You must be signed in to change notification settings - Fork 1
/
toolkit_config.py
75 lines (60 loc) · 2.12 KB
/
toolkit_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
Retrun the config in dict
'''
import configparser
import os.path
import logging
config_file = os.path.dirname(
os.path.realpath(__file__)) + os.sep + 'config.ini'
def read_config_general(configFile=config_file):
try:
logging.info('Read config file: ' + configFile)
configRead = configparser.ConfigParser()
configRead.read(configFile)
config = {}
for sectionName in configRead.sections():
config[sectionName] = {}
options = [i for i in configRead.options(sectionName)]
for j in options:
config[sectionName][j] = configRead[sectionName][j]
except Exception as e:
logging.error("Error read config file, check config.ini")
# print(e)
raise
return config
def read_config_mail(configFile=config_file):
try:
logging.info('Read config file: ' + configFile)
configRead = configparser.ConfigParser()
configRead.read(configFile)
sectionName = 'mail'
options = [i.upper() for i in configRead.options(sectionName)]
config = {}
for j in options:
config[j] = configRead[sectionName][j]
except Exception as e:
logging.error("Error read config file, check config.ini")
# print(e)
raise
return config
def read_config_mysql(configFile=config_file):
try:
logging.info('Read config file: ' + configFile)
configRead = configparser.ConfigParser()
configRead.read(configFile)
config = {
'host': configRead['database']['host'],
'port': int(configRead['database']['port']),
'user': configRead['database']['user'],
'passwd': configRead['database']['passwd'],
'db': configRead['database']['db'],
'charset': 'utf8'
}
except Exception as e:
logging.error("Error read config file, check config.ini")
# print(e)
raise
return config
if __name__ == '__main__':
config = read_config_mail('config_test.ini')
print(config)