Skip to content

Commit

Permalink
check if file is cached
Browse files Browse the repository at this point in the history
  • Loading branch information
AWehrhahn committed Jul 9, 2021
1 parent 07918ea commit 27ac74d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/pysme/large_file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
import shutil
from tempfile import NamedTemporaryFile

from astropy.utils.data import import_file_to_cache, download_file, clear_download_cache
from astropy.utils.data import (
import_file_to_cache,
download_file,
clear_download_cache,
is_url_in_cache,
)

from .config import Config

Expand Down Expand Up @@ -99,21 +104,17 @@ def get(self, key):
# Otherwise get it from the cache or online if necessary
newest = self.pointers[key]
url = join(self.server, newest)
is_cached = is_url_in_cache(url, pkgname="pysme")
fname = download_file(url, cache=True, pkgname="pysme")

try:
if not is_cached and url.endswith(".gz"):
# If the file is compressed
# Replace the cache file with the decompressed file
with gzip.open(fname, "rb") as f_in:
# This should check whether this is a gzip file or not
f_in.peek(1)
with NamedTemporaryFile("wb") as f_out:
shutil.copyfileobj(f_in, f_out)
f_out.flush()
import_file_to_cache(url, f_out.name, pkgname="pysme")
except gzip.BadGzipFile:
# The file was already decompressed
pass

return fname

Expand Down

0 comments on commit 27ac74d

Please sign in to comment.