make file undergo a redownload on cache mismatch
@@ -48,7 +48,7 @@ def get_data_dir():
return data_dir
-def data_path(filename):
+def data_path(filename, redownload_on_hash_mismatch=True):
filepath = get_data_dir().joinpath(filename)
with open(HERE.joinpath("hashes.json"), "r") as hash_file:
@@ -73,9 +73,6 @@ def data_path(filename):
try:
cached_filehash = hashes[filename]
-
- if cached_filehash != calculated_filehash:
- raise ValueError("The file on disk does not match the recorded hash.")
except KeyError:
warnings.warn("Hash not found in hashes.json. File will be updated.")
hashes[filename] = calculated_filehash
@@ -84,3 +81,12 @@ def data_path(filename):
json.dump(hashes, hash_file, indent=2, sort_keys=True)
return filepath.resolve()
+
+ if cached_filehash != calculated_filehash:
+ if redownload_on_hash_mismatch:
+ filepath.unlink()
+ return data_path(filename, redownload_on_hash_mismatch=False)
+
+ raise ValueError("The file on disk does not match the recorded hash.")
+
+ return filepath.resolve()
