From 2f278739a4e921fe67d5077c96c14dfa3911579d Mon Sep 17 00:00:00 2001 From: Jeff Klenzing <19592220+jklenzing@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:22:34 -0400 Subject: [PATCH 1/3] BUG: error if temp_dir unset --- pysatNASA/instruments/methods/cdaweb.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/pysatNASA/instruments/methods/cdaweb.py b/pysatNASA/instruments/methods/cdaweb.py index 5a56cd07..62ef1e3f 100644 --- a/pysatNASA/instruments/methods/cdaweb.py +++ b/pysatNASA/instruments/methods/cdaweb.py @@ -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() @@ -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: @@ -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 @@ -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) + Warnings + -------- + - Warns if temp_path not set when unzipping. + """ 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) From c29d81d7c98dbf8cbb5dd31d9e8064f88557d0f5 Mon Sep 17 00:00:00 2001 From: Jeff Klenzing <19592220+jklenzing@users.noreply.github.com> Date: Thu, 29 Aug 2024 14:22:47 -0400 Subject: [PATCH 2/3] TST: test _get_file --- pysatNASA/tests/test_methods_cdaweb.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pysatNASA/tests/test_methods_cdaweb.py b/pysatNASA/tests/test_methods_cdaweb.py index c5184a06..666e0f2c 100644 --- a/pysatNASA/tests/test_methods_cdaweb.py +++ b/pysatNASA/tests/test_methods_cdaweb.py @@ -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 @@ -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", From 97d4e960aa779878d222e6d13f175d64988eb41f Mon Sep 17 00:00:00 2001 From: Jeff Klenzing <19592220+jklenzing@users.noreply.github.com> Date: Thu, 29 Aug 2024 16:38:59 -0400 Subject: [PATCH 3/3] Update pysatNASA/instruments/methods/cdaweb.py --- pysatNASA/instruments/methods/cdaweb.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pysatNASA/instruments/methods/cdaweb.py b/pysatNASA/instruments/methods/cdaweb.py index 62ef1e3f..b0e71803 100644 --- a/pysatNASA/instruments/methods/cdaweb.py +++ b/pysatNASA/instruments/methods/cdaweb.py @@ -619,9 +619,9 @@ 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) - Warnings - -------- - - Warns if temp_path not set when unzipping. + Raises + ------ + ValueError if temp_path not specified for zip_method """