Account for situations where a state file does not (yet) exist

This commit is contained in:
hygienic-books 2022-03-23 23:38:25 +01:00
parent 83921912a4
commit 81ce5812a6

View File

@ -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 # 3: No search results to download
# 4: State file already exists, has more than 0 bytes size but doesn't contain usable JSON # 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 # 5: State file lock cannot be acquired within file_lock_timeout
# 6: Unable to create state directory
class CONST(object): class CONST(object):
@ -461,6 +462,13 @@ def get_state_file_abs_path(
config_obj: configparser.ConfigParser()) -> str: config_obj: configparser.ConfigParser()) -> str:
state_dir = config_obj.get(section_name, "state_files_dir") state_dir = config_obj.get(section_name, "state_files_dir")
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 = \ state_file = \
config_obj.get(section_name, "state_file_name_prefix") + \ config_obj.get(section_name, "state_file_name_prefix") + \
section_name + \ section_name + \
@ -534,7 +542,8 @@ def log_successful_download(
with lock: with lock:
state_file_none_or_valid_json(state_file_abs_path) 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: try:
json_state = json.load(state_file) json_state = json.load(state_file)
except json.JSONDecodeError: except json.JSONDecodeError:
@ -651,7 +660,7 @@ def get_content_length(
def get_json_state( def get_json_state(
state_file_abs_path: str) -> json.loads: state_file_abs_path: str) -> json.loads:
try:
with open(state_file_abs_path, "r", encoding="utf-8") as state_file: with open(state_file_abs_path, "r", encoding="utf-8") as state_file:
try: try:
json_state = json.load(state_file) json_state = json.load(state_file)
@ -659,6 +668,9 @@ def get_json_state(
return [] return []
else: else:
return json_state 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( def is_already_downloaded(