Skip to content

Commit 89cace2

Browse files
committed
feat: sixth step, the ability to compare files .yaml
1 parent 8eac88b commit 89cace2

File tree

10 files changed

+92
-39
lines changed

10 files changed

+92
-39
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ check: test lint
2424
build:
2525
uv build
2626

27-
file-test:
27+
file-test-json:
2828
uv run gendiff /home/zk/python-project-50/tests/test_data/file1.json /home/zk/python-project-50/tests/test_data/file2.json
2929

30-
full-check: test-coverage-without-xml file-test lint
30+
file-test-yaml:
31+
uv run gendiff /home/zk/python-project-50/tests/test_data/file1.yaml /home/zk/python-project-50/tests/test_data/file2.yaml
32+
33+
full-check: test-coverage-without-xml file-test-json lint
3134

3235
.PHONY: install test lint selfcheck check build
3336

gendiff/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from gendiff.cli import parse_args
2-
from gendiff.gendiff import generate_diff, main, read_file_json
2+
from gendiff.gendiff import generate_diff, main, read_file
33

44
__all__ = [
55
'parse_args',
66
'generate_diff',
7-
'read_file_json',
7+
'read_file',
88
'main',
99
]

gendiff/gendiff.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22

3+
import yaml
4+
35
from gendiff.cli import parse_args
46

57

@@ -8,13 +10,18 @@ def main() -> None:
810
print(generate_diff(args.first_file, args.second_file))
911

1012

11-
def read_file_json(path: str):
13+
def read_file(path: str):
14+
if path.endswith('.json'):
15+
load_data = json.load
16+
elif path.endswith(('.yaml', '.yml')):
17+
load_data = yaml.safe_load
18+
else:
19+
raise ValueError(f"ERROR: Unsupported format of file {path}.")
1220
try:
1321
with open(path, mode='r', encoding='utf-8') as file:
14-
return json.load(file)
15-
except Exception as error:
16-
print("Can't read_file_json\n", error)
17-
return None
22+
return load_data(file)
23+
except OSError as error:
24+
raise OSError(f"ERROR: Can't read file {path}. Reason: {error}")
1825

1926

2027
def get_list_of_dict_with_sign(data1, data2) -> list:
@@ -33,15 +40,10 @@ def get_list_of_dict_with_sign(data1, data2) -> list:
3340

3441

3542
def sort_list(items: list):
36-
def sort_by_rule(item: dict) -> str:
37-
"""The sign is changed to correctly sort items with the same key."""
38-
if item['sign'] == '+':
39-
sign = '-'
40-
elif item['sign'] == '-':
41-
sign = '+'
42-
else:
43-
sign = ' '
44-
return str([item['key'], sign])
43+
def sort_by_rule(item: dict) -> tuple:
44+
"""The sign -> digit for correctly sort items with the same key."""
45+
sign_order = {'-': 0, '+': 1, ' ': 2}
46+
return (item['key'], sign_order[item['sign']])
4547
items.sort(key=sort_by_rule)
4648
return items
4749

@@ -50,6 +52,7 @@ def make_str_from_list(items: list) -> str:
5052
"""
5153
Type checking for the output of strings without quotes,
5254
and for the correct output of True, False in the form of true, false.
55+
Doesn't matter for .yaml.
5356
"""
5457
list_of_str = ['{']
5558
for item in items:
@@ -71,7 +74,8 @@ def make_str_from_list(items: list) -> str:
7174

7275

7376
def generate_diff(path1, path2) -> str:
74-
data1 = read_file_json(path1)
75-
data2 = read_file_json(path2)
76-
sorted_list_of_dict = sort_list(get_list_of_dict_with_sign(data1, data2))
77-
return make_str_from_list(sorted_list_of_dict)
77+
dict_data1 = read_file(path1)
78+
dict_data2 = read_file(path2)
79+
list_of_dict = get_list_of_dict_with_sign(dict_data1, dict_data2)
80+
sorted_list = sort_list(list_of_dict)
81+
return make_str_from_list(sorted_list)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ readme = "README.md"
99
requires-python = ">=3.12"
1010
dependencies = [
1111
"argparse>=1.4.0",
12+
"pyyaml>=6.0.2",
1213
]
1314

1415
[dependency-groups]

tests/test_data/file1.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,3 @@
44
"proxy": "123.234.53.22",
55
"follow": false
66
}
7-

tests/test_data/file1.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
host: hexlet.io
2+
timeout: 50
3+
proxy: 123.234.53.22
4+
follow: false

tests/test_data/file2.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,3 @@
33
"verbose": true,
44
"host": "hexlet.io"
55
}
6-

tests/test_data/file2.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
timeout: 20
2+
verbose: true
3+
host: hexlet.io

tests/test_gendiff.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
import os
22

3-
from gendiff.gendiff import generate_diff, read_file_json, sort_list
3+
from gendiff.gendiff import generate_diff, read_file, sort_list
44

55
current_dir = os.path.dirname(os.path.abspath(__file__))
66
dir_with_data = os.path.join(current_dir, 'test_data')
77

8-
data1 = {
8+
right_data1 = {
99
"host": "hexlet.io",
1010
"timeout": 50,
1111
"proxy": "123.234.53.22",
1212
"follow": False
1313
}
1414

15-
data2 = {
15+
right_data2 = {
1616
"timeout": 20,
1717
"verbose": True,
1818
"host": "hexlet.io"
1919
}
2020

2121

22-
def test_read_file_json():
23-
file1_path = os.path.join(dir_with_data, 'file1.json')
24-
file2_path = os.path.join(dir_with_data, 'file2.json')
25-
assert read_file_json(file1_path) == data1
26-
assert read_file_json(file1_path) != data2
27-
assert read_file_json(file2_path) == data2
28-
assert read_file_json(file2_path) != data1
22+
def test_read_file():
23+
file1_path_json = os.path.join(dir_with_data, 'file1.json')
24+
file2_path_json = os.path.join(dir_with_data, 'file2.json')
25+
file1_path_yaml = os.path.join(dir_with_data, 'file1.yaml')
26+
file2_path_yaml = os.path.join(dir_with_data, 'file2.yaml')
27+
assert read_file(file1_path_json) == right_data1
28+
assert read_file(file1_path_json) != right_data2
29+
assert read_file(file2_path_json) == right_data2
30+
assert read_file(file2_path_json) != right_data1
31+
assert read_file(file1_path_yaml) == right_data1
32+
assert read_file(file1_path_yaml) != right_data2
33+
assert read_file(file2_path_yaml) == right_data2
34+
assert read_file(file2_path_yaml) != right_data1
2935

3036

3137
def test_sort_list():
@@ -56,7 +62,11 @@ def test_generate_diff():
5662
" + verbose: true\n"
5763
"}")
5864
wrong_str = ''
59-
file1_path = os.path.join(dir_with_data, 'file1.json')
60-
file2_path = os.path.join(dir_with_data, 'file2.json')
61-
assert generate_diff(file1_path, file2_path) == right_str
62-
assert generate_diff(file1_path, file2_path) != wrong_str
65+
file1_path_json = os.path.join(dir_with_data, 'file1.json')
66+
file2_path_json = os.path.join(dir_with_data, 'file2.json')
67+
file1_path_yaml = os.path.join(dir_with_data, 'file1.yaml')
68+
file2_path_yaml = os.path.join(dir_with_data, 'file2.yaml')
69+
assert generate_diff(file1_path_json, file2_path_json) == right_str
70+
assert generate_diff(file1_path_json, file2_path_json) != wrong_str
71+
assert generate_diff(file1_path_yaml, file2_path_yaml) == right_str
72+
assert generate_diff(file1_path_yaml, file2_path_yaml) != wrong_str

uv.lock

Lines changed: 31 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)