Skip to content

Commit 31c99c4

Browse files
committed
stylish add
1 parent f6e8b59 commit 31c99c4

File tree

3 files changed

+68
-43
lines changed

3 files changed

+68
-43
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
[![Maintainability](https://api.codeclimate.com/v1/badges/88675eaf4e4ca1e04a88/maintainability)](https://codeclimate.com/github/ttehasi/python-project-50/maintainability)
66
[![Test Coverage](https://api.codeclimate.com/v1/badges/88675eaf4e4ca1e04a88/test_coverage)](https://codeclimate.com/github/ttehasi/python-project-50/test_coverage)
77

8-
## Asciinema JSON file:
8+
## Asciinema JSON file (default usage):
99

1010
[![asciicast](https://asciinema.org/a/djCfDR2K0qTnGeiqy4hVWhLO9.svg)](https://asciinema.org/a/djCfDR2K0qTnGeiqy4hVWhLO9)
1111

1212
[![asciicast](https://asciinema.org/a/mkiLw1Llc4brT2wrSHaMW7H4T.svg)](https://asciinema.org/a/mkiLw1Llc4brT2wrSHaMW7H4T)
1313

14-
## Asciinema YAML file:
14+
## Asciinema YAML file (default usage):
1515

1616
[![asciicast](https://asciinema.org/a/H918cvPLQohe9DMY2NDxLKcaU.svg)](https://asciinema.org/a/H918cvPLQohe9DMY2NDxLKcaU)
1717

gendiff/gen_diff.py

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

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

56

@@ -8,57 +9,37 @@ def open_f(path_to_file) -> dict:
89
return json.load(file)
910

1011

11-
def stringify(value, lvl=1):
12-
if isinstance(value, dict):
13-
result = '{\n'
14-
for el, val in value.items():
15-
result += f'{' ' * lvl}{el}: '
16-
result += stringify(val, lvl + 1) + '\n'
17-
result += ' ' * (lvl - 1) + '}'
18-
else:
19-
if value is True:
20-
result = 'true'
21-
elif value is None:
22-
result = 'null'
23-
elif value is False:
24-
result = 'false'
25-
else:
26-
result = str(value)
27-
return result
28-
29-
30-
def generate_diff(file1, file2, lvl=1, format_name='stylish'):
31-
if isinstance(file1, str) and isinstance(file2, str):
32-
if str(file1)[-5:] == '.json' and str(file2)[-5:] == '.json':
33-
file1 = open_f(file1)
34-
file2 = open_f(file2)
35-
else:
36-
file1 = open_ymlf(file1)
37-
file2 = open_ymlf(file2)
38-
result = ''
12+
def difference(file1, file2):
13+
result = {}
3914
if isinstance(file1, dict) and isinstance(file2, dict):
40-
result += '{\n'
4115
keys1 = sorted(list(file1.keys()))
4216
keys2 = sorted(list(file2.keys()))
4317
keys12 = sorted(set(keys1 + keys2))
4418
for i in keys12:
4519
if i in keys1 and i in keys2:
4620
if isinstance(file1[i], dict) and isinstance(file2[i], dict):
47-
result += f'{' ' * lvl}{i}: '
48-
result += generate_diff(file1[i], file2[i], lvl + 1) + '\n'
21+
result[i] = ('nested', generate_diff(file1[i], file2[i]))
4922
else:
5023
if file1[i] == file2[i]:
51-
result += f'{' ' * lvl}{i}: {file1[i]}' + '\n'
24+
result[i] = ('unchanged', file1[i])
5225
else:
53-
result += (f'{' ' * (lvl - 1) + ' - '}{i}: '
54-
f'{stringify(file1[i], lvl=lvl + 1)}') + '\n'
55-
result += (f'{' ' * (lvl - 1) + ' + '}{i}: '
56-
f'{stringify(file2[i], lvl=lvl + 1)}') + '\n'
26+
result[i] = ('changed', file1[i], file2[i])
5727
if (i in keys1) and (i not in keys2):
58-
result += (f'{' ' * (lvl - 1) + ' - '}{i}: '
59-
f'{stringify(file1[i], lvl=lvl + 1)}') + '\n'
28+
result[i] = ('removed', file1[i])
6029
if (i not in keys1) and (i in keys2):
61-
result += (f'{' ' * (lvl - 1) + ' + '}{i}: '
62-
f'{stringify(file2[i], lvl=lvl + 1)}') + '\n'
63-
result += ' ' * (lvl - 1) + '}'
30+
result[i] = ('added', file2[i])
6431
return result
32+
33+
34+
def generate_diff(file1, file2, format_name='stylish'):
35+
if isinstance(file1, str) and isinstance(file2, str):
36+
if str(file1)[-5:] == '.json' and str(file2)[-5:] == '.json':
37+
file1 = open_f(file1)
38+
file2 = open_f(file2)
39+
else:
40+
file1 = open_ymlf(file1)
41+
file2 = open_ymlf(file2)
42+
diff = difference(file1, file2)
43+
match format_name:
44+
case 'stylish':
45+
return gen_diff_stylish(diff)

gendiff/gen_diff_stylish.py

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

0 commit comments

Comments
 (0)