From e059432402845e18f1a8b2c84f05885ead9b5738 Mon Sep 17 00:00:00 2001 From: Daniel Markstedt Date: Thu, 21 Oct 2021 17:10:25 -0700 Subject: [PATCH] Clean up config files after error --- src/web/file_cmds.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/web/file_cmds.py b/src/web/file_cmds.py index e16eba24..c750a15f 100644 --- a/src/web/file_cmds.py +++ b/src/web/file_cmds.py @@ -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}"}