Skip to content

Commit ad6cb80

Browse files
authored
Merge pull request #2386 from jd-au/CASDA-Use-QueryWithLogin
Switch CASDA to QueryWithLogin
2 parents f20c85b + 740b05a commit ad6cb80

File tree

8 files changed

+199
-77
lines changed

8 files changed

+199
-77
lines changed

.mailmap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Hans Moritz Guenter <[email protected]>
2828
2929
3030
Jaladh Singhal <[email protected]>
31+
3132
Javier Ballester <[email protected]>
3233
Javier Duran <[email protected]>
3334

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ casda
2323

2424
- Add the ability to produce 2D and 3D cutouts from ASKAP images and cubes. [#2366]
2525

26+
- Use the standard ``login`` method for authenticating, which supports the system
27+
keyring [#2386]
28+
2629
jplsbdb
2730
^^^^^^^
2831

astroquery/casda/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Conf(_config.ConfigNamespace):
1111

1212
server = _config.ConfigItem(
1313
['https://casda.csiro.au/casda_vo_tools/sia2/query'],
14-
'Name of the CASDA SIA server to use.'
14+
'Address of the CASDA SIA server to use.'
1515
)
1616
timeout = _config.ConfigItem(
1717
30,
@@ -25,6 +25,14 @@ class Conf(_config.ConfigNamespace):
2525
['https://casda.csiro.au/casda_data_access/'],
2626
'Address of the CASDA SODA server'
2727
)
28+
login_url = _config.ConfigItem(
29+
['https://data.csiro.au/casda_vo_proxy/vo/tap/availability'],
30+
'Address for test logins'
31+
)
32+
username = _config.ConfigItem(
33+
'',
34+
'Optional default username for CASDA archive.'
35+
)
2836

2937

3038
conf = Conf()

astroquery/casda/core.py

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
from xml.etree import ElementTree
99
from datetime import datetime, timezone
10+
import keyring
1011

1112
# 2. third party imports
1213
import astropy.units as u
@@ -17,24 +18,22 @@
1718
import numpy as np
1819

1920
# 3. local imports - use relative imports
20-
# commonly required local imports shown below as example
21-
# all Query classes should inherit from BaseQuery.
22-
from ..query import BaseQuery
23-
# has common functions required by most modules
21+
from ..query import QueryWithLogin
2422
from ..utils import commons
2523
# prepend_docstr is a way to copy docstrings between methods
2624
from ..utils import prepend_docstr_nosections
2725
# async_to_sync generates the relevant query tools from _async methods
2826
from ..utils import async_to_sync
2927
# import configurable items declared in __init__.py
3028
from . import conf
29+
from ..exceptions import LoginError
3130

3231
# export all the public classes and methods
3332
__all__ = ['Casda', 'CasdaClass']
3433

3534

3635
@async_to_sync
37-
class CasdaClass(BaseQuery):
36+
class CasdaClass(QueryWithLogin):
3837

3938
"""
4039
Class for accessing ASKAP data through the CSIRO ASKAP Science Data Archive (CASDA). Typical usage:
@@ -46,18 +45,62 @@ class CasdaClass(BaseQuery):
4645
URL = conf.server
4746
TIMEOUT = conf.timeout
4847
POLL_INTERVAL = conf.poll_interval
48+
USERNAME = conf.username
4949
_soda_base_url = conf.soda_base_url
50+
_login_url = conf.login_url
5051
_uws_ns = {'uws': 'http://www.ivoa.net/xml/UWS/v1.0'}
5152

52-
def __init__(self, user=None, password=None):
53+
def __init__(self):
5354
super().__init__()
54-
if user is None:
55-
self._authenticated = False
55+
56+
def _login(self, *, username=None, store_password=False,
57+
reenter_password=False):
58+
"""
59+
login to non-public data as a known user
60+
61+
Parameters
62+
----------
63+
username : str, optional
64+
Username to the CASDA archive, uses ATNF OPAL credentials. If not given, it should be
65+
specified in the config file.
66+
store_password : bool, optional
67+
Stores the password securely in your keyring. Default is False.
68+
reenter_password : bool, optional
69+
Asks for the password even if it is already stored in the
70+
keyring. This is the way to overwrite an already stored passwork
71+
on the keyring. Default is False.
72+
"""
73+
74+
if username is None:
75+
if not self.USERNAME:
76+
raise LoginError("If you do not pass a username to login(), "
77+
"you should configure a default one!")
78+
else:
79+
username = self.USERNAME
80+
81+
# Get password from keyring or prompt
82+
password, password_from_keyring = self._get_password(
83+
"astroquery:casda.csiro.au", username, reenter=reenter_password)
84+
85+
# Login to CASDA to test credentals
86+
log.info("Authenticating {0} on CASDA ...".format(username))
87+
auth = (username, password)
88+
login_response = self._request("GET", self._login_url, auth=auth,
89+
timeout=self.TIMEOUT, cache=False)
90+
authenticated = login_response.status_code == 200
91+
92+
if authenticated:
93+
log.info("Authentication successful!")
94+
self.USERNAME = username
95+
self._auth = (username, password)
96+
97+
# When authenticated, save password in keyring if needed
98+
if password_from_keyring is None and store_password:
99+
keyring.set_password("astroquery:casda.csiro.au", username, password)
56100
else:
57-
self._authenticated = True
58-
# self._user = user
59-
# self._password = password
60-
self._auth = (user, password)
101+
log.exception("Authentication failed")
102+
103+
return authenticated
61104

62105
def query_region_async(self, coordinates, radius=1*u.arcmin, height=None, width=None,
63106
get_query_payload=False, cache=True):
@@ -274,7 +317,7 @@ def stage_data(self, table, verbose=False):
274317

275318
return self._complete_job(job_url, verbose)
276319

277-
def cutout(self, table, *, coordinates=None, radius=None, height=None,
320+
def cutout(self, table, *, coordinates=None, radius=1*u.arcmin, height=None,
278321
width=None, band=None, channel=None, verbose=False):
279322
"""
280323
Produce a cutout from each selected file. All requests for data must use authentication. If you have access to
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<availability xmlns="http://www.ivoa.net/xml/VOSIAvailability/v1.0">
3+
<available>true</available>
4+
<upSince>2022-04-07T11:11:40.006Z</upSince>
5+
<note></note>
6+
</availability>

0 commit comments

Comments
 (0)