-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd45150
commit ce9e6e2
Showing
16 changed files
with
184 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,14 @@ | ||
import json | ||
|
||
|
||
def generate_diff(file_path1, file_path2): | ||
def generate_diff_recursion(file_content1, file_content2): | ||
keys = sorted(set(file_content1.keys()) | set(file_content2.keys())) | ||
diff_lines = [] | ||
for key in keys: | ||
if key not in file_content2: | ||
diff_lines.append(f' - {key}: {file_content1[key]}') | ||
elif key not in file_content1: | ||
diff_lines.append(f' + {key}: {file_content2[key]}') | ||
elif file_content1[key] != file_content2[key]: | ||
diff_lines.append(f' - {key}: {file_content1[key]}') | ||
diff_lines.append(f' + {key}: {file_content2[key]}') | ||
else: | ||
diff_lines.append(f' {key}: {file_content1[key]}') | ||
return '{\n' + '\n'.join(diff_lines) + '\n}' | ||
|
||
with open(file_path1, 'r') as f1, open(file_path2, 'r') as f2: | ||
file_content1 = json.load(f1) | ||
file_content2 = json.load(f2) | ||
return generate_diff_recursion(file_content1, file_content2) | ||
def generate_diff(first_file_content, second_file_content): | ||
keys = sorted(set(first_file_content.keys()) | set(second_file_content.keys())) | ||
diff_lines = [] | ||
for key in keys: | ||
if key not in second_file_content: | ||
diff_lines.append(f' - {key}: {first_file_content[key]}') | ||
elif key not in first_file_content: | ||
diff_lines.append(f' + {key}: {second_file_content[key]}') | ||
elif first_file_content[key] != second_file_content[key]: | ||
diff_lines.append(f' - {key}: {first_file_content[key]}') | ||
diff_lines.append(f' + {key}: {second_file_content[key]}') | ||
else: | ||
diff_lines.append(f' {key}: {first_file_content[key]}') | ||
return '{\n' + '\n'.join(diff_lines) + '\n}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,6 @@ | ||
from gendiff.json_diff import open_json | ||
from gendiff.yaml_diff import open_yaml | ||
|
||
|
||
def convert_bool_value(val): | ||
bool_value = {'True': 'true', 'False': 'false'} | ||
if isinstance(val, bool): | ||
return bool_value[str(val)] | ||
else: | ||
return val | ||
|
||
|
||
def format_decoder(filepath): | ||
if filepath.endswith('json'): | ||
return open_json(filepath) | ||
elif filepath.endswith('yml') or filepath.endswith('yaml'): | ||
return open_yaml(filepath) | ||
else: | ||
raise ValueError("Invalid file format!") |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,23 @@ | ||
import argparse | ||
import json | ||
import yaml | ||
import os | ||
|
||
|
||
def parse_file(): | ||
def parse_file(first_file, second_file): | ||
parser = argparse.ArgumentParser( | ||
description='Compares two configuration files and shows a difference.' | ||
) | ||
parser.add_argument('first_file') | ||
parser.add_argument('second_file') | ||
parser.add_argument('-f', '--format', help='set format of output') | ||
args = parser.parse_args() | ||
|
||
file_ext = os.path.splitext(first_file)[1] | ||
if file_ext == 'json': | ||
deserialized_first_file, deserialized_second_file = json.load(open(args.first_file, args.second_file)) | ||
elif file_ext == 'yml' or file_ext == 'yaml': | ||
deserialized_first_file, deserialized_second_file = yaml.safe_load(open(args.first_file, args.second_file)) | ||
else: | ||
raise ValueError("Invalid file format!") | ||
return deserialized_first_file, deserialized_second_file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
44 changes: 44 additions & 0 deletions
44
tests/fixtures/comparison_results/correct_recursive_comparison
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
{ | ||
common: { | ||
+ follow: false | ||
setting1: Value 1 | ||
- setting2: 200 | ||
- setting3: true | ||
+ setting3: null | ||
+ setting4: blah blah | ||
+ setting5: { | ||
key5: value5 | ||
} | ||
setting6: { | ||
doge: { | ||
- wow: | ||
+ wow: so much | ||
} | ||
key: value | ||
+ ops: vops | ||
} | ||
} | ||
group1: { | ||
- baz: bas | ||
+ baz: bars | ||
foo: bar | ||
- nest: { | ||
key: value | ||
} | ||
+ nest: str | ||
} | ||
- group2: { | ||
abc: 12345 | ||
deep: { | ||
id: 45 | ||
} | ||
} | ||
+ group3: { | ||
deep: { | ||
id: { | ||
number: 45 | ||
} | ||
} | ||
fee: 100500 | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
timeout: 20 | ||
verbose: true | ||
host: hexlet.io | ||
host: hexlet.io |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"common": { | ||
"setting1": "Value 1", | ||
"setting2": 200, | ||
"setting3": true, | ||
"setting6": { | ||
"key": "value", | ||
"doge": { | ||
"wow": "" | ||
} | ||
} | ||
}, | ||
"group1": { | ||
"baz": "bas", | ||
"foo": "bar", | ||
"nest": { | ||
"key": "value" | ||
} | ||
}, | ||
"group2": { | ||
"abc": 12345, | ||
"deep": { | ||
"id": 45 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
common: { | ||
setting1: Value 1, | ||
setting2: 200, | ||
setting3: true, | ||
setting6: { | ||
key: value, | ||
doge: { | ||
wow: | ||
} | ||
} | ||
}, | ||
group1: { | ||
baz: bas, | ||
foo: bar, | ||
nest: { | ||
key: value | ||
} | ||
}, | ||
group2: { | ||
abc: 12345, | ||
deep: { | ||
id: 45 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"common": { | ||
"follow": false, | ||
"setting1": "Value 1", | ||
"setting3": null, | ||
"setting4": "blah blah", | ||
"setting5": { | ||
"key5": "value5" | ||
}, | ||
"setting6": { | ||
"key": "value", | ||
"ops": "vops", | ||
"doge": { | ||
"wow": "so much" | ||
} | ||
} | ||
}, | ||
"group1": { | ||
"foo": "bar", | ||
"baz": "bars", | ||
"nest": "str" | ||
}, | ||
"group3": { | ||
"deep": { | ||
"id": { | ||
"number": 45 | ||
} | ||
}, | ||
"fee": 100500 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
common: { | ||
follow: false | ||
setting1: Value 1 | ||
setting3: null | ||
setting4: blah blah | ||
setting5: { | ||
key5: value5 | ||
} | ||
setting6: { | ||
key: value | ||
ops: vops | ||
doge: { | ||
wow: so much | ||
} | ||
} | ||
} | ||
group1: { | ||
foo: bar | ||
baz: bars | ||
nest: str | ||
} | ||
group3: { | ||
deep: { | ||
id: { | ||
number: 45 | ||
} | ||
} | ||
fee: 100500 | ||
} |