Skip to content

Commit

Permalink
fix mentor's comments 2
Browse files Browse the repository at this point in the history
  • Loading branch information
DSungatulin committed Mar 3, 2024
1 parent 51c0652 commit 4df4fd7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 34 deletions.
15 changes: 15 additions & 0 deletions gendiff/arg_parser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import argparse


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', default='stylish',
choices=['stylish', 'plain', 'json'],
help='set format of output (default: \'stylish\')',
)
return parser.parse_args()
2 changes: 1 addition & 1 deletion gendiff/formatters/json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json


def json_format(current_data):
def get_jsoned(current_data):
return json.dumps(current_data, indent=2)
10 changes: 5 additions & 5 deletions gendiff/formatters/output_format_determinant.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from gendiff.formatters.stylish import get_stylished
from gendiff.formatters.plain import get_plained
from gendiff.formatters.json import json_format
from gendiff.formatters.json import get_jsoned


def determine_output_file_format(output_format):
def determine_output_file_format(diff, output_format):
if output_format == 'stylish':
return get_stylished
return get_stylished(diff)
elif output_format == 'plain':
return get_plained
return get_plained(diff)
elif output_format == 'json':
return json_format
return get_jsoned(diff)
else:
raise ValueError(
f'Invalid output format "{output_format}". '
Expand Down
2 changes: 1 addition & 1 deletion gendiff/gendiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
def generate_diff(filepath1, filepath2, output_format='stylish'):
config1 = read_file_data(filepath1)
config2 = read_file_data(filepath2)
return determine_output_file_format(output_format)(get_diff(config1, config2))
return determine_output_file_format(get_diff(config1, config2), output_format)
35 changes: 11 additions & 24 deletions gendiff/parser.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
import argparse
import json
import yaml


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', default='stylish',
choices=['stylish', 'plain', 'json'],
help='set format of output (default: \'stylish\')',
)
return parser.parse_args()


def get_file_format(filepath):
file = open(filepath)
if filepath.endswith('.json'):
return 'json'
return file, 'json'
elif filepath.endswith('.yml') or filepath.endswith('.yaml'):
return 'yaml'
return file, 'yaml'
else:
file.close()
raise TypeError("Invalid file format!")


def read_file_data(filepath):
with open(filepath) as file:
if get_file_format(filepath) == 'json':
return json.load(file)
elif get_file_format(filepath) == 'yaml':
return yaml.safe_load(file)
else:
raise TypeError("Invalid file format!")
file, file_format = get_file_format(filepath)
if file_format == 'json':
return json.load(file)
elif file_format == 'yaml':
return yaml.safe_load(file)
else:
raise TypeError("Invalid file format!")
2 changes: 1 addition & 1 deletion gendiff/scripts/gendiff.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python3
from gendiff.gendiff import generate_diff
from gendiff.parser import parse_file
from gendiff.arg_parser import parse_file


def main():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_json.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from gendiff.formatters.json import json_format
from gendiff.formatters.json import get_jsoned


def test_json_format():
Expand All @@ -11,4 +11,4 @@ def test_json_format():
}
}
expected_output = json.dumps(data, indent=2)
assert json_format(data) == expected_output
assert get_jsoned(data) == expected_output

0 comments on commit 4df4fd7

Please sign in to comment.