@@ -239,15 +239,14 @@ def __init__(
239
239
self .default = self .data = ConfigFile .folder_mapping [folder ]
240
240
241
241
# currently only supports "json" files
242
- # assert fext=="json", local_logger.error(f'''Can't load file with extension: {fext}''')
243
- # if the extension is correct -> appending the extension name to the filename
244
242
self .file += "." + self .fext
245
243
# making path
246
244
self .path = os .path .join (self .folder , self .file )
247
245
# self.last_m_time = os.path.getmtime(self.path)
248
246
249
247
def __enter__ (self ):
250
- self .make_file ()
248
+ if not self .make_file ():
249
+ return self .default
251
250
self .read ()
252
251
return self
253
252
@@ -275,8 +274,20 @@ def make_file(self):
275
274
return False
276
275
return True
277
276
277
+ def is_json (self , file ):
278
+ try :
279
+ out = json .loads (file )
280
+ except :
281
+ return False
282
+ return True
283
+
278
284
def save (self ):
279
- """makes the file from the dict"""
285
+ """makes the file from the dict
286
+ can we really do anything if it fails to write? Save the original contents are write them again?"""
287
+ if not self .is_json (self .data ):
288
+ raise ValueError (f"Incorrect JSON for updated { self .file } " )
289
+ local_logger .critical (f"Discarding changes to { self .file } because they invalidated the JSON file." )
290
+
280
291
try :
281
292
with open (self .path , "w" , encoding = "utf-8" ) as file :
282
293
json .dump (self .data , file )
@@ -298,6 +309,8 @@ def read(self):
298
309
local_logger .error (f"An exception occured while reading { self .file } ." )
299
310
local_logger .exception (e )
300
311
raise e
312
+ return self .default
313
+
301
314
302
315
303
316
class Translator (object ):
0 commit comments