Skip to content

Commit 15fc4ca

Browse files
committed
Adding the retype_password to the alma and cosmosim login method, too
1 parent 86cfcad commit 15fc4ca

File tree

2 files changed

+65
-8
lines changed

2 files changed

+65
-8
lines changed

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+
retype_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+
retype_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 retype_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/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+
retype_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+
retype_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 retype_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:

0 commit comments

Comments
 (0)