From 81ce5812a6087e04586b0701830c004bfd58b391 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Wed, 23 Mar 2022 23:38:25 +0100 Subject: [PATCH] Account for situations where a state file does not (yet) exist --- mvw-dl.py | 42 +++++++++++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/mvw-dl.py b/mvw-dl.py index f9d997f..bc2859d 100644 --- a/mvw-dl.py +++ b/mvw-dl.py @@ -89,6 +89,7 @@ JSONType = t.Union[str, int, float, bool, None, t.Dict[str, t.Any], t.List[t.Any # 3: No search results to download # 4: State file already exists, has more than 0 bytes size but doesn't contain usable JSON # 5: State file lock cannot be acquired within file_lock_timeout +# 6: Unable to create state directory class CONST(object): @@ -461,12 +462,19 @@ def get_state_file_abs_path( config_obj: configparser.ConfigParser()) -> str: state_dir = config_obj.get(section_name, "state_files_dir") - state_file = \ - config_obj.get(section_name, "state_file_name_prefix") + \ - section_name + \ - config_obj.get(section_name, "state_file_name_suffix") - state_file_abs_path = os.path.join(state_dir, state_file) - return state_file_abs_path + try: + os.makedirs(state_dir, exist_ok=True) + except OSError: + log.error(f"Unable to create '[{section}]' state directory '{state_dir}'. " + f"We're not going to be able to log state information. Exiting 6 ...") + sys.exit(6) + else: + state_file = \ + config_obj.get(section_name, "state_file_name_prefix") + \ + section_name + \ + config_obj.get(section_name, "state_file_name_suffix") + state_file_abs_path = os.path.join(state_dir, state_file) + return state_file_abs_path def state_file_none_or_valid_json( @@ -534,7 +542,8 @@ def log_successful_download( with lock: state_file_none_or_valid_json(state_file_abs_path) - with open(state_file_abs_path, "r+", encoding="utf-8") as state_file: + state_file_open_mode = "r+" if os.path.exists(state_file_abs_path) else "w+" + with open(state_file_abs_path, state_file_open_mode, encoding="utf-8") as state_file: try: json_state = json.load(state_file) except json.JSONDecodeError: @@ -651,14 +660,17 @@ def get_content_length( def get_json_state( state_file_abs_path: str) -> json.loads: - - with open(state_file_abs_path, "r", encoding="utf-8") as state_file: - try: - json_state = json.load(state_file) - except json.JSONDecodeError: - return [] - else: - return json_state + try: + with open(state_file_abs_path, "r", encoding="utf-8") as state_file: + try: + json_state = json.load(state_file) + except json.JSONDecodeError: + return [] + else: + return json_state + except FileNotFoundError: + log.debug(f"State file does not exist (yet), assuming no previous downloads have ever happened ...") + return [] def is_already_downloaded(