Skip to content

Commit

Permalink
step 5
Browse files Browse the repository at this point in the history
  • Loading branch information
DSungatulin committed Jan 8, 2024
1 parent ce9e6e2 commit 62de713
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 33 deletions.
37 changes: 23 additions & 14 deletions gendiff/define_diff.py
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)
6 changes: 0 additions & 6 deletions gendiff/engine.py

This file was deleted.

18 changes: 9 additions & 9 deletions gendiff/parser.py
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
6 changes: 3 additions & 3 deletions gendiff/scripts/gendiff.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/usr/bin/env python3
from gendiff.define_diff import generate_diff
from gendiff import parser
from gendiff.parser import parse_file


def main():
first_file, second_file = parser.parse_file()
file_difference = generate_diff(first_file, second_file)
args = parse_file()
file_difference = generate_diff(args.first_file, args.second_file)
return print(file_difference)


Expand Down
61 changes: 60 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ packages = [{ include = "gendiff" },]

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
pyyaml = "^6.0.1"

[tool.poetry.group.dev.dependencies]
flake8 = "^6.1.0"
Expand Down

0 comments on commit 62de713

Please sign in to comment.