Skip to content

Commit 2386a45

Browse files
committed
add: eighth step, add io_utils.py
1 parent 8c6003f commit 2386a45

File tree

10 files changed

+203
-113
lines changed

10 files changed

+203
-113
lines changed

gendiff/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from gendiff.cli import parse_args
2-
from gendiff.gendiff import generate_diff, main, read_file
2+
from gendiff.gendiff import generate_diff, main
3+
from gendiff.io_utils import read_file
34

45
__all__ = [
56
'parse_args',

gendiff/gendiff.py

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
1-
import json
2-
3-
import yaml
4-
51
from gendiff.cli import parse_args
2+
from gendiff.io_utils import read_file
3+
from gendiff.views.json_format import format_output_json
64
from gendiff.views.plain import format_output_plain
75
from gendiff.views.stylish import format_output_stylish
86

97

10-
def read_file(path: str):
11-
try:
12-
if path.endswith('.json'):
13-
load_data = json.load
14-
elif path.endswith(('.yaml', '.yml')):
15-
load_data = yaml.safe_load
16-
else:
17-
raise ValueError
18-
19-
with open(path, mode='r', encoding='utf-8') as file:
20-
return load_data(file)
21-
except OSError as error:
22-
print(f"ERROR: Can't read file {path}. Reason: {error}")
23-
return None
24-
except ValueError:
25-
print(f"ERROR: Unsupported format of file {path}.")
26-
return None
27-
28-
298
def get_diff(data1, data2) -> list:
309
if data1 == data2:
3110
return data1
@@ -72,10 +51,15 @@ def generate_diff(path1, path2, format_name='stylish') -> str:
7251
dict_data1 = read_file(path1)
7352
dict_data2 = read_file(path2)
7453
sorted_diff = get_diff(dict_data1, dict_data2)
75-
if format_name == 'stylish':
76-
return format_output_stylish(sorted_diff)
77-
elif format_name == 'plain':
78-
return format_output_plain(sorted_diff)
54+
match format_name:
55+
case 'stylish':
56+
return format_output_stylish(sorted_diff)
57+
case 'plain':
58+
return format_output_plain(sorted_diff)
59+
case 'json':
60+
return format_output_json(sorted_diff)
61+
case _:
62+
print("Wrong format: {format_name}")
7963

8064

8165
def main() -> None:

gendiff/io_utils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import json
2+
3+
import yaml
4+
5+
6+
def read_file(path: str):
7+
try:
8+
if path.endswith('.json'):
9+
load_data = json.load
10+
elif path.endswith(('.yaml', '.yml')):
11+
load_data = yaml.safe_load
12+
else:
13+
raise ValueError
14+
15+
with open(path, mode='r', encoding='utf-8') as file:
16+
return load_data(file)
17+
except OSError as error:
18+
print(f"ERROR: Can't read file {path}. Reason: {error}")
19+
return None
20+
except ValueError:
21+
print(f"ERROR: Unsupported format of file {path}.")
22+
return None

gendiff/views/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
from gendiff.views.json_format import format_output_json
2+
from gendiff.views.plain import format_output_plain
13
from gendiff.views.stylish import format_output_stylish
24

35
__all__ = [
4-
'format_output_stylish'
6+
'format_output_stylish',
7+
'format_output_json',
8+
'format_output_plain'
59
]

gendiff/views/json_format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def format_output_json(diff: list):
2+
pass

tests/test_data/test_result.txt

Lines changed: 6 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,8 @@
11
{
2-
common: {
3-
+ follow: false
4-
setting1: Value 1
5-
- setting2: 200
6-
- setting3: true
7-
+ setting3: null
8-
+ setting4: blah blah
9-
+ setting5: {
10-
key5: value5
11-
}
12-
setting6: {
13-
doge: {
14-
- wow:
15-
+ wow: so much
16-
}
17-
key: value
18-
+ ops: vops
19-
}
20-
}
21-
group1: {
22-
- baz: bas
23-
+ baz: bars
24-
foo: bar
25-
- nest: {
26-
key: value
27-
}
28-
+ nest: str
29-
}
30-
- group2: {
31-
abc: 12345
32-
deep: {
33-
id: 45
34-
}
35-
}
36-
+ group3: {
37-
deep: {
38-
id: {
39-
number: 45
40-
}
41-
}
42-
fee: 100500
43-
}
2+
- follow: false
3+
host: hexlet.io
4+
- proxy: 123.234.53.22
5+
- timeout: 50
6+
+ timeout: 20
7+
+ verbose: true
448
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"common": {
3+
"follow": false,
4+
"setting1": "Value 1",
5+
"setting2": 200,
6+
"setting3": null,
7+
"setting4": "blah blah",
8+
"setting5": {
9+
"key5": "value5"
10+
},
11+
"setting6": {
12+
"doge": {
13+
"wow": "so much"
14+
},
15+
"key": "value",
16+
"ops": "vops"
17+
}
18+
},
19+
"group1": {
20+
"baz": "bars",
21+
"foo": "bar",
22+
"nest": "str"
23+
},
24+
"group2": {
25+
"abc": 12345,
26+
"deep": {
27+
"id": 45
28+
}
29+
},
30+
"group3": {
31+
"deep": {
32+
"id": {
33+
"number": 45
34+
}
35+
},
36+
"fee": 100500
37+
}
38+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"common": {
3+
"follow": false,
4+
"setting1": "Value 1",
5+
"setting2": 200,
6+
"setting3": true,
7+
"setting3": null,
8+
"setting4": "blah blah",
9+
"setting5": {
10+
"key5": "value5"
11+
},
12+
"setting6": {
13+
"doge": {
14+
"wow": "",
15+
"wow": "so much"
16+
},
17+
"key": "value",
18+
"ops": "vops"
19+
}
20+
},
21+
"group1": {
22+
"baz": "bas",
23+
"baz": "bars",
24+
"foo": "bar",
25+
"nest": {
26+
"key": "value"
27+
},
28+
"nest": "str"
29+
},
30+
"group2": {
31+
"abc": 12345,
32+
"deep": {
33+
"id": 45
34+
}
35+
},
36+
"group3": {
37+
"deep": {
38+
"id": {
39+
"number": 45
40+
}
41+
},
42+
"fee": 100500
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
common: {
3+
+ follow: false
4+
setting1: Value 1
5+
- setting2: 200
6+
- setting3: true
7+
+ setting3: null
8+
+ setting4: blah blah
9+
+ setting5: {
10+
key5: value5
11+
}
12+
setting6: {
13+
doge: {
14+
- wow:
15+
+ wow: so much
16+
}
17+
key: value
18+
+ ops: vops
19+
}
20+
}
21+
group1: {
22+
- baz: bas
23+
+ baz: bars
24+
foo: bar
25+
- nest: {
26+
key: value
27+
}
28+
+ nest: str
29+
}
30+
- group2: {
31+
abc: 12345
32+
deep: {
33+
id: 45
34+
}
35+
}
36+
+ group3: {
37+
deep: {
38+
id: {
39+
number: 45
40+
}
41+
}
42+
fee: 100500
43+
}
44+
}

tests/test_gendiff.py

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22

3-
from gendiff.gendiff import generate_diff, read_file
3+
from gendiff.gendiff import generate_diff
4+
from gendiff.io_utils import read_file
45

56
current_dir = os.path.dirname(os.path.abspath(__file__))
67
dir_with_data = os.path.join(current_dir, 'test_data')
@@ -20,66 +21,52 @@
2021

2122

2223
def test_read_file():
23-
file1_json = os.path.join(dir_with_data, 'file1.json')
24-
file2_json = os.path.join(dir_with_data, 'file2.json')
25-
file1_yaml = os.path.join(dir_with_data, 'file1.yaml')
26-
file2_yaml = os.path.join(dir_with_data, 'file2.yaml')
27-
assert read_file(file1_json) == right_data1
28-
assert read_file(file1_json) != right_data2
29-
assert read_file(file2_json) == right_data2
30-
assert read_file(file2_json) != right_data1
31-
assert read_file(file1_yaml) == right_data1
32-
assert read_file(file1_yaml) != right_data2
33-
assert read_file(file2_yaml) == right_data2
34-
assert read_file(file2_yaml) != right_data1
24+
file1_json_path = os.path.join(dir_with_data, 'file1.json')
25+
file2_json_path = os.path.join(dir_with_data, 'file2.json')
26+
file1_yaml_path = os.path.join(dir_with_data, 'file1.yaml')
27+
file2_yaml_path = os.path.join(dir_with_data, 'file2.yaml')
3528

29+
assert read_file(file1_json_path) == right_data1
30+
assert read_file(file1_json_path) != right_data2
31+
assert read_file(file2_json_path) == right_data2
32+
assert read_file(file2_json_path) != right_data1
33+
assert read_file(file1_yaml_path) == right_data1
34+
assert read_file(file1_yaml_path) != right_data2
35+
assert read_file(file2_yaml_path) == right_data2
36+
assert read_file(file2_yaml_path) != right_data1
3637

37-
def test_generate_diff():
38-
right_str = ("{\n"
39-
" - follow: false\n"
40-
" host: hexlet.io\n"
41-
" - proxy: 123.234.53.22\n"
42-
" - timeout: 50\n"
43-
" + timeout: 20\n"
44-
" + verbose: true\n"
45-
"}")
46-
test_file_path = os.path.join(dir_with_data, 'test_result.txt')
47-
try:
48-
with open(test_file_path, 'r', encoding='utf-8') as file:
49-
right_data = file.read()
50-
except OSError:
51-
print('Can not open test_file.')
5238

53-
test_file_path = os.path.join(dir_with_data, 'test_result_plain.txt')
39+
def open_file(file_name: str) -> dict:
40+
test_file_path = os.path.join(dir_with_data, file_name)
5441
try:
5542
with open(test_file_path, 'r', encoding='utf-8') as file:
56-
right_data_plain = file.read()
43+
return file.read()
5744
except OSError:
5845
print('Can not open test_file.')
5946

60-
wrong_str = ''
61-
file1_json = os.path.join(dir_with_data, 'file1.json')
62-
file2_json = os.path.join(dir_with_data, 'file2.json')
63-
file1_yaml = os.path.join(dir_with_data, 'file1.yaml')
64-
file2_yaml = os.path.join(dir_with_data, 'file2.yaml')
6547

48+
def test_generate_diff():
49+
right_data_stylish = open_file('test_result_stylish.txt')
50+
right_data_plain = open_file('test_result_plain.txt')
51+
# right_data_json = open_file('test_result_json.txt')
52+
wrong_str = ''
53+
6654
file3_json = os.path.join(dir_with_data, 'file3.json')
6755
file4_json = os.path.join(dir_with_data, 'file4.json')
6856
file3_yaml = os.path.join(dir_with_data, 'file3.yaml')
6957
file4_yaml = os.path.join(dir_with_data, 'file4.yaml')
7058

71-
assert generate_diff(file1_json, file2_json) == right_str
72-
assert generate_diff(file1_json, file2_json) != wrong_str
73-
assert generate_diff(file1_yaml, file2_yaml) == right_str
74-
assert generate_diff(file1_yaml, file2_yaml) != wrong_str
75-
76-
assert generate_diff(file3_json, file4_json) == right_data
59+
assert generate_diff(file3_json, file4_json) == right_data_stylish
7760
assert generate_diff(file3_json, file4_json) != wrong_str
78-
assert generate_diff(file3_yaml, file4_yaml) == right_data
61+
assert generate_diff(file3_yaml, file4_yaml) == right_data_stylish
7962
assert generate_diff(file3_yaml, file4_yaml) != wrong_str
8063

8164
assert generate_diff(file3_json, file4_json, 'plain') == right_data_plain
8265
assert generate_diff(file3_json, file4_json, 'plain') != wrong_str
8366
assert generate_diff(file3_yaml, file4_yaml, 'plain') == right_data_plain
8467
assert generate_diff(file3_yaml, file4_yaml, 'plain') != wrong_str
8568

69+
# assert generate_diff(file3_json, file4_json, 'json') == right_data_json
70+
# assert generate_diff(file3_json, file4_json, 'json') != wrong_str
71+
# assert generate_diff(file3_yaml, file4_yaml, 'json') == right_data_json
72+
# assert generate_diff(file3_yaml, file4_yaml, 'json') != wrong_str

0 commit comments

Comments
 (0)