|
5 | 5 | import os, socket |
6 | 6 | from glob import iglob |
7 | 7 | 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 |
9 | 53 |
|
10 | 54 | class ParticipantHandler(object): |
11 | 55 |
|
@@ -169,11 +213,7 @@ def get_process(self, trigger, project): |
169 | 213 | continue |
170 | 214 |
|
171 | 215 | 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]) |
177 | 217 | self.log.info("Found valid conf %s.conf" % filename[:-5]) |
178 | 218 | except IOError as exc: |
179 | 219 | # we don't care if there is no .conf file |
|
0 commit comments