-
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
ce9e6e2
commit 62de713
Showing
6 changed files
with
96 additions
and
33 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,14 +1,23 @@ | ||
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}' | ||
from gendiff.parser import determine_file_format | ||
|
||
|
||
def generate_diff(filepath1, filepath2): | ||
def generate_diff_recursion(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}' | ||
|
||
with open(filepath1, 'r') as f1, open(filepath2, 'r') as f2: | ||
data1 = determine_file_format(f1) | ||
data2 = determine_file_format(f2) | ||
return generate_diff_recursion(data1, data2) |
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,23 +1,23 @@ | ||
import argparse | ||
import json | ||
import yaml | ||
import os | ||
|
||
|
||
def parse_file(first_file, second_file): | ||
def parse_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)) | ||
return args | ||
|
||
def determine_file_format(filepath): | ||
if filepath.endswith('json'): | ||
data = json.load(open(filepath)) | ||
elif filepath.endswith('yml') or filepath.endswith('yaml'): | ||
data = yaml.safe_load(open(filepath)) | ||
else: | ||
raise ValueError("Invalid file format!") | ||
return deserialized_first_file, deserialized_second_file | ||
return data |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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