Account for situations where a state file does not (yet) exist
This commit is contained in:
parent
83921912a4
commit
81ce5812a6
16
mvw-dl.py
16
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,6 +462,13 @@ def get_state_file_abs_path(
|
||||
config_obj: configparser.ConfigParser()) -> str:
|
||||
|
||||
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 = \
|
||||
config_obj.get(section_name, "state_file_name_prefix") + \
|
||||
section_name + \
|
||||
@ -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,7 +660,7 @@ def get_content_length(
|
||||
|
||||
def get_json_state(
|
||||
state_file_abs_path: str) -> json.loads:
|
||||
|
||||
try:
|
||||
with open(state_file_abs_path, "r", encoding="utf-8") as state_file:
|
||||
try:
|
||||
json_state = json.load(state_file)
|
||||
@ -659,6 +668,9 @@ def get_json_state(
|
||||
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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user