Skip to content

Commit a86989b

Browse files
committed
Make linter heppier
1 parent ecaaca2 commit a86989b

File tree

4 files changed

+73
-74
lines changed

4 files changed

+73
-74
lines changed

astroquery/eso/core.py

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from ..exceptions import RemoteServiceError, NoResultsWarning, LoginError
3232
from ..query import QueryWithLogin
3333
from ..utils import schema
34-
from .utils import py2adql, _split_str_as_list_of_str, sanitize_val, to_cache, hash, _check_response
34+
from .utils import py2adql, _split_str_as_list_of_str, sanitize_val, to_cache, eso_hash
3535
import pyvo
3636

3737
__doctest_skip__ = ['EsoClass.*']
@@ -41,7 +41,6 @@ class CalSelectorError(Exception):
4141
"""
4242
Raised on failure to parse CalSelector's response.
4343
"""
44-
pass
4544

4645

4746
class AuthInfo:
@@ -94,6 +93,8 @@ def __init__(self, timeout=None):
9493
self._auth_info: Optional[AuthInfo] = None
9594
self.timeout = timeout
9695
self._hash = None
96+
# for future debugging
97+
self._payload = None
9798

9899
@property
99100
def timeout(self):
@@ -114,7 +115,7 @@ def tap_url() -> str:
114115
return url
115116

116117
def request_file(self, query_str: str):
117-
h = hash(query_str=query_str, url=self.tap_url())
118+
h = eso_hash(query_str=query_str, url=self.tap_url())
118119
fn = self.cache_location.joinpath(h + ".pickle")
119120
return fn
120121

@@ -133,12 +134,12 @@ def from_cache(self, query_str, cache_timeout):
133134
if not isinstance(cached_table, Table):
134135
cached_table = None
135136
else:
136-
log.debug(f"Cache expired for {table_file}...")
137+
log.debug("Cache expired for %s ... ", table_file)
137138
cached_table = None
138139
except FileNotFoundError:
139140
cached_table = None
140141
if cached_table:
141-
log.debug("Retrieved data from {0}".format(table_file))
142+
log.debug("Retrieved data from %s", table_file)
142143
return cached_table
143144

144145
def _authenticate(self, *, username: str, password: str) -> bool:
@@ -152,7 +153,7 @@ def _authenticate(self, *, username: str, password: str) -> bool:
152153
"client_secret": "clientSecret",
153154
"username": username,
154155
"password": password}
155-
log.info(f"Authenticating {username} on 'www.eso.org' ...")
156+
log.info("Authenticating %s on 'www.eso.org' ...", username)
156157
response = self._request('GET', self.AUTH_URL, params=url_params)
157158
if response.status_code == 200:
158159
token = json.loads(response.content)['id_token']
@@ -187,8 +188,8 @@ def _get_auth_info(self, username: str, *, store_password: bool = False,
187188

188189
return username, password
189190

190-
def _login(self, *, username: str = None, store_password: bool = False,
191-
reenter_password: bool = False) -> bool:
191+
def _login(self, *args, username: str = None, store_password: bool = False,
192+
reenter_password: bool = False, **kwargs) -> bool:
192193
"""
193194
Login to the ESO User Portal.
194195
@@ -221,7 +222,7 @@ def _get_auth_header(self) -> Dict[str, str]:
221222
else:
222223
return {}
223224

224-
def _query_tap_service(self, query_str: str, cache: Optional[bool] = None) -> Optional[astropy.table.Table]:
225+
def query_tap_service(self, query_str: str, cache: Optional[bool] = None) -> Optional[astropy.table.Table]:
225226
"""
226227
returns an astropy.table.Table from an adql query string
227228
Example use:
@@ -245,10 +246,12 @@ def _query_tap_service(self, query_str: str, cache: Optional[bool] = None) -> Op
245246
table_to_return = tap.search(query_str).to_table()
246247
to_cache(table_to_return, self.request_file(query_str=query_str))
247248

248-
except pyvo.dal.exceptions.DALQueryError:
249-
raise pyvo.dal.exceptions.DALQueryError(f"\n\nError executing the following query:\n\n{query_str}\n\n")
249+
except pyvo.dal.exceptions.DALQueryError as e:
250+
raise pyvo.dal.exceptions.DALQueryError(f"\n\n\
251+
Error executing the following query:\n\n{query_str}\n\n") from e
250252
except Exception as e:
251-
raise Exception(f"\n\nUnknown exception {e} while executing the following query: \n\n{query_str}\n\n")
253+
raise RuntimeError(f"\n\n\
254+
Unknown exception {e} while executing the following query: \n\n{query_str}\n\n") from e
252255

253256
if len(table_to_return) > 0:
254257
return table_to_return
@@ -269,7 +272,7 @@ def list_instruments(self, *, cache=True) -> List[str]:
269272
if self._instruments is None:
270273
self._instruments = []
271274
query_str = "select table_name from TAP_SCHEMA.tables where schema_name='ist' order by table_name"
272-
res = self._query_tap_service(query_str)["table_name"].data
275+
res = self.query_tap_service(query_str, cache=cache)["table_name"].data
273276
self._instruments = list(map(lambda x: x.split(".")[1], res))
274277
return self._instruments
275278

@@ -288,15 +291,15 @@ def list_collections(self, *, cache=True) -> List[str]:
288291
c = QueryOnCollection.column_name
289292
t = QueryOnCollection.table_name
290293
query_str = f"select distinct {c} from {t}"
291-
res = self._query_tap_service(query_str)[c].data
294+
res = self.query_tap_service(query_str, cache=cache)[c].data
292295

293296
self._collections = list(res)
294297
return self._collections
295298

296299
def _query_instrument_or_collection(self,
297300
query_on: QueryOnField, primary_filter: Union[List[str], str], *,
298301
column_filters: Dict = None,
299-
columns: Union[List, str] = None, help=False, cache=True,
302+
columns: Union[List, str] = None, print_help=False, cache=True,
300303
**kwargs) -> astropy.table.Table:
301304
"""
302305
Query instrument- or collection-specific data contained in the ESO archive.
@@ -335,18 +338,18 @@ def _query_instrument_or_collection(self,
335338
columns = columns or []
336339

337340
# TODO - move help printing to its own function
338-
if help:
341+
if print_help:
339342
help_query = \
340343
f"select column_name, datatype from TAP_SCHEMA.columns where table_name = '{query_on.table_name}'"
341-
h = self._query_tap_service(help_query)
342-
log.info(f"Columns present in the table: {h}")
344+
h = self.query_tap_service(help_query)
345+
log.info("Columns present in the table: %s", h)
343346
return
344347

345348
filters = {**dict(kwargs), **column_filters}
346349
c_size = 0.1775 # so that even HARPS fits to pass the tests
347350
if 'box' in filters.keys():
348351
# TODO make c_size a parameter
349-
c_size = c_size
352+
# c_size = c_size
350353
del filters['box']
351354
if isinstance(primary_filter, str):
352355
primary_filter = _split_str_as_list_of_str(primary_filter)
@@ -369,38 +372,38 @@ def _query_instrument_or_collection(self,
369372
query = py2adql(table=query_on.table_name, columns=columns, where_constraints=where_constraints,
370373
top=self.ROW_LIMIT)
371374

372-
return self._query_tap_service(query_str=query, cache=cache)
375+
return self.query_tap_service(query_str=query, cache=cache)
373376

374377
@deprecated_renamed_argument(old_name='open_form', new_name=None, since='0.4.8')
375378
def query_instrument(self, instrument: Union[List, str] = None, *,
376379
column_filters: Dict = None, columns: Union[List, str] = None,
377-
open_form=False, help=False, cache=True,
380+
open_form=False, print_help=False, cache=True,
378381
**kwargs) -> astropy.table.Table:
379382
return self._query_instrument_or_collection(query_on=QueryOnInstrument,
380383
primary_filter=instrument,
381384
column_filters=column_filters,
382385
columns=columns,
383-
help=help,
386+
print_help=print_help,
384387
cache=cache,
385388
**kwargs)
386389

387390
@deprecated_renamed_argument(old_name='open_form', new_name=None, since='0.4.8')
388391
def query_collections(self, collections: Union[List, str] = None, *,
389392
column_filters: Dict = None, columns: Union[List, str] = None,
390-
open_form=False, help=False, cache=True,
393+
open_form=False, print_help=False, cache=True,
391394
**kwargs) -> astropy.table.Table:
392395
column_filters = column_filters or {}
393396
columns = columns or []
394397
return self._query_instrument_or_collection(query_on=QueryOnCollection,
395398
primary_filter=collections,
396399
column_filters=column_filters,
397400
columns=columns,
398-
help=help,
401+
print_help=print_help,
399402
cache=cache,
400403
**kwargs)
401404

402-
def query_main(self, *, column_filters={}, columns=[],
403-
open_form=False, help=False, cache=True, **kwargs):
405+
def query_main(self, *, column_filters=None, columns=None,
406+
print_help=False, cache=True, **kwargs):
404407
"""
405408
Query raw data contained in the ESO archive.
406409
@@ -437,12 +440,19 @@ def query_main(self, *, column_filters={}, columns=[],
437440
where_constraints_strlist = [f"{k} = {sanitize_val(v)}" for k, v in filters.items()]
438441
where_constraints = where_constraints_strlist
439442

443+
if print_help:
444+
help_query = \
445+
"select column_name, datatype from TAP_SCHEMA.columns where table_name = 'dbo.raw'"
446+
h = self.query_tap_service(help_query, cache=cache)
447+
log.info("Columns present in the table: %s", h)
448+
return
449+
440450
query = py2adql(table="dbo.raw",
441451
columns=columns,
442452
where_constraints=where_constraints,
443453
top=self.ROW_LIMIT)
444454

445-
return self._query_tap_service(query_str=query)
455+
return self.query_tap_service(query_str=query)
446456

447457
def get_headers(self, product_ids, *, cache=True):
448458
"""
@@ -572,8 +582,7 @@ def _download_eso_files(self, file_ids: List[str], destination: Optional[str],
572582
filename, downloaded = self._download_eso_file(file_link, destination, overwrite)
573583
downloaded_files.append(filename)
574584
if downloaded:
575-
log.info(f"Successfully downloaded dataset"
576-
f" {file_id} to {filename}")
585+
log.info(f"Successfully downloaded dataset {file_id} to {filename}")
577586
except requests.HTTPError as http_error:
578587
if http_error.response.status_code == 401:
579588
log.error(f"Access denied to {file_link}")
@@ -674,9 +683,10 @@ def get_associated_files(self, datasets: List[str], *, mode: str = "raw",
674683
# remove input datasets from calselector results
675684
return list(associated_files.difference(set(datasets)))
676685

677-
@deprecated_renamed_argument(('request_all_objects', 'request_id'), (None, None), since=['0.4.7', '0.4.7'])
678-
def retrieve_data(self, datasets, *, continuation=False, destination=None, with_calib=None,
679-
request_all_objects=False, unzip=True, request_id=None):
686+
@deprecated_renamed_argument(('request_all_objects', 'request_id'), (None, None),
687+
since=['0.4.7', '0.4.7'])
688+
def retrieve_data(self, datasets, *, continuation=False, destination=None, with_calib=None, unzip=True,
689+
request_all_objects=None, request_id=None):
680690
"""
681691
Retrieve a list of datasets form the ESO archive.
682692
@@ -711,6 +721,7 @@ def retrieve_data(self, datasets, *, continuation=False, destination=None, with_
711721
>>> files = Eso.retrieve_data(dpids)
712722
713723
"""
724+
_ = request_all_objects, request_id
714725
return_string = False
715726
if isinstance(datasets, str):
716727
return_string = True
@@ -742,14 +753,15 @@ def retrieve_data(self, datasets, *, continuation=False, destination=None, with_
742753
log.info("Done!")
743754
return files[0] if files and len(files) == 1 and return_string else files
744755

745-
def _activate_form(self, response, *, form_index=0, form_id=None, inputs={},
756+
def _activate_form(self, response, *, form_index=0, form_id=None, inputs=None,
746757
cache=True, method=None):
747758
"""
748759
Parameters
749760
----------
750761
method: None or str
751762
Can be used to override the form-specified method
752763
"""
764+
inputs = inputs or {}
753765
# Extract form from response
754766
root = BeautifulSoup(response.content, 'html5lib')
755767
if form_id is None:
@@ -776,7 +788,7 @@ def _activate_form(self, response, *, form_index=0, form_id=None, inputs={},
776788
elif form.attrs['enctype'] == 'application/x-www-form-urlencoded':
777789
fmt = 'application/x-www-form-urlencoded' # post(url, data=payload)
778790
else:
779-
raise Exception("enctype={0} is not supported!".format(form.attrs['enctype']))
791+
raise RuntimeError("enctype={0} is not supported!".format(form.attrs['enctype']))
780792
else:
781793
fmt = 'application/x-www-form-urlencoded' # post(url, data=payload)
782794
# Extract payload from form
@@ -885,7 +897,7 @@ def _activate_form(self, response, *, form_index=0, form_id=None, inputs={},
885897

886898
return response
887899

888-
def query_apex_quicklooks(self, *, project_id=None, help=False,
900+
def query_apex_quicklooks(self, *, project_id=None, print_help=False,
889901
open_form=False, cache=True, **kwargs):
890902
"""
891903
APEX data are distributed with quicklook products identified with a
@@ -903,8 +915,9 @@ def query_apex_quicklooks(self, *, project_id=None, help=False,
903915
table = None
904916
if open_form:
905917
webbrowser.open(apex_query_url)
906-
elif help:
907-
return self._print_instrument_help(apex_query_url, 'apex')
918+
elif print_help:
919+
# TODO: print some sensible help message
920+
return "No help available for apex at the moment."
908921
else:
909922

910923
payload = {'wdbo': 'csv/download'}
@@ -918,7 +931,7 @@ def query_apex_quicklooks(self, *, project_id=None, help=False,
918931
cache=cache, method='application/x-www-form-urlencoded')
919932

920933
content = apex_response.content
921-
if _check_response(content):
934+
if content:
922935
# First line is always garbage
923936
content = content.split(b'\n', 1)[1]
924937
try:
@@ -967,31 +980,31 @@ def _print_query_help(self, url, *, cache=True):
967980
checkbox_name = ""
968981
checkbox_value = ""
969982
for tag in section.next_siblings:
970-
if tag.name == u"table":
983+
if tag.name == "table":
971984
break
972-
elif tag.name == u"input":
973-
if tag.get(u'type') == u"checkbox":
985+
elif tag.name == "input":
986+
if tag.get('type') == "checkbox":
974987
checkbox_name = tag['name']
975-
checkbox_value = u"[x]" if ('checked' in tag.attrs) else u"[ ]"
988+
checkbox_value = "[x]" if ('checked' in tag.attrs) else "[ ]"
976989
name = ""
977990
value = ""
978991
else:
979992
name = tag['name']
980993
value = ""
981-
elif tag.name == u"select":
994+
elif tag.name == "select":
982995
options = []
983996
for option in tag.select("option"):
984997
options += ["{0} ({1})".format(option['value'], "".join(option.stripped_strings))]
985-
name = tag[u"name"]
998+
name = tag["name"]
986999
value = ", ".join(options)
9871000
else:
9881001
name = ""
9891002
value = ""
990-
if u"tab_" + name == checkbox_name:
1003+
if "tab_" + name == checkbox_name:
9911004
checkbox = checkbox_value
9921005
else:
9931006
checkbox = " "
994-
if name != u"":
1007+
if name != "":
9951008
result_string.append("{0} {1}: {2}"
9961009
.format(checkbox, name, value))
9971010

astroquery/eso/tests/test_eso.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def calselector_request(url, **kwargs):
8989
def test_sinfoni_SgrAstar(monkeypatch):
9090
# monkeypatch instructions from https://pytest.org/latest/monkeypatch.html
9191
eso = Eso()
92-
monkeypatch.setattr(eso, '_query_tap_service', monkey_tap)
92+
monkeypatch.setattr(eso, 'query_tap_service', monkey_tap)
9393
result = eso.query_instrument('sinfoni', target='SGRA')
9494
# test all results are there and the expected target is present
9595
assert len(result) == 50
@@ -99,7 +99,7 @@ def test_sinfoni_SgrAstar(monkeypatch):
9999
def test_main_SgrAstar(monkeypatch):
100100
# monkeypatch instructions from https://pytest.org/latest/monkeypatch.html
101101
eso = Eso()
102-
monkeypatch.setattr(eso, '_query_tap_service', monkey_tap)
102+
monkeypatch.setattr(eso, 'query_tap_service', monkey_tap)
103103
result = eso.query_main(target='SGR A', object='SGR A')
104104
# test all results are there and the expected target is present
105105
assert len(result) == 23
@@ -110,7 +110,7 @@ def test_main_SgrAstar(monkeypatch):
110110
def test_vvv(monkeypatch):
111111
# monkeypatch instructions from https://pytest.org/latest/monkeypatch.html
112112
eso = Eso()
113-
monkeypatch.setattr(eso, '_query_tap_service', monkey_tap)
113+
monkeypatch.setattr(eso, 'query_tap_service', monkey_tap)
114114
result = eso.query_collections(collections='VVV',
115115
coord1=266.41681662, coord2=-29.00782497,
116116
box='01 00 00',

0 commit comments

Comments
 (0)