-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCfgReader.py
More file actions
58 lines (51 loc) · 2.31 KB
/
CfgReader.py
File metadata and controls
58 lines (51 loc) · 2.31 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
#!/usr/bin/env python
#*PLEASE NOTE * In order for the program to work, you must make sure the config file begins immediately in the following format:
#Key:Value. Starting from the very top line of the file and going down.
#If you have other config files you would like to include, please write: incldue:additional_config_file_name
import sys
from collections import OrderedDict
script_name = sys.argv[0][:-3]
if len(sys.argv) <= 1:
cfg_file_name = script_name + '.cfg'
else:
cfg_file_name = sys.argv[1]
def getConfig(directory_path):
config_path = directory_path
vector_cfg_file_names = []
dic = dict()
#vector_cfg_file_names, dic = loadcfg(directory_path, cfg_file_name ,vector_cfg_file_names, dic)
loadcfg(directory_path, cfg_file_name ,vector_cfg_file_names, dic)
return dic
def loadcfg(config_path, file_name,vector_cfg_file_names,dic):
vector_cfg_file_names.append(file_name)
abs_file_name = file_name
current_file = open(abs_file_name) #Open the config file to be looked through
for line in current_file:
lineSplit = line.split(':', 1)
key = lineSplit[0]
try:
NEW_value = lineSplit[1].strip()
except IndexError:
continue
name = NEW_value
if key == "include": #This means there are other cfg files to be included
found = 0
for old_file in vector_cfg_file_names:
print "vector_cfg_file_name=", old_file
if name == old_file: #Check if the included file is already in the list of config files to parse
found = 1
break
if found == 1:
print "file already included"
print dic.items()
continue
else:
print "Recursive call ......"
loadcfg(config_path, NEW_value ,vector_cfg_file_names ,dic) #Recursively look through each config file included otherwise
else:
if key in dic.keys():
print "WARNING: already exists in dictionary <key,Old_value> and is now replaced by <key, NEW_value>"
dic[key] = NEW_value
else:
dic[key] = NEW_value
return vector_cfg_file_names,dic