Skip to content

Commit 88d552e

Browse files
committed
Merge branch 'cached_download' into alma-private-fix
2 parents 6a3dc96 + 999b86f commit 88d552e

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

astroquery/alma/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def download_files(self, files, cache=True):
362362
downloaded_files = []
363363
for fileLink in unique(files):
364364
filename = self._request("GET", fileLink, save=True,
365-
timeout=self.TIMEOUT)
365+
timeout=self.TIMEOUT, cache=cache)
366366
downloaded_files.append(filename)
367367
return downloaded_files
368368

astroquery/query.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ def _request(self, method, url, params=None, data=None, headers=None,
152152
local_filename = url.split('/')[-1]
153153
local_filepath = os.path.join(self.cache_location or savedir or
154154
'.', local_filename)
155-
log.info("Downloading {0}...".format(local_filename))
155+
# REDUNDANT: spinner has this log.info("Downloading {0}...".format(local_filename))
156156
self._download_file(url, local_filepath, timeout=timeout,
157-
auth=auth)
157+
auth=auth, cache=cache)
158158
return local_filepath
159159
else:
160160
query = AstroQuery(method, url, params=params, data=data,
@@ -174,7 +174,7 @@ def _request(self, method, url, params=None, data=None, headers=None,
174174
to_cache(response, query.request_file(self.cache_location))
175175
return response
176176

177-
def _download_file(self, url, local_filepath, timeout=None, auth=None):
177+
def _download_file(self, url, local_filepath, timeout=None, auth=None, cache=False):
178178
"""
179179
Download a file. Resembles `astropy.utils.data.download_file` but uses
180180
the local ``_session``
@@ -187,12 +187,30 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None):
187187
else:
188188
length = None
189189

190+
if cache and os.path.exists(local_filepath):
191+
if length is not None:
192+
statinfo = os.stat(local_filepath)
193+
if statinfo.st_size != length:
194+
log.warn("Found cached file {0} with size {1} that is "
195+
"different from expected size {2}"
196+
.format(local_filepath,
197+
statinfo.st_size,
198+
length))
199+
else:
200+
response.close()
201+
return
202+
else:
203+
log.info("Found cached file {0}.".format(local_filepath))
204+
response.close()
205+
return
206+
190207
blocksize = astropy.utils.data.conf.download_block_size
191208

192209
bytes_read = 0
193210

194211
with ProgressBarOrSpinner(length,
195-
'Downloading URL {0} ...'.format(url)) as pb:
212+
'Downloading URL {0} to {1} ...'.format(url,
213+
local_filepath)) as pb:
196214
with open(local_filepath, 'wb') as f:
197215
for block in response.iter_content(blocksize):
198216
f.write(block)

0 commit comments

Comments
 (0)