Skip to content

Commit

Permalink
Rename merge_config, add comment, move deepcopy into function
Browse files Browse the repository at this point in the history
hackebrot committed Jul 2, 2017
1 parent d76f7f2 commit 4a499ce
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions cookiecutter/config.py
Original file line number Diff line number Diff line change
@@ -39,19 +39,23 @@ def _expand_path(path):
return path


def merge_config(default, overwrite):
def merge_configs(default, overwrite):
"""Recursively update a dict with the key/value pair of another.
Dict values that are dictionaries themselves will be updated, whilst
preserving existing keys.
"""
new_config = copy.deepcopy(default)

for k, v in overwrite.items():
# Make sure to preserve existing items in
# nested dicts, for example `abbreviations`
if isinstance(v, dict):
default[k] = merge_config(default[k], v)
continue
default[k] = v
new_config[k] = merge_configs(default[k], v)
else:
new_config[k] = v

return default
return new_config


def get_config(config_path):
@@ -69,7 +73,7 @@ def get_config(config_path):
''.format(config_path, e)
)

config_dict = merge_config(copy.deepcopy(DEFAULT_CONFIG), yaml_dict)
config_dict = merge_configs(DEFAULT_CONFIG, yaml_dict)

raw_replay_dir = config_dict['replay_dir']
config_dict['replay_dir'] = _expand_path(raw_replay_dir)
2 changes: 1 addition & 1 deletion tests/test_get_config.py
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ def test_merge_config():
}
}

assert config.merge_config(default, user_config) == expected_config
assert config.merge_configs(default, user_config) == expected_config


def test_get_config():

0 comments on commit 4a499ce

Please sign in to comment.