Skip to content

Commit 206b256

Browse files
committed
Merge pull request #679 from bsipocz/keyring_login_add_retype_password
Add retype_password parameter for keyring using login()s
2 parents ab3a753 + 1746a29 commit 206b256

File tree

5 files changed

+81
-17
lines changed

5 files changed

+81
-17
lines changed

astroquery/alma/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ class Conf(_config.ConfigNamespace):
1010
Configuration parameters for `astroquery.alma`.
1111
"""
1212

13-
timeout = _config.ConfigItem(60, "Timeout in seconds")
13+
timeout = _config.ConfigItem(60, "Timeout in seconds.")
1414

1515
archive_url = _config.ConfigItem(
1616
['http://almascience.org',
1717
'http://almascience.eso.org',
1818
'http://almascience.nrao.edu',
1919
'http://almascience.nao.ac.jp',
2020
'http://beta.cadc-ccda.hia-iha.nrc-cnrc.gc.ca'],
21-
'The ALMA Archive mirror to use')
21+
'The ALMA Archive mirror to use.')
22+
23+
username = _config.ConfigItem(
24+
"",
25+
'Optional default username for ALMA archive.')
2226

2327
conf = Conf()
2428

astroquery/alma/core.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import astropy.io.votable as votable
2525

2626
from ..exceptions import (RemoteServiceError, TableParseError,
27-
InvalidQueryError)
27+
InvalidQueryError, LoginError)
2828
from ..utils import commons, system_tools
2929
from ..utils.process_asyncs import async_to_sync
3030
from ..query import QueryWithLogin
@@ -38,6 +38,7 @@ class AlmaClass(QueryWithLogin):
3838

3939
TIMEOUT = conf.timeout
4040
archive_url = conf.archive_url
41+
USERNAME = conf.username
4142

4243
def __init__(self):
4344
super(AlmaClass, self).__init__()
@@ -394,7 +395,31 @@ def _parse_result(self, response, verbose=False):
394395
table = first_table.to_table(use_names_over_ids=True)
395396
return table
396397

397-
def _login(self, username, store_password=False):
398+
def _login(self, username=None, store_password=False,
399+
reenter_password=False):
400+
"""
401+
Login to the ALMA Science Portal.
402+
403+
Parameters
404+
----------
405+
username : str, optional
406+
Username to the ALMA Science Portal. If not given, it should be
407+
specified in the config file.
408+
store_password : bool, optional
409+
Stores the password securely in your keyring. Default is False.
410+
reenter_password : bool, optional
411+
Asks for the password even if it is already stored in the
412+
keyring. This is the way to overwrite an already stored passwork
413+
on the keyring. Default is False.
414+
"""
415+
416+
if username is None:
417+
if self.USERNAME == "":
418+
raise LoginError("If you do not pass a username to login(), "
419+
"you should configure a default one!")
420+
else:
421+
username = self.USERNAME
422+
398423
# Check if already logged in
399424
loginpage = self._request("GET", "https://asa.alma.cl/cas/login",
400425
cache=False)
@@ -404,8 +429,12 @@ def _login(self, username, store_password=False):
404429
return True
405430

406431
# Get password from keyring or prompt
407-
password_from_keyring = keyring.get_password("astroquery:asa.alma.cl",
408-
username)
432+
if reenter_password is False:
433+
password_from_keyring = keyring.get_password(
434+
"astroquery:asa.alma.cl", username)
435+
else:
436+
password_from_keyring = None
437+
409438
if password_from_keyring is None:
410439
if system_tools.in_ipynb():
411440
log.warn("You may be using an ipython notebook:"

astroquery/cosmosim/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@ class Conf(_config.ConfigNamespace):
2121

2222
query_url = _config.ConfigItem(
2323
['http://www.cosmosim.org/uws/query'],
24-
'CosmoSim UWS query URL')
24+
'CosmoSim UWS query URL.')
2525
schema_url = _config.ConfigItem(
2626
['http://www.cosmosim.org/query/account/databases/json'],
27-
'CosmoSim json query URL for generating database schema')
27+
'CosmoSim json query URL for generating database schema.')
2828
timeout = _config.ConfigItem(
2929
60.0,
30-
'Timeout for CosmoSim query')
30+
'Timeout for CosmoSim query.')
31+
username = _config.ConfigItem(
32+
"",
33+
'Optional default username for CosmoSim database.')
3134

3235
conf = Conf()
3336

astroquery/cosmosim/core.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# Astroquery imports
2424
from ..query import QueryWithLogin
2525
from . import conf
26+
from ..exceptions import LoginError
2627

2728
conf.max_lines = -1
2829
conf.max_width = -1
@@ -35,11 +36,34 @@ class CosmoSimClass(QueryWithLogin):
3536
QUERY_URL = conf.query_url
3637
SCHEMA_URL = conf.schema_url
3738
TIMEOUT = conf.timeout
39+
USERNAME = conf.username
3840

3941
def __init__(self):
4042
super(CosmoSimClass, self).__init__()
4143

42-
def _login(self, username, password=None, store_password=False):
44+
def _login(self, username=None, password=None, store_password=False,
45+
reenter_password=False):
46+
"""
47+
Login to the CosmoSim database.
48+
49+
Parameters
50+
----------
51+
username : str, optional
52+
Username to the CosmoSim database. If not given, it should be
53+
specified in the config file.
54+
store_password : bool, optional
55+
Stores the password securely in your keyring. Default is False.
56+
reenter_password : bool, optional
57+
Asks for the password even if it is already stored in the
58+
keyring. This is the way to overwrite an already stored passwork
59+
on the keyring. Default is False.
60+
"""
61+
if username is None:
62+
if self.USERNAME == "":
63+
raise LoginError("If you do not pass a username to login(), "
64+
"you should configure a default one!")
65+
else:
66+
username = self.USERNAME
4367

4468
# login after logging out (interactive)
4569
if not hasattr(self, 'session'):
@@ -55,9 +79,13 @@ def _login(self, username, password=None, store_password=False):
5579
self.username = username
5680

5781
# Get password from keyring or prompt
58-
password_from_keyring = keyring.get_password(
59-
"astroquery:www.cosmosim.org", self.username)
60-
if not password_from_keyring:
82+
if reenter_password is False:
83+
password_from_keyring = keyring.get_password(
84+
"astroquery:www.cosmosim.org", self.username)
85+
else:
86+
password_from_keyring = None
87+
88+
if password_from_keyring is None:
6189
logging.warning("No password was found in the keychain for the "
6290
"provided username.")
6391
if password:

astroquery/eso/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _activate_form(self, response, form_index=0, inputs={}, cache=True,
171171
return response
172172

173173
def _login(self, username=None, store_password=False,
174-
retype_password=False):
174+
reenter_password=False):
175175
"""
176176
Login to the ESO User Portal.
177177
@@ -182,7 +182,7 @@ def _login(self, username=None, store_password=False,
182182
specified in the config file.
183183
store_password : bool, optional
184184
Stores the password securely in your keyring. Default is False.
185-
retype_password : bool, optional
185+
reenter_password : bool, optional
186186
Asks for the password even if it is already stored in the
187187
keyring. This is the way to overwrite an already stored passwork
188188
on the keyring. Default is False.
@@ -195,7 +195,7 @@ def _login(self, username=None, store_password=False,
195195
username = self.USERNAME
196196

197197
# Get password from keyring or prompt
198-
if retype_password is False:
198+
if reenter_password is False:
199199
password_from_keyring = keyring.get_password(
200200
"astroquery:www.eso.org", username)
201201
else:
@@ -318,7 +318,7 @@ def query_surveys(self, surveys='', cache=True,
318318
query_dict["max_rows_returned"] = self.ROW_LIMIT
319319
else:
320320
query_dict["max_rows_returned"] = 10000
321-
321+
322322
survey_response = self._activate_form(survey_form, form_index=0,
323323
inputs=query_dict, cache=cache)
324324

0 commit comments

Comments
 (0)