Skip to content

Commit 0aa429e

Browse files
committed
fix: exception when open file
1 parent 3fcd396 commit 0aa429e

File tree

6 files changed

+158
-7
lines changed

6 files changed

+158
-7
lines changed

gendiff/gendiff.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,22 @@ def main() -> None:
1111

1212

1313
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}.")
2014
try:
15+
if path.endswith('.json'):
16+
load_data = json.load
17+
elif path.endswith(('.yaml', '.yml')):
18+
load_data = yaml.safe_load
19+
else:
20+
raise ValueError
21+
2122
with open(path, mode='r', encoding='utf-8') as file:
2223
return load_data(file)
2324
except OSError as error:
24-
raise OSError(f"ERROR: Can't read file {path}. Reason: {error}")
25+
print(f"ERROR: Can't read file {path}. Reason: {error}")
26+
return None
27+
except ValueError:
28+
print(f"ERROR: Unsupported format of file {path}.")
29+
return None
2530

2631

2732
def get_list_of_dict_with_sign(data1, data2) -> list:
@@ -73,6 +78,13 @@ def make_str_from_list(items: list) -> str:
7378
return '\n'.join(list_of_str)
7479

7580

81+
# def check_the_nesting(list_of_dict: list) -> list:
82+
# for item in list_of_dict:
83+
# value = item['value']
84+
# if isinstance(value, dict):
85+
# get_list_of_dict_with_sign(value)
86+
87+
7688
def generate_diff(path1, path2) -> str:
7789
dict_data1 = read_file(path1)
7890
dict_data2 = read_file(path2)

tests/test_data/file3.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"common": {
3+
"setting1": "Value 1",
4+
"setting2": 200,
5+
"setting3": true,
6+
"setting6": {
7+
"key": "value",
8+
"doge": {
9+
"wow": ""
10+
}
11+
}
12+
},
13+
"group1": {
14+
"baz": "bas",
15+
"foo": "bar",
16+
"nest": {
17+
"key": "value"
18+
}
19+
},
20+
"group2": {
21+
"abc": 12345,
22+
"deep": {
23+
"id": 45
24+
}
25+
}
26+
}

tests/test_data/file3.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
common:
2+
setting1: Value 1
3+
setting2: 200
4+
setting3: true
5+
setting6:
6+
key: value
7+
doge:
8+
wow: ''
9+
group1:
10+
baz: bas
11+
foo: bar
12+
nest:
13+
key: value
14+
group2:
15+
abc: 12345
16+
deep:
17+
id: 45

tests/test_data/file4.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"common": {
3+
"follow": false,
4+
"setting1": "Value 1",
5+
"setting3": null,
6+
"setting4": "blah blah",
7+
"setting5": {
8+
"key5": "value5"
9+
},
10+
"setting6": {
11+
"key": "value",
12+
"ops": "vops",
13+
"doge": {
14+
"wow": "so much"
15+
}
16+
}
17+
},
18+
"group1": {
19+
"foo": "bar",
20+
"baz": "bars",
21+
"nest": "str"
22+
},
23+
"group3": {
24+
"deep": {
25+
"id": {
26+
"number": 45
27+
}
28+
},
29+
"fee": 100500
30+
}
31+
}

tests/test_data/file4.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
common:
2+
follow: false
3+
setting1: Value 1
4+
setting3: null
5+
setting4: blah blah
6+
setting5:
7+
key5: value5
8+
setting6:
9+
key: value
10+
ops: vops
11+
doge:
12+
wow: so much
13+
group1:
14+
foo: bar
15+
baz: bars
16+
nest: str
17+
group3:
18+
deep:
19+
id:
20+
number: 45
21+
fee: 100500

tests/test_data/test_result.txt

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+
}

0 commit comments

Comments
 (0)