Skip to content

Commit

Permalink
Fix logic that I broke
Browse files Browse the repository at this point in the history
  • Loading branch information
nabobalis committed Apr 25, 2024
1 parent 99c3a36 commit fdf6aea
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions sunpy/data/data_manager/cache.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
from pathlib import Path
from datetime import datetime
from urllib.request import urlopen
Expand Down Expand Up @@ -59,23 +58,22 @@ def download(self, urls, namespace='', redownload=False):
"""
if isinstance(urls, str | Path):
urls = [urls]
# Logic flow
# Logic plan
# 1. If redownload: Download, update cache and return file path
# 2. If not redownload: Check cache,
# i. If present in cache:
# - If cache expired, download and add to cache and remove entry from cache
# - If cache expired, download and update cache (without removing the old file if the download fails)
# - If cache not expired, return path
for url in urls:
present = self._get_by_url(url)
if present:
cache_details = self._get_by_url(url)
if cache_details:
break
if present and not redownload and not self._has_expired(present):
return Path(present['file_path'])
if cache_details and not redownload and not self._has_expired(cache_details):
return Path(cache_details['file_path'])
try:
file_path, file_hash, url = self._download_and_hash(urls, namespace)
if present and (redownload or self._has_expired(present)):
os.remove(present['file_path'])
self._storage.delete_by_key('url', present['url'])
if cache_details and (redownload or self._has_expired(cache_details)):
self._storage.delete_by_key('url', cache_details['url'])
self._storage.store({
'file_hash': file_hash,
'file_path': str(file_path),
Expand All @@ -85,12 +83,12 @@ def download(self, urls, namespace='', redownload=False):
return file_path
except Exception as e:
exception_msg = f"{e} \n"
if not present:
if not cache_details:
raise RuntimeError(exception_msg) from e

Check warning on line 87 in sunpy/data/data_manager/cache.py

View check run for this annotation

Codecov / codecov/patch

sunpy/data/data_manager/cache.py#L87

Added line #L87 was not covered by tests
warn_user(
f"{exception_msg}Due to the above error, you will be working with a stale version of the file in the cache."
)
return Path(present['file_path'])
return Path(cache_details['file_path'])

def _has_expired(self, details):
"""
Expand Down

0 comments on commit fdf6aea

Please sign in to comment.