forked from arcc/SG2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_config.py
47 lines (45 loc) · 1.48 KB
/
get_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
#!/usr/bin/python
import os
from mysql.connector.constants import ClientFlag
def get_config(filename):
curr_file_path = os.path.abspath(__file__)
path, pyf = os.path.split(curr_file_path)
fn = os.path.join(path, filename)
f = open(fn)
config = {}
local = False
for line in f.readlines():
line = line.strip()
if line.startswith("#"):
continue
if line.startswith('local'):
ls = line.split()
if ls[1].lower() in ['true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh']:
local = True
continue
ls = line.split()
if ls != []:
if ls[0].startswith('<'):
block = ls[0][1:-1]
config[block] = {}
else:
if block == '':
raise ValueError("A information block has to start wtih <block_name>")
config[block][ls[0]]=ls[1]
result_local = {}
result = {}
for key in config.keys():
if 'local' in key:
reskey = key.split('_')[1]
result_local[reskey] = config[key]
else:
result[key] = config[key]
for db in result.keys(): # TODO: Need to change here in the futhre.
if 'client_flags' in result[db].keys():
result[db]['client_flags'] = [ClientFlag.LOCAL_FILES]
if local:
return result_local
else:
return result
if __name__== "__main__":
print get_config('config.dat')