forked from jmlingeman/Network-Inference-Workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteConfig.py
36 lines (31 loc) · 1.48 KB
/
WriteConfig.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
def WriteConfig(algorithm_name, settings ):
# What we are going to try to do here is read in a template file
# that has the arguments we're filling in the the format {{argname}}
# and replace those with the value that we have stored in the settings
# hash.
template_dir = settings["global"]["template_dir"]
template_file = open(template_dir + '/' + algorithm_name + '.' + \
settings[algorithm_name]["file_ext"], 'r')
# Read in the template
template = template_file.readlines()
for i in xrange(len(template)):
line = template[i]
if "{{" in line and "}}" in line:
# Loop through the line and replace all of the params with the
# params from the settings dict.
if line.count("{{") == line.count("}}"):
for p in xrange(line.count("{{")):
line = template[i]
param_str = line[line.index("{{"):line.index("}}")+2]
template[i] = template[i].replace(param_str, \
str(settings[algorithm_name][param_str[2:len(param_str)-2]]))
else:
print "ERROR in template file. Forgot {{ or }}?"
settings[algorithm_name]["settings_file"] = \
settings[algorithm_name]["output_dir"] + "/" + algorithm_name + \
"_settings" + "." + settings[algorithm_name]["file_ext"]
config_file = open(settings[algorithm_name]["settings_file"], 'w')
for l in template:
config_file.write(l)
config_file.flush()
config_file.close()