Skip to content

Commit

Permalink
Merge pull request #244 from pysat/bug/get_file
Browse files Browse the repository at this point in the history
BUG/STY: update `_get_file` in download
  • Loading branch information
jklenzing authored Aug 29, 2024
2 parents 53a3b7a + 97d4e96 commit fa3e01f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 14 additions & 5 deletions pysatNASA/instruments/methods/cdaweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@ def download(date_array, data_path, tag='', inst_id='', supported_tags=None,
stop=date_array[-1])

# Create temproary directory if files need to be unzipped.
# Use one temp dir for all files if needed.
if 'zip_method' in inst_dict.keys():
zip_method = inst_dict['zip_method']
temp_dir = tempfile.TemporaryDirectory()
Expand Down Expand Up @@ -577,10 +578,11 @@ def download(date_array, data_path, tag='', inst_id='', supported_tags=None,
with requests.get(remote_path) as req:
if req.status_code != 404:
if zip_method:
get_file(req.content, data_path, fname,
temp_path=temp_dir.name, zip_method=zip_method)
_get_file(req.content, data_path, fname,
temp_path=temp_dir.name,
zip_method=zip_method)
else:
get_file(req.content, data_path, fname)
_get_file(req.content, data_path, fname)
logger.info(''.join(('Successfully downloaded ',
fname, '.')))
else:
Expand All @@ -599,7 +601,7 @@ def download(date_array, data_path, tag='', inst_id='', supported_tags=None,
return


def get_file(remote_file, data_path, fname, temp_path=None, zip_method=None):
def _get_file(remote_file, data_path, fname, temp_path=None, zip_method=None):
"""Retrieve a file, unzipping if necessary.
Parameters
Expand All @@ -617,11 +619,18 @@ def get_file(remote_file, data_path, fname, temp_path=None, zip_method=None):
The method used to zip the file. Supports 'zip' and None.
If None, downloads files directly. (default=None)
Raises
------
ValueError if temp_path not specified for zip_method
"""

if zip_method:
# Use a temporary location.
dl_fname = os.path.join(temp_path, fname)
if temp_path:
dl_fname = os.path.join(temp_path, fname)
else:
raise ValueError('Temp path needs to be set if unzipping')
else:
# Use the pysat data directory.
dl_fname = os.path.join(data_path, fname)
Expand Down
12 changes: 10 additions & 2 deletions pysatNASA/tests/test_methods_cdaweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def test_bad_zip_warning_get_files(self, caplog):
temp_dir = tempfile.TemporaryDirectory()

with caplog.at_level(logging.WARNING, logger='pysat'):
cdw.get_file(req.content, '.', 'test.txt', temp_path=temp_dir.name,
zip_method='badzip')
cdw._get_file(req.content, '.', 'test.txt', temp_path=temp_dir.name,
zip_method='badzip')
captured = caplog.text

# Check for appropriate warning
Expand All @@ -119,6 +119,14 @@ def test_bad_zip_warning_get_files(self, caplog):

return

def test_get_file_unzip_without_temp_path(self):
"""Test that warning when cdf file does not have expected params."""

with pytest.raises(ValueError) as excinfo:
cdw._get_file('remote_file', 'fake_path', 'fname', zip_method='zip')
assert str(excinfo.value).find('Temp path needs') >= 0
return

@pytest.mark.parametrize("bad_key,bad_val,err_msg",
[("tag", "badval", "inst_id / tag combo unknown."),
("inst_id", "badval",
Expand Down

0 comments on commit fa3e01f

Please sign in to comment.