33import yaml
44
55from gendiff .cli import parse_args
6+ from gendiff .views import make_str_from_list
67
78
89def 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-
7528def 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
12881def 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 ))
0 commit comments