diff --git a/astroquery/eso/core.py b/astroquery/eso/core.py index d63a5808d7..e9f9a1b1e1 100644 --- a/astroquery/eso/core.py +++ b/astroquery/eso/core.py @@ -461,7 +461,7 @@ def retrieve_data(self, datasets, cache=True): Returns ------- - files : list of strings + files : list of strings or string List of files that have been locally downloaded from the archive. Examples @@ -475,7 +475,10 @@ def retrieve_data(self, datasets, cache=True): files = [] if isinstance(datasets, six.string_types): + return_list = False datasets = [datasets] + else: + return_list = True if not isinstance(datasets, (list, tuple, np.ndarray)): raise TypeError("Datasets must be given as a list of strings.") @@ -546,7 +549,10 @@ def retrieve_data(self, datasets, cache=True): fileLink = "http://dataportal.eso.org/dataPortal"+fileId.attrs['value'].split()[1] filename = self._request("GET", fileLink, save=True) files.append(system_tools.gunzip(filename)) + self._session.redirect_cache.clear() # EMpty the redirect cache of this request session log.info("Done!") + if (not return_list) and (len(files)==1): + files = files[0] return files def verify_data_exists(self, dataset): diff --git a/astroquery/eso/tests/test_eso_remote.py b/astroquery/eso/tests/test_eso_remote.py index 85504bfd68..5613a33baa 100644 --- a/astroquery/eso/tests/test_eso_remote.py +++ b/astroquery/eso/tests/test_eso_remote.py @@ -112,3 +112,10 @@ def test_retrieve_data(self): result = eso.retrieve_data("MIDI.2014-07-25T02:03:11.561") assert len(result)>0 assert "MIDI.2014-07-25T02:03:11.561" in result[0] + + @pytest.mark.skipif('not Eso.USERNAME') + def test_retrieve_data_twice(self): + eso = Eso() + eso.login() + result1 = eso.retrieve_data("MIDI.2014-07-25T02:03:11.561") + result2 = eso.retrieve_data("AMBER.2006-03-14T07:40:19.830") diff --git a/astroquery/query.py b/astroquery/query.py index 6c93f30c8f..bd0d8fe7b1 100644 --- a/astroquery/query.py +++ b/astroquery/query.py @@ -18,6 +18,7 @@ def to_cache(response, cache_file): + log.debug("Caching data to {0}".format(cache_file)) with open(cache_file, "wb") as f: pickle.dump(response, f) @@ -71,11 +72,9 @@ def hash(self): def request_file(self, cache_location): fn = os.path.join(cache_location, self.hash() + ".pickle") - log.debug("Request file is {0}".format(fn)) return fn def from_cache(self, cache_location): - log.debug("Retrieving data from {0}".format(cache_location)) request_file = self.request_file(cache_location) try: with open(request_file, "rb") as f: @@ -84,6 +83,8 @@ def from_cache(self, cache_location): response = None except: response = None + if response: + log.debug("Retrieving data from {0}".format(request_file)) return response @@ -95,7 +96,7 @@ class BaseQuery(object): """ def __init__(self): - self.__session = requests.session() + self._session = requests.session() self.cache_location = os.path.join(paths.get_cache_dir(), 'astroquery', self.__class__.__name__.split("Class")[0]) if not os.path.exists(self.cache_location): @@ -157,12 +158,12 @@ def _request(self, method, url, params=None, data=None, headers=None, if ((self.cache_location is None) or (not self._cache_active) or (not cache)): with suspend_cache(self): - response = query.request(self.__session, stream=stream, + response = query.request(self._session, stream=stream, auth=auth) else: response = query.from_cache(self.cache_location) if not response: - response = query.request(self.__session, + response = query.request(self._session, self.cache_location, stream=stream, auth=auth) @@ -172,9 +173,9 @@ def _request(self, method, url, params=None, data=None, headers=None, def _download_file(self, url, local_filepath, timeout=None, auth=None): """ Download a file. Resembles `astropy.utils.data.download_file` but uses - the local ``__session`` + the local ``_session`` """ - response = self.__session.get(url, timeout=timeout, stream=True, + response = self._session.get(url, timeout=timeout, stream=True, auth=auth) if 'content-length' in response.headers: length = int(response.headers['content-length'])