Account for situations where a state file does not (yet) exist
This commit is contained in:
parent
83921912a4
commit
81ce5812a6
42
mvw-dl.py
42
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
|
# 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,12 +462,19 @@ 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")
|
||||||
state_file = \
|
try:
|
||||||
config_obj.get(section_name, "state_file_name_prefix") + \
|
os.makedirs(state_dir, exist_ok=True)
|
||||||
section_name + \
|
except OSError:
|
||||||
config_obj.get(section_name, "state_file_name_suffix")
|
log.error(f"Unable to create '[{section}]' state directory '{state_dir}'. "
|
||||||
state_file_abs_path = os.path.join(state_dir, state_file)
|
f"We're not going to be able to log state information. Exiting 6 ...")
|
||||||
return state_file_abs_path
|
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(
|
def state_file_none_or_valid_json(
|
||||||
@ -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,14 +660,17 @@ 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)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
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(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user