From 02b13228905b1a340d06ab7af728ac4fd1c2caba Mon Sep 17 00:00:00 2001 From: Julien Woillez Date: Thu, 18 Sep 2014 16:48:29 +0200 Subject: [PATCH 1/6] If single DP ID requested without a list, naked filename is returned. --- astroquery/eso/core.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/astroquery/eso/core.py b/astroquery/eso/core.py index d63a5808d7..af31cc1657 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.") @@ -547,6 +550,8 @@ def retrieve_data(self, datasets, cache=True): filename = self._request("GET", fileLink, save=True) files.append(system_tools.gunzip(filename)) log.info("Done!") + if (not return_list) and (len(files)==1): + files = files[0] return files def verify_data_exists(self, dataset): From 0bd1d1fce99a69f12b7a09a0ab6cc4a0c36d130c Mon Sep 17 00:00:00 2001 From: Julien Woillez Date: Fri, 19 Sep 2014 20:36:12 +0200 Subject: [PATCH 2/6] Changed location of caching debug messages --- astroquery/query.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/astroquery/query.py b/astroquery/query.py index 6c93f30c8f..43791e10db 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 From 9807cda79073b8f2f594c454e32f82d218155764 Mon Sep 17 00:00:00 2001 From: Julien Woillez Date: Fri, 19 Sep 2014 20:48:17 +0200 Subject: [PATCH 3/6] Converted private __session from to non-public _session --- astroquery/query.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/astroquery/query.py b/astroquery/query.py index 43791e10db..6a66f31ddd 100644 --- a/astroquery/query.py +++ b/astroquery/query.py @@ -96,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): @@ -158,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) @@ -175,7 +175,7 @@ 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`` """ - 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']) From dd9997d57256cc58debbd26524313396031c9250 Mon Sep 17 00:00:00 2001 From: Julien Woillez Date: Fri, 19 Sep 2014 20:53:58 +0200 Subject: [PATCH 4/6] Clear session redirect_cache New requests will break otherwise --- astroquery/eso/core.py | 1 + 1 file changed, 1 insertion(+) diff --git a/astroquery/eso/core.py b/astroquery/eso/core.py index af31cc1657..e9f9a1b1e1 100644 --- a/astroquery/eso/core.py +++ b/astroquery/eso/core.py @@ -549,6 +549,7 @@ 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] From 2eca3433c0a3715a36937b7e86451a8f2bbf8c01 Mon Sep 17 00:00:00 2001 From: Julien Woillez Date: Fri, 19 Sep 2014 20:58:07 +0200 Subject: [PATCH 5/6] Add test for two retrieve_data() in a row --- astroquery/eso/tests/test_eso_remote.py | 7 +++++++ 1 file changed, 7 insertions(+) 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") From afc714bf263deef354c99d40dc34efdd54e42288 Mon Sep 17 00:00:00 2001 From: Julien Woillez Date: Fri, 19 Sep 2014 21:42:03 +0200 Subject: [PATCH 6/6] Docstring: __session -> _session --- astroquery/query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/astroquery/query.py b/astroquery/query.py index 6a66f31ddd..bd0d8fe7b1 100644 --- a/astroquery/query.py +++ b/astroquery/query.py @@ -173,7 +173,7 @@ 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, auth=auth)