Skip to content

Commit c7d7a47

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 c7d7a47

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

launchers/robogrator.py

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

1054
class ParticipantHandler(object):
1155

@@ -169,11 +213,7 @@ def get_process(self, trigger, project):
169213
continue
170214

171215
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))
216+
config = merge_configs("%s.conf" % filename[:-5])
177217
self.log.info("Found valid conf %s.conf" % filename[:-5])
178218
except IOError as exc:
179219
# we don't care if there is no .conf file

0 commit comments

Comments
 (0)