Skip to content

Commit ad6f021

Browse files
committed
add plain format_name
1 parent 55f8c8e commit ad6f021

File tree

6 files changed

+71
-1
lines changed

6 files changed

+71
-1
lines changed

gendiff/gen_diff.py

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

3+
from gendiff.gen_diff_plain import gen_diff_plain
34
from gendiff.gen_diff_stylish import gen_diff_stylish
45
from gendiff.pars_yaml import open_ymlf
56

@@ -43,3 +44,5 @@ def generate_diff(file1, file2, format_name='stylish'):
4344
match format_name:
4445
case 'stylish':
4546
return gen_diff_stylish(diff)
47+
case 'plain':
48+
return gen_diff_plain(diff)

gendiff/gen_diff_plain.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
def stringify_plain(value):
2+
if value is True:
3+
result = 'true'
4+
elif value is None:
5+
result = 'null'
6+
elif value is False:
7+
result = 'false'
8+
else:
9+
result = f"'{value}'"
10+
return result
11+
12+
13+
def gen_diff_plain(value, lvl=''):
14+
res = ''
15+
for el, val in value.items():
16+
match val[0]:
17+
case 'nested':
18+
19+
res += gen_diff_plain(val[1], lvl + f'{el}.')
20+
case 'added':
21+
if isinstance(val[1], dict):
22+
res += (f"Property '{lvl + el}'"
23+
f" was added with value: [complex value]" + '\n')
24+
else:
25+
res += (f"Property '{lvl + el}'"
26+
f" was added with value:"
27+
f" {stringify_plain(val[1])}") + '\n'
28+
case 'removed':
29+
res += f"Property '{lvl + el}' was removed" + '\n'
30+
case 'changed':
31+
if isinstance(val[1], dict):
32+
res += (f"Property '{lvl + el}'"
33+
f" was updated. From [complex value]"
34+
f" to {stringify_plain(val[2])}") + '\n'
35+
elif isinstance(val[2], dict):
36+
res += (f"Property '{lvl + el}'"
37+
f" was updated. From {stringify_plain(val[1])}"
38+
f" to [complex value]") + '\n'
39+
else:
40+
res += (f"Property '{lvl + el}'"
41+
f" was updated. From {stringify_plain(val[1])}"
42+
f" to {stringify_plain(val[2])}") + '\n'
43+
return res
76 Bytes
Binary file not shown.

gendiff/scripts/gendiff.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def main():
1313
parser.add_argument('second_file')
1414
parser.add_argument('-f', '--format', help='set format of output')
1515
diff = generate_diff(parser.parse_args().first_file,
16-
parser.parse_args().second_file)
16+
parser.parse_args().second_file,
17+
parser.parse_args().format)
1718
return diff
1819

1920

tests/test_data/result_plain.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Property 'common.follow' was added with value: false
2+
Property 'common.setting2' was removed
3+
Property 'common.setting3' was updated. From true to null
4+
Property 'common.setting4' was added with value: 'blah blah'
5+
Property 'common.setting5' was added with value: [complex value]
6+
Property 'common.setting6.doge.wow' was updated. From '' to 'so much'
7+
Property 'common.setting6.ops' was added with value: 'vops'
8+
Property 'group1.baz' was updated. From 'bas' to 'bars'
9+
Property 'group1.nest' was updated. From [complex value] to 'str'
10+
Property 'group2' was removed
11+
Property 'group3' was added with value: [complex value]

tests/test_generate_diff_plain.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from gendiff.gen_diff import generate_diff
2+
from tests.test_generate_diff import get_test_data_path, read_file
3+
4+
5+
def test_generate_diff():
6+
result = read_file('result_plain.txt')
7+
actual = generate_diff(str(get_test_data_path('file11.json')),
8+
str(get_test_data_path('file22.json')), 'plain')
9+
yam_act = generate_diff(str(get_test_data_path('file11.yml')),
10+
str(get_test_data_path('file22.yml')), 'plain')
11+
assert actual == result
12+
assert yam_act == result

0 commit comments

Comments
 (0)