15
15
import typing
16
16
import pathlib
17
17
import shutil
18
+ import json
18
19
19
20
20
21
def normalize (sentence : str ) -> str :
@@ -182,34 +183,35 @@ def convert_to_bool(value: typing.Any) -> bool:
182
183
return False
183
184
184
185
185
- def read_setting (_setting_file_path : str , setting_name : typing . Any ) -> typing .Optional [ str ] :
186
+ def read_setting (_setting_file_path : str , setting_name : str = "" ) -> typing .Any :
186
187
if os .path .exists (_setting_file_path ) is False :
187
188
return
188
189
else :
189
- root = otfdlib .Otfd ()
190
- root .load (_setting_file_path )
191
- root .parse ()
192
- return root .get_value (setting_name )
190
+ with open (_setting_file_path , encoding = "utf-8_sig" , mode = "r" ) as f :
191
+ json_dictionary = json .load (f )
192
+ if setting_name :
193
+ return json_dictionary [setting_name ]
194
+ else :
195
+ return json_dictionary
193
196
194
197
195
- def write_setting (_setting_file_path : str , setting_name : typing . Any , setting_value : typing .Any ) -> None :
198
+ def write_setting (_setting_file_path : str , setting_name : str , setting_value : typing .Any ) -> None :
196
199
if os .path .exists (_setting_file_path ) is False :
197
200
with open (_setting_file_path , mode = "w" , encoding = "utf-8_sig" ) as f :
198
- f .write ("" )
199
- root = otfdlib .Otfd ()
200
- root .load (_setting_file_path )
201
- root .parse ()
202
- root .add (str (setting_name ), str (setting_value ))
203
- root .write ()
204
- return
201
+ json .dump ({}, f )
202
+ with open (_setting_file_path , encoding = "utf-8_sig" , mode = "r" ) as f :
203
+ json_dictionary = json .load (f )
204
+ json_dictionary [setting_name ] = setting_value
205
+ with open (_setting_file_path , encoding = "utf-8_sig" , mode = "w" ) as f :
206
+ json .dump (json_dictionary , f , indent = 4 )
205
207
206
208
207
- def read_flag (_flag_file_path : str , flag_name : typing . Any ) -> bool :
208
- return convert_to_bool ( read_setting (_flag_file_path , flag_name ) )
209
+ def read_flag (_flag_file_path : str , flag_name : str ) -> bool :
210
+ return read_setting (_flag_file_path , flag_name )
209
211
210
212
211
- def set_flag (_flag_file_path : str , flag_name : typing . Any , flag_value : typing . Any ) -> None :
212
- write_setting (_flag_file_path , str ( flag_name ), convert_to_bool ( flag_value ) )
213
+ def set_flag (_flag_file_path : str , flag_name : str , flag_value : bool ) -> None :
214
+ write_setting (_flag_file_path , flag_name , flag_value )
213
215
return
214
216
215
217
@@ -222,26 +224,15 @@ def solve_setting_conflict(default_setting_file_path: str, current_setting_file_
222
224
current .write (default .read ())
223
225
return
224
226
else :
225
- default_setting = otfdlib .Otfd ()
226
- default_setting .load (default_setting_file_path )
227
- default_setting .parse ()
228
- default_index_list = default_setting .get_index_list ()
229
- current_setting = otfdlib .Otfd ()
230
- current_setting .load (current_setting_file_path )
231
- current_setting .parse ()
232
- current_index_list = current_setting .get_index_list ()
233
- need_to_add = list (set (default_index_list ) - set (current_index_list ))
234
- current_setting .update (
235
- {index : default_setting .get_value (index ) for index in need_to_add })
236
- need_to_delete = list (set (current_index_list ) -
237
- set (default_index_list ))
227
+ default_setting = read_setting (default_setting_file_path )
228
+ current_setting = read_setting (current_setting_file_path )
229
+ need_to_delete = list (
230
+ set (current_setting .keys ()) - set (default_setting .keys ()))
238
231
for index in need_to_delete :
239
232
current_setting .pop (index )
240
- default_setting .sorted ()
241
- current_setting .sorted ()
242
- default_setting .write ()
243
- current_setting .write ()
244
- return
233
+ solved_setting = default_setting | current_setting
234
+ with open (current_setting_file_path , encoding = "utf-8_sig" , mode = "w" ) as f :
235
+ json .dump (solved_setting , f , indent = 4 )
245
236
246
237
247
238
def generate_search_engine_url (search_engine : str = "google" , keyword : str = None , define : bool = False ) -> str :
0 commit comments