Skip to content

Commit 23d471c

Browse files
committed
add: compare trees
1 parent 0c0661a commit 23d471c

File tree

4 files changed

+87
-59
lines changed

4 files changed

+87
-59
lines changed

gendiff/cli.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ def parse_args(args=None):
88
epilog='Hi, Lyova!)'
99
)
1010

11-
parser.add_argument('-f', '--format', help='set format of output')
11+
parser.add_argument(
12+
'-f', '--format', type=str,
13+
default='stylish',
14+
help='set format of output'
15+
)
1216
parser.add_argument('first_file')
1317
parser.add_argument('second_file')
1418

gendiff/gendiff.py

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import yaml
44

55
from gendiff.cli import parse_args
6+
from gendiff.views import make_str_from_list
67

78

89
def read_file(path: str):
@@ -24,54 +25,6 @@ def read_file(path: str):
2425
return None
2526

2627

27-
def make_str_from_dict(items: dict, enclos=0) -> str:
28-
indent = ' ' * enclos
29-
list_of_str = ['{']
30-
for key, value in items.items():
31-
if not isinstance(key, str):
32-
key = json.dumps(key)
33-
34-
if isinstance(value, dict):
35-
value = make_str_from_dict(value, enclos + 1)
36-
37-
if not isinstance(value, str):
38-
value = json.dumps(value)
39-
40-
list_of_str.append(f"{indent} {key}: {value}")
41-
list_of_str.append(f"{indent}}}")
42-
return '\n'.join(list_of_str)
43-
44-
45-
def make_str_from_list(items: list, enclos=0) -> str:
46-
"""
47-
Type checking for the output of strings without quotes,
48-
and for the correct output of True, False in the form of true, false.
49-
Doesn't matter for .yaml.
50-
"""
51-
indent = ' ' * enclos
52-
list_of_str = ['{']
53-
for item in items:
54-
sign = item['sign']
55-
56-
if isinstance(item['key'], str):
57-
key = item['key']
58-
else:
59-
key = json.dumps(item['key'])
60-
61-
if isinstance(item['value'], str):
62-
value = item['value']
63-
elif isinstance(item['value'], list):
64-
value = make_str_from_list(item['value'], enclos + 1)
65-
elif isinstance(item['value'], dict):
66-
value = make_str_from_dict(item['value'], enclos + 1)
67-
else:
68-
value = json.dumps(item['value'])
69-
70-
list_of_str.append(f"{indent} {sign} {key}: {value}")
71-
list_of_str.append(f"{indent}}}")
72-
return '\n'.join(list_of_str)
73-
74-
7528
def sort_list(items: list):
7629
def sort_by_rule(item: dict) -> tuple:
7730
"""The sign -> digit for correctly sort items with the same key."""
@@ -118,13 +71,13 @@ def get_list_of_dict(data1, data2) -> list:
11871
return sort_list(result)
11972

12073

121-
def generate_diff(path1, path2) -> str:
74+
def generate_diff(path1, path2, format_name='stylish') -> str:
12275
dict_data1 = read_file(path1)
12376
dict_data2 = read_file(path2)
12477
sorted_list_of_dict = get_list_of_dict(dict_data1, dict_data2)
125-
return make_str_from_list(sorted_list_of_dict)
78+
return make_str_from_list(sorted_list_of_dict, format_name)
12679

12780

12881
def main() -> None:
12982
args = parse_args()
130-
print(generate_diff(args.first_file, args.second_file))
83+
print(generate_diff(args.first_file, args.second_file, args.format))

gendiff/views.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import json
2+
3+
4+
def make_str_from_dict(items: dict, enclose=0) -> str:
5+
indent = ' ' * enclose
6+
list_of_str = ['{']
7+
for key, value in items.items():
8+
if not isinstance(key, str):
9+
key = json.dumps(key)
10+
11+
if isinstance(value, dict):
12+
value = make_str_from_dict(value, enclose + 1)
13+
14+
if not isinstance(value, str):
15+
value = json.dumps(value)
16+
17+
list_of_str.append(f"{indent} {key}: {value}")
18+
list_of_str.append(f"{indent}}}")
19+
return '\n'.join(list_of_str)
20+
21+
22+
def make_str_from_list(items: list, format_name, enclose=0) -> str:
23+
"""
24+
Type checking for the output of strings without quotes,
25+
and for the correct output of True, False in the form of true, false.
26+
Doesn't matter for .yaml.
27+
"""
28+
if format_name != 'stylish':
29+
return "format {format_name} unsupported"
30+
indent = ' ' * enclose
31+
list_of_str = ['{']
32+
for item in items:
33+
sign = item['sign']
34+
35+
if isinstance(item['key'], str):
36+
key = item['key']
37+
else:
38+
key = json.dumps(item['key'])
39+
40+
if isinstance(item['value'], str):
41+
value = item['value']
42+
elif isinstance(item['value'], list):
43+
value = make_str_from_list(item['value'], format_name, enclose + 1)
44+
elif isinstance(item['value'], dict):
45+
value = make_str_from_dict(item['value'], enclose + 1)
46+
else:
47+
value = json.dumps(item['value'])
48+
49+
list_of_str.append(f"{indent} {sign} {key}: {value}")
50+
list_of_str.append(f"{indent}}}")
51+
return '\n'.join(list_of_str)

tests/test_gendiff.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,39 @@ def test_sort_list():
5454

5555
def test_generate_diff():
5656
right_str = ("{\n"
57-
" - follow: false\n"
58-
" host: hexlet.io\n"
59-
" - proxy: 123.234.53.22\n"
60-
" - timeout: 50\n"
61-
" + timeout: 20\n"
62-
" + verbose: true\n"
63-
"}")
57+
" - follow: false\n"
58+
" host: hexlet.io\n"
59+
" - proxy: 123.234.53.22\n"
60+
" - timeout: 50\n"
61+
" + timeout: 20\n"
62+
" + verbose: true\n"
63+
"}")
64+
test_file_path = os.path.join(dir_with_data, 'test_result.txt')
65+
try:
66+
with open(test_file_path, 'r', encoding='utf-8') as file:
67+
right_data = file.read()
68+
except OSError:
69+
print('Can not open test_file.')
70+
6471
wrong_str = ''
72+
print(right_data)
6573
file1_path_json = os.path.join(dir_with_data, 'file1.json')
6674
file2_path_json = os.path.join(dir_with_data, 'file2.json')
6775
file1_path_yaml = os.path.join(dir_with_data, 'file1.yaml')
6876
file2_path_yaml = os.path.join(dir_with_data, 'file2.yaml')
77+
78+
file3_path_json = os.path.join(dir_with_data, 'file3.json')
79+
file4_path_json = os.path.join(dir_with_data, 'file4.json')
80+
file3_path_yaml = os.path.join(dir_with_data, 'file3.yaml')
81+
file4_path_yaml = os.path.join(dir_with_data, 'file4.yaml')
82+
6983
assert generate_diff(file1_path_json, file2_path_json) == right_str
7084
assert generate_diff(file1_path_json, file2_path_json) != wrong_str
7185
assert generate_diff(file1_path_yaml, file2_path_yaml) == right_str
7286
assert generate_diff(file1_path_yaml, file2_path_yaml) != wrong_str
87+
88+
assert generate_diff(file3_path_json, file4_path_json) == right_data
89+
assert generate_diff(file3_path_json, file4_path_json) != wrong_str
90+
assert generate_diff(file3_path_yaml, file4_path_yaml) == right_data
91+
assert generate_diff(file3_path_yaml, file4_path_yaml) != wrong_str
92+

0 commit comments

Comments
 (0)