Skip to content

Commit 41443a8

Browse files
committed
robogrator: Inherit config values from parent project(s)
Start reading config values from root and merge changes defined later in project path.
1 parent 0b07383 commit 41443a8

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

launchers/robogrator.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,49 @@
66
from glob import iglob
77
import json
88

9+
def dict_merge(a, b):
10+
'''recursively merges dict's. not just simple a['key'] = b['key'], if
11+
both a and bhave a key who's value is a dict then dict_merge is called
12+
on both values and the result stored in the returned dictionary.'''
13+
if not isinstance(b, dict):
14+
return b
15+
result = deepcopy(a)
16+
for k, v in b.iteritems():
17+
if k in result and isinstance(result[k], dict):
18+
result[k] = dict_merge(result[k], v)
19+
else:
20+
result[k] = deepcopy(v)
21+
return result
22+
23+
def merge_configs(config_path):
24+
""" takes full path to config file requested. Tries to read that file
25+
from root dir and then overwrite value if they are defined later in
26+
project hierarcy.
27+
28+
e.g. /<look_fist_here>/<then_here>/<etc>/<actual>.conf
29+
"""
30+
31+
conf_look_path = ""
32+
final_config = None
33+
path, conf_filename = os.path.split(config_path)
34+
35+
# start looking / reading conf file from the root dir and
36+
# then add / modify keys found from childer
37+
for p in path.split(os.sep):
38+
conf_look_path = os.path.join(conf_look_path, p)
39+
mconf = os.path.join(conf_look_path, conf_filename)
40+
if os.path.exists(mconf):
41+
with open(mconf, 'r') as config_file:
42+
lines = [line.strip() if not line.strip().startswith('#') else \
43+
"" for line in config_file.readlines()]
44+
config = json.loads("\n".join(lines))
45+
if not final_config:
46+
final_config = config
47+
else:
48+
# deep merge later found confs to final conf
49+
final_config = dict_merge(final_config, config)
50+
51+
return final_config
952

1053
class ParticipantHandler(object):
1154

@@ -169,11 +212,7 @@ def get_process(self, trigger, project):
169212
continue
170213

171214
try:
172-
config = None
173-
with open("%s.conf" % filename[:-5], 'r') as config_file:
174-
lines = [line.strip() if not line.strip().startswith('#') \
175-
else "" for line in config_file.readlines()]
176-
config = json.loads("\n".join(lines))
215+
config = merge_configs("%s.conf" % filename[:-5])
177216
self.log.info("Found valid conf %s.conf" % filename[:-5])
178217
except IOError as exc:
179218
# we don't care if there is no .conf file

0 commit comments

Comments
 (0)