def pop(self, key, default=empty): try: value = pop_nested_key(self._wrapped, key) except KeyError: if default is empty: raise KeyError("Key '{0}' not found in {1}".format( key, self.wrapped)) else: value = default return self.resolve(value)
def pop(self, key, default=empty): try: value = pop_nested_key(self._wrapped, key) except KeyError: if default is empty: raise KeyError( "Key '{0}' not found in {1}".format(key, self._wrapped) ) else: value = default return self.resolve(value)
def upgrade_v1_to_v2(v1_config): """ Upgrade a v1 config file to a v2 config file. """ errors = get_validation_errors(v1_config, version=V1) if errors: raise ValueError( "Cannot upgrade invalid config. Please ensure that your current " "configuration file is valid:\n\n{0}".format( format_errors(errors), ) ) v1_default_config = load_default_config(version=V1) v2_default_config = load_default_config(version=V2) if v1_config == v1_default_config: return v2_default_config upgraded_v1_config = copy.deepcopy(v1_config) for key_path in NEW_V2_PATHS: if has_nested_key(upgraded_v1_config, key_path): continue set_nested_key( upgraded_v1_config, key_path, get_nested_key(v2_default_config, key_path), ) for old_path, new_path in V2_TRANSLATIONS.items(): if not has_nested_key(upgraded_v1_config, old_path): continue set_nested_key( upgraded_v1_config, new_path, pop_nested_key(upgraded_v1_config, old_path), ) for key_path in V2_UPDATES: if not has_nested_key(upgraded_v1_config, key_path): continue current_value = get_nested_key(upgraded_v1_config, key_path) old_default_value = get_nested_key(v1_default_config, key_path) if current_value == old_default_value: set_nested_key( upgraded_v1_config, key_path, get_nested_key(v2_default_config, key_path), ) # bump the version set_nested_key(upgraded_v1_config, 'version', V2) errors = get_validation_errors(upgraded_v1_config, version=V2) if errors: raise ValueError( "Upgraded configuration did not pass validation:\n\n" "\n=============Original-Configuration============\n" "{0}" "\n=============Upgraded-Configuration============\n" "{1}" "\n=============Validation-Errors============\n" "{2}".format( pprint.pformat(dict(v1_config)), pprint.pformat(dict(upgraded_v1_config)), format_errors(errors), ) ) return upgraded_v1_config
def upgrade_v3_to_v4(v3_config): """ Upgrade a v3 config file to a v4 config file. """ errors = get_validation_errors(v3_config, version=V3) if errors: raise ValueError( "Cannot upgrade invalid config. Please ensure that your current " "configuration file is valid:\n\n{0}".format( format_errors(errors), )) v3_default_config = load_default_config(version=V3) v4_default_config = load_default_config(version=V4) if v3_config == v3_default_config: return v4_default_config upgraded_v3_config = copy.deepcopy(v3_config) for key_path in NEW_V4_PATHS: if has_nested_key(upgraded_v3_config, key_path): continue set_nested_key( upgraded_v3_config, key_path, get_nested_key(v4_default_config, key_path), ) for old_path, new_path in MOVED_V3_PATHS.items(): default_value = get_nested_key(v4_default_config, new_path) if has_nested_key(upgraded_v3_config, old_path): existing_value = pop_nested_key(upgraded_v3_config, old_path) if is_dict(default_value) and is_dict(existing_value): merged_value = deep_merge_dicts(default_value, existing_value) elif is_list_like(default_value) and is_list_like(existing_value): merged_value = list( set(itertools.chain(default_value, existing_value))) else: raise ValueError("Unable to merge {0} with {1}".format( type(default_value), type(existing_value), )) set_nested_key( upgraded_v3_config, new_path, merged_value, ) else: set_nested_key( upgraded_v3_config, new_path, default_value, ) # bump the version set_nested_key(upgraded_v3_config, 'version', V4) errors = get_validation_errors(upgraded_v3_config, version=V4) if errors: raise ValueError("Upgraded configuration did not pass validation:\n\n" "\n=============Original-Configuration============\n" "{0}" "\n=============Upgraded-Configuration============\n" "{1}" "\n=============Validation-Errors============\n" "{2}".format( pprint.pformat(dict(v3_config)), pprint.pformat(dict(upgraded_v3_config)), format_errors(errors), )) return upgraded_v3_config
def upgrade_v3_to_v4(v3_config): """ Upgrade a v3 config file to a v4 config file. """ errors = get_validation_errors(v3_config, version=V3) if errors: raise ValueError( "Cannot upgrade invalid config. Please ensure that your current " "configuration file is valid:\n\n{0}".format( format_errors(errors), ) ) v3_default_config = load_default_config(version=V3) v4_default_config = load_default_config(version=V4) if v3_config == v3_default_config: return v4_default_config upgraded_v3_config = copy.deepcopy(v3_config) for key_path in NEW_V4_PATHS: if has_nested_key(upgraded_v3_config, key_path): continue set_nested_key( upgraded_v3_config, key_path, get_nested_key(v4_default_config, key_path), ) for old_path, new_path in MOVED_V3_PATHS.items(): default_value = get_nested_key(v4_default_config, new_path) if has_nested_key(upgraded_v3_config, old_path): existing_value = pop_nested_key(upgraded_v3_config, old_path) if is_dict(default_value) and is_dict(existing_value): merged_value = deep_merge_dicts(default_value, existing_value) elif is_list_like(default_value) and is_list_like(existing_value): merged_value = list(set(itertools.chain(default_value, existing_value))) else: raise ValueError( "Unable to merge {0} with {1}".format( type(default_value), type(existing_value), ) ) set_nested_key( upgraded_v3_config, new_path, merged_value, ) else: set_nested_key( upgraded_v3_config, new_path, default_value, ) # bump the version set_nested_key(upgraded_v3_config, 'version', V4) errors = get_validation_errors(upgraded_v3_config, version=V4) if errors: raise ValueError( "Upgraded configuration did not pass validation:\n\n" "\n=============Original-Configuration============\n" "{0}" "\n=============Upgraded-Configuration============\n" "{1}" "\n=============Validation-Errors============\n" "{2}".format( pprint.pformat(dict(v3_config)), pprint.pformat(dict(upgraded_v3_config)), format_errors(errors), ) ) return upgraded_v3_config
def upgrade_v5_to_v6(v5_config): """ Upgrade a v5 config file to a v6 config file. """ errors = get_validation_errors(v5_config, version=V5) if errors: raise ValueError( "Cannot upgrade invalid config. Please ensure that your current " "configuration file is valid:\n\n{0}".format( format_errors(errors), ) ) v5_default_config = load_default_config(version=V5) v6_default_config = load_default_config(version=V6) if v5_config == v5_default_config: return v6_default_config upgraded_v5_config = copy.deepcopy(v5_config) # new configuration values whos keys were not present in the previous # configuration. for key_path in NEW_V6_PATHS: if has_nested_key(upgraded_v5_config, key_path): continue set_nested_key( upgraded_v5_config, key_path, get_nested_key(v6_default_config, key_path), ) if has_nested_key(upgraded_v5_config, 'compilation.contracts_source_dir'): current_contracts_source_dir = pop_nested_key( upgraded_v5_config, 'compilation.contracts_source_dir', ) if is_string(current_contracts_source_dir): contract_source_dirs = [current_contracts_source_dir] else: contract_source_dirs = current_contracts_source_dir set_nested_key( upgraded_v5_config, 'compilation.contract_source_dirs', contract_source_dirs, ) # bump the version set_nested_key(upgraded_v5_config, 'version', V6) errors = get_validation_errors(upgraded_v5_config, version=V6) if errors: raise ValueError( "Upgraded configuration did not pass validation:\n\n" "\n=============Original-Configuration============\n" "{0}" "\n=============Upgraded-Configuration============\n" "{1}" "\n=============Validation-Errors============\n" "{2}".format( pprint.pformat(dict(v5_config)), pprint.pformat(dict(upgraded_v5_config)), format_errors(errors), ) ) return upgraded_v5_config
def test_pop_nested_key(key, expected): config = copy.deepcopy(CONFIG_DICT) actual = pop_nested_key(config, key) assert actual == expected
def test_pop_nested_key_with_missing_key(key, expected): config = copy.deepcopy(CONFIG_DICT) with pytest.raises(KeyError): pop_nested_key(config, key)
def upgrade_v4_to_v5(v4_config): """ Upgrade a v4 config file to a v5 config file. """ errors = get_validation_errors(v4_config, version=V4) if errors: raise ValueError( "Cannot upgrade invalid config. Please ensure that your current " "configuration file is valid:\n\n{0}".format( format_errors(errors), )) v4_default_config = load_default_config(version=V4) v5_default_config = load_default_config(version=V5) if v4_config == v4_default_config: return v5_default_config upgraded_v4_config = copy.deepcopy(v4_config) # new configuration values whos keys were not present in the previous # configuration. for key_path in NEW_V5_PATHS: if has_nested_key(upgraded_v4_config, key_path): continue set_nested_key( upgraded_v4_config, key_path, get_nested_key(v5_default_config, key_path), ) # keys in the new configuration that were relocated. for old_path, new_path in MOVED_V4_PATHS.items(): default_value = get_nested_key(v5_default_config, new_path) if has_nested_key(upgraded_v4_config, old_path): existing_value = pop_nested_key(upgraded_v4_config, old_path) if is_dict(default_value) and is_dict(existing_value): merged_value = deep_merge_dicts(default_value, existing_value) elif is_list_like(default_value) and is_list_like(existing_value): merged_value = list( set(itertools.chain(default_value, existing_value))) else: raise ValueError("Unable to merge {0} with {1}".format( type(default_value), type(existing_value), )) set_nested_key( upgraded_v4_config, new_path, merged_value, ) else: set_nested_key( upgraded_v4_config, new_path, default_value, ) # keys from the previous configuration that were changed. for key_path in MODIFIED_V4_PATHS: new_default = get_nested_key(v5_default_config, key_path) if key_path not in upgraded_v4_config: set_nested_key( upgraded_v4_config, key_path, new_default, ) else: current_value = get_nested_key(upgraded_v4_config, key_path) old_default = get_nested_key(v4_default_config, key_path) if current_value == old_default: set_nested_key( upgraded_v4_config, key_path, new_default, ) # bump the version set_nested_key(upgraded_v4_config, 'version', V5) errors = get_validation_errors(upgraded_v4_config, version=V5) if errors: raise ValueError("Upgraded configuration did not pass validation:\n\n" "\n=============Original-Configuration============\n" "{0}" "\n=============Upgraded-Configuration============\n" "{1}" "\n=============Validation-Errors============\n" "{2}".format( pprint.pformat(dict(v4_config)), pprint.pformat(dict(upgraded_v4_config)), format_errors(errors), )) return upgraded_v4_config
def upgrade_v4_to_v5(v4_config): """ Upgrade a v4 config file to a v5 config file. """ errors = get_validation_errors(v4_config, version=V4) if errors: raise ValueError( "Cannot upgrade invalid config. Please ensure that your current " "configuration file is valid:\n\n{0}".format( format_errors(errors), ) ) v4_default_config = load_default_config(version=V4) v5_default_config = load_default_config(version=V5) if v4_config == v4_default_config: return v5_default_config upgraded_v4_config = copy.deepcopy(v4_config) # new configuration values whos keys were not present in the previous # configuration. for key_path in NEW_V5_PATHS: if has_nested_key(upgraded_v4_config, key_path): continue set_nested_key( upgraded_v4_config, key_path, get_nested_key(v5_default_config, key_path), ) # keys in the new configuration that were relocated. for old_path, new_path in MOVED_V4_PATHS.items(): default_value = get_nested_key(v5_default_config, new_path) if has_nested_key(upgraded_v4_config, old_path): existing_value = pop_nested_key(upgraded_v4_config, old_path) if is_dict(default_value) and is_dict(existing_value): merged_value = deep_merge_dicts(default_value, existing_value) elif is_list_like(default_value) and is_list_like(existing_value): merged_value = list(set(itertools.chain(default_value, existing_value))) else: raise ValueError( "Unable to merge {0} with {1}".format( type(default_value), type(existing_value), ) ) set_nested_key( upgraded_v4_config, new_path, merged_value, ) else: set_nested_key( upgraded_v4_config, new_path, default_value, ) # keys from the previous configuration that were changed. for key_path in MODIFIED_V4_PATHS: new_default = get_nested_key(v5_default_config, key_path) if key_path not in upgraded_v4_config: set_nested_key( upgraded_v4_config, key_path, new_default, ) else: current_value = get_nested_key(upgraded_v4_config, key_path) old_default = get_nested_key(v4_default_config, key_path) if current_value == old_default: set_nested_key( upgraded_v4_config, key_path, new_default, ) # bump the version set_nested_key(upgraded_v4_config, 'version', V5) errors = get_validation_errors(upgraded_v4_config, version=V5) if errors: raise ValueError( "Upgraded configuration did not pass validation:\n\n" "\n=============Original-Configuration============\n" "{0}" "\n=============Upgraded-Configuration============\n" "{1}" "\n=============Validation-Errors============\n" "{2}".format( pprint.pformat(dict(v4_config)), pprint.pformat(dict(upgraded_v4_config)), format_errors(errors), ) ) return upgraded_v4_config