Clean up config files after error

This commit is contained in:
Daniel Markstedt 2021-10-21 17:10:25 -07:00
parent 269b718ec7
commit e059432402

View File

@ -244,9 +244,11 @@ def write_config(file_name):
return {"status": True, "msg": f"Successfully wrote to file: {file_name}"}
except (IOError, ValueError, EOFError, TypeError) as e:
logging.error(str(e))
delete_file(file_name)
return {"status": False, "msg": str(e)}
except:
logging.error(f"Could not write to file: {file_name}")
delete_file(file_name)
return {"status": False, "msg": f"Could not write to file: {file_name}"}
@ -284,21 +286,24 @@ def read_config(file_name):
def write_drive_properties(file_name, conf):
"""
Writes a drive property configuration file to the images dir.
Writes a drive property configuration file to the config dir.
Takes file name base (str) and conf (list of dicts) as arguments
Returns dict with boolean status and str msg
"""
from json import dump
file_path = cfg_dir + file_name
try:
with open(cfg_dir + file_name, "w") as json_file:
with open(file_path, "w") as json_file:
dump(conf, json_file, indent=4)
return {"status": True, "msg": f"Successfully wrote to file: {file_name}"}
return {"status": True, "msg": f"Successfully wrote to file: {file_path}"}
except (IOError, ValueError, EOFError, TypeError) as e:
logging.error(str(e))
delete_file(file_path)
return {"status": False, "msg": str(e)}
except:
logging.error(f"Could not write to file: {file_name}")
return {"status": False, "msg": f"Could not write to file: {file_name}"}
logging.error(f"Could not write to file: {file_path}")
delete_file(file_path)
return {"status": False, "msg": f"Could not write to file: {file_path}"}