Skip to content

Commit 1bed399

Browse files
keflavichbsipocz
authored andcommitted
A combination of many squashed commits for adding tests and several follow-up fixes.
1 parent 0c10fe9 commit 1bed399

File tree

3 files changed

+393
-19
lines changed

3 files changed

+393
-19
lines changed

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ New Tools and Services
99
API changes
1010
-----------
1111

12+
query.py
13+
^^^^^^^^
14+
15+
- ``_download_file`` now returns the local file path in all cases (formerly, it would return ``None`` if the file was already cached). Some corner cases where downloads were not properly continued have been fixed [#3232]
16+
1217

1318

1419
Service fixes and enhancements
@@ -132,6 +137,8 @@ Infrastructure, Utility and Other Changes and Additions
132137
- Removed usage of the astropy TestRunner, therefore the unadvertised
133138
``astroquery.test()`` functionality. [#3215]
134139

140+
- ``_download_file`` now returns the local file path in all cases (formerly, it would return ``None`` if the file was already cached). Some corner cases where downloads were not properly continued have been fixed [#3232]
141+
135142

136143
0.4.9 (2025-01-24)
137144
==================

astroquery/query.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -425,29 +425,29 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
425425
if 'content-length' in response.headers:
426426
length = int(response.headers['content-length'])
427427
if length == 0:
428-
log.warn('URL {0} has length=0'.format(url))
428+
log.warning('URL {0} has length=0'.format(url))
429429
else:
430430
length = None
431431

432432
if ((os.path.exists(local_filepath)
433433
and ('Accept-Ranges' in response.headers)
434+
and length is not None
434435
and continuation)):
435436
open_mode = 'ab'
436437

437438
existing_file_length = os.stat(local_filepath).st_size
438-
if length is not None and existing_file_length >= length:
439-
# all done!
440-
log.info("Found cached file {0} with expected size {1}."
441-
.format(local_filepath, existing_file_length))
442-
return
443-
elif existing_file_length == 0:
439+
if existing_file_length == 0:
444440
log.info(f"Found existing {local_filepath} file with length 0. Overwriting.")
445441
open_mode = 'wb'
446442
if head_safe:
447443
response = self._session.request(method, url,
448444
timeout=timeout, stream=True,
449445
auth=auth, **kwargs)
450446
response.raise_for_status()
447+
elif existing_file_length >= length:
448+
# all done!
449+
log.info(f"Found cached file {local_filepath} with size {existing_file_length} = {length}.")
450+
return local_filepath
451451
else:
452452
log.info("Continuing download of file {0}, with {1} bytes to "
453453
"go ({2}%)".format(local_filepath,
@@ -475,22 +475,21 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
475475
f"that is different from expected size {length}. ")
476476
if continuation:
477477
log.warning(
478-
"Continuation was requested but is not possible because "
479-
"'Accepts-Ranges' is not in the response headers.")
478+
"Continuation was requested but is not possible because "
479+
"'Accepts-Ranges' is not in the response headers.")
480480
open_mode = 'wb'
481481
response = self._session.request(method, url,
482-
timeout=timeout, stream=True,
483-
auth=auth, **kwargs)
482+
timeout=timeout, stream=True,
483+
auth=auth, **kwargs)
484484
response.raise_for_status()
485485
else:
486-
log.info("Found cached file {0} with expected size {1}."
487-
.format(local_filepath, statinfo.st_size))
486+
log.info(f"Found cached file {local_filepath} with expected size {statinfo.st_size}.")
488487
response.close()
489-
return
488+
return local_filepath
490489
else:
491-
log.info("Found cached file {0}.".format(local_filepath))
492-
response.close()
493-
return
490+
# This case doesn't appear reachable under normal circumstances
491+
# It is not covered by tests, and probably indicates a badly-behaved server
492+
raise ValueError(f"Found cached file {local_filepath}. Could not verify length.")
494493
else:
495494
open_mode = 'wb'
496495
if head_safe:
@@ -528,7 +527,7 @@ def _download_file(self, url, local_filepath, timeout=None, auth=None,
528527
f.write(response.content)
529528

530529
response.close()
531-
return response
530+
return local_filepath
532531

533532

534533
@deprecated(since="v0.4.7", message=("The suspend_cache function is deprecated,"

0 commit comments

Comments
 (0)