def _write_new_config_file(path, output): original_contents = open(path).read() output = remove_defaults(output, CONFIG_SCHEMA) new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS) lines = original_contents.splitlines(True) rev_line_indices_reversed = list(reversed([ i for i, line in enumerate(lines) if REV_LINE_RE.match(line) ])) for line in new_contents.splitlines(True): if REV_LINE_RE.match(line): # It's possible we didn't identify the rev lines in the original if not rev_line_indices_reversed: break line_index = rev_line_indices_reversed.pop() original_line = lines[line_index] orig_match = REV_LINE_RE.match(original_line) new_match = REV_LINE_RE.match(line) lines[line_index] = REV_LINE_FMT.format( orig_match.group(1), orig_match.group(2), new_match.group(3), orig_match.group(4), ) # If we failed to intelligently rewrite the rev lines, fall back to the # pretty-formatted yaml output to_write = ''.join(lines) if remove_defaults(ordered_load(to_write), CONFIG_SCHEMA) != output: to_write = new_contents with open(path, 'w') as f: f.write(to_write)
def _write_new_config_file(path, output): with open(path) as f: original_contents = f.read() output = remove_defaults(output, CONFIG_SCHEMA) new_contents = ordered_dump(output, **C.YAML_DUMP_KWARGS) lines = original_contents.splitlines(True) rev_line_indices_reversed = list( reversed([ i for i, line in enumerate(lines) if REV_LINE_RE.match(line) ]), ) for line in new_contents.splitlines(True): if REV_LINE_RE.match(line): # It's possible we didn't identify the rev lines in the original if not rev_line_indices_reversed: break line_index = rev_line_indices_reversed.pop() original_line = lines[line_index] orig_match = REV_LINE_RE.match(original_line) new_match = REV_LINE_RE.match(line) lines[line_index] = REV_LINE_FMT.format( orig_match.group(1), orig_match.group(2), new_match.group(3), orig_match.group(4), ) # If we failed to intelligently rewrite the rev lines, fall back to the # pretty-formatted yaml output to_write = ''.join(lines) if remove_defaults(ordered_load(to_write), CONFIG_SCHEMA) != output: to_write = new_contents with open(path, 'w') as f: f.write(to_write)
def test_conditional_recurse_remove_defaults(tvalue): val = {'t': tvalue, 'v': {'k': tvalue}} ret = remove_defaults(val, conditional_recurse) assert ret == {'t': tvalue, 'v': {}} val = {'t': tvalue, 'v': {'k': not tvalue}} ret = remove_defaults(val, conditional_recurse) assert ret == {'t': tvalue, 'v': {'k': not tvalue}}
def test_conditional_optional_remove_default(tvalue): ret = remove_defaults({'t': tvalue, 'v': tvalue}, conditional_optional) assert ret == {'t': tvalue} ret = remove_defaults({'t': tvalue, 'v': not tvalue}, conditional_optional) assert ret == {'t': tvalue, 'v': not tvalue}
def test_optional_optional_remove_defaults(): val = {'builder': {'noop': True}} ret = remove_defaults(val, optional_nested_optional_schema) assert ret == {}
def test_remove_defaults_optional_recurse_non_default(): ret = remove_defaults({'links': [{'key': True}]}, optional_nested_schema) assert ret == {'links': [{'key': True}]}
def test_remove_defaults_optional_recurse_present_at_default(): assert remove_defaults({'links': []}, optional_nested_schema) == {}
def test_remove_defaults_optional_recurse_not_present(): assert remove_defaults({}, optional_nested_schema) == {}
def test_remove_defaults_nested(): val = {'repo': 'repo1', 'hooks': [{'key': False}]} ret = remove_defaults(val, nested_schema_optional) assert ret == {'repo': 'repo1', 'hooks': [{}]}
def test_remove_defaults_does_nothing_on_non_optional(): ret = remove_defaults({'key': True}, map_required) assert ret == {'key': True}
def test_remove_defaults_map_in_list(): ret = remove_defaults([{'key': False}], Array(map_optional)) assert ret == [{}]
def test_remove_defaults_does_not_change_non_default(): ret = remove_defaults({'key': True}, map_optional) assert ret == {'key': True}
def test_remove_defaults_nothing_to_remove(): ret = remove_defaults({}, map_optional) assert ret == {}
def test_remove_defaults_removes_defaults(): ret = remove_defaults({'key': False}, map_optional) assert ret == {}
def test_remove_defaults_copies_object(): val = {'key': False} ret = remove_defaults(val, map_optional) assert ret is not val