Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion astroquery/cosmosim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ class Conf(_config.ConfigNamespace):

conf = Conf()

from .core import CosmoSim,CosmoSimClass
from .core import CosmoSim, CosmoSimClass

__all__ = ['CosmoSim', 'CosmoSimClass', 'Conf', 'conf']
72 changes: 40 additions & 32 deletions astroquery/cosmosim/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
from ..query import QueryWithLogin
from . import conf

__all__ = ['CosmoSim']
# temp imports
import ipdb

#__all__ = ['CosmoSim']
__doctest_skip__ = ['CosmoSim.*']

class CosmoSimClass(QueryWithLogin):

Expand Down Expand Up @@ -59,17 +62,17 @@ def _login(self, username, password=None, store_password=False):
if not password_from_keyring:
logging.warning("No password was found in the keychain for the provided username.")
if password:
self.password = password
self._password = password
else:
self.password = getpass.getpass("{0}, enter your CosmoSim password:\n".format(self.username))
self._password = getpass.getpass("{0}, enter your CosmoSim password:\n".format(self.username))
else:
logging.warning("Using the password found in the keychain for the provided username.")
self.password = password_from_keyring
self._password = password_from_keyring

# Authenticate
warnings.warn("Authenticating {0} on www.cosmosim.org...".format(self.username))
authenticated = self._request('POST', CosmoSim.QUERY_URL,
auth=(self.username, self.password),
auth=(self.username, self._password),
cache=False)
if authenticated.status_code == 200:
warnings.warn("Authentication successful!")
Expand All @@ -82,12 +85,17 @@ def _login(self, username, password=None, store_password=False):
self._existing_tables()

if authenticated.status_code == 200 and password_from_keyring is None and store_password:
keyring.set_password("astroquery:www.cosmosim.org", self.username, self.password)
keyring.set_password("astroquery:www.cosmosim.org", self.username, self._password)

# Delete job; prevent them from piling up with phase PENDING
if authenticated.status_code == 200:
soup = BeautifulSoup(authenticated.content)
self.delete_job(jobid=str(soup.find("uws:jobid").string),squash=True)
# on first login attempt, no field 'uws:jobid', but doesn't
# seem to create a job when logging in
try:
self.delete_job(jobid=str(soup.find("uws:jobid").string),squash=True)
except:
pass

return authenticated

Expand All @@ -103,7 +111,7 @@ def logout(self, deletepw=False):
-------
"""

if hasattr(self,'username') and hasattr(self,'password') and hasattr(self,'session'):
if hasattr(self,'username') and hasattr(self,'_password') and hasattr(self,'session'):
if deletepw is True:
try:
keyring.delete_password("astroquery:www.cosmosim.org", self.username)
Expand All @@ -113,7 +121,7 @@ def logout(self, deletepw=False):

del self.session
del self.username
del self.password
del self._password
else:
logging.error("You must log in before attempting to logout.")

Expand All @@ -122,9 +130,9 @@ def check_login_status(self):
Public function which checks the status of a user login attempt.
"""

if hasattr(self,'username') and hasattr(self,'password') and hasattr(self,'session'):
if hasattr(self,'username') and hasattr(self,'_password') and hasattr(self,'session'):
authenticated = self._request('POST', CosmoSim.QUERY_URL,
auth=(self.username,self.password),
auth=(self.username,self._password),
cache=False)
if authenticated.status_code == 200:
warnings.warn("Status: You are logged in as {}.".format(self.username))
Expand Down Expand Up @@ -171,7 +179,7 @@ def run_sql_query(self, query_string, tablename=None, queue=None,
if tablename in self.table_dict.values():
result = self._request('POST',
CosmoSim.QUERY_URL,
auth=(self.username,self.password),
auth=(self.username,self._password),
data={'query':query_string,'phase':'run',
'queue':queue},
cache=cache)
Expand All @@ -186,13 +194,13 @@ def run_sql_query(self, query_string, tablename=None, queue=None,
warnings.warn("Generated table name: {}".format(gen_tablename))
elif tablename is None:
result = self._request('POST', CosmoSim.QUERY_URL,
auth=(self.username, self.password),
auth=(self.username, self._password),
data={'query':query_string, 'phase':'run',
'queue':queue},
cache=cache)
else:
result = self._request('POST', CosmoSim.QUERY_URL,
auth=(self.username, self.password),
auth=(self.username, self._password),
data={'query':query_string,
'table':'{}'.format(tablename),
'phase':'run', 'queue':queue},
Expand Down Expand Up @@ -256,7 +264,7 @@ def check_job_status(self,jobid=None):

response = self._request('GET',
CosmoSim.QUERY_URL+'/{}'.format(jobid)+'/phase',
auth=(self.username, self.password),
auth=(self.username, self._password),
data={'print':'b'},cache=False)
logging.info("Job {}: {}".format(jobid,response.content))
return response.content
Expand Down Expand Up @@ -285,7 +293,7 @@ def check_all_jobs(self, phase=None, regex=None, sortby=None):
"""

checkalljobs = self._request('GET', CosmoSim.QUERY_URL,
auth=(self.username, self.password),
auth=(self.username, self._password),
params={'print':'b'},cache=False)

self.job_dict={}
Expand Down Expand Up @@ -379,8 +387,8 @@ def check_all_jobs(self, phase=None, regex=None, sortby=None):
if not phase and not regex:
if not sortby:
t = Table()
t['JobID'] = self.job_dict.keys()
t['Phase'] = self.job_dict.values()
t['JobID'] = [i for i in self.job_dict.keys()]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could use list(self.job_dict.keys()) for the same effect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - I definitely tried casting to a list straight away. However, oddly enough, it gave me issues. I had to actually do a list comprehension to fix this. Nonetheless, I'm going to see if it works.

t['Phase'] = [i for i in self.job_dict.values()]
t.pprint()
else:
if sortby.upper() == 'TABLENAME':
Expand Down Expand Up @@ -510,7 +518,7 @@ def completed_job_info(self,jobid=None,output=False):
completed_jobids = [key for key in self.job_dict.keys() if self.job_dict[key] == 'COMPLETED']
response_list = [self._request('GET',
CosmoSim.QUERY_URL+"/{}".format(completed_jobids[i]),
auth=(self.username, self.password),cache=False)
auth=(self.username, self._password),cache=False)
for i in range(len(completed_jobids))]
self.response_dict_current = {}
for i,vals in enumerate(completed_jobids):
Expand All @@ -520,7 +528,7 @@ def completed_job_info(self,jobid=None,output=False):
response_list = [self._request('GET',
CosmoSim.QUERY_URL+"/{}".format(jobid),
auth=(self.username,
self.password),cache=False)]
self._password),cache=False)]
self.response_dict_current = {}
self.response_dict_current[jobid] = self._generate_response_dict(response_list[0])
else:
Expand Down Expand Up @@ -587,7 +595,7 @@ def _starttime_dict(self):
if self.job_dict[key] == 'COMPLETED']
response_list = [self._request('GET',
CosmoSim.QUERY_URL+"/{}".format(i),
auth=(self.username,self.password),cache=False)
auth=(self.username,self._password),cache=False)
for i in completed_ids]
soups = [BeautifulSoup(response_list[i].content) for i in range(len(response_list))]
self.starttime_dict = {}
Expand Down Expand Up @@ -626,7 +634,7 @@ def general_job_info(self,jobid=None,output=False):
response_list = [self._request('GET',
CosmoSim.QUERY_URL+"/{}".format(jobid),
auth=(self.username,
self.password),cache=False)]
self._password),cache=False)]
if response_list[0].ok is False:
logging.error('Must provide a valid jobid.')
return
Expand Down Expand Up @@ -675,7 +683,7 @@ def delete_job(self,jobid=None,squash=None):

if self.job_dict[jobid] in ['COMPLETED','ERROR','ABORTED','PENDING']:
result = self.session.delete(CosmoSim.QUERY_URL+"/{}".format(jobid),
auth=(self.username, self.password),
auth=(self.username, self._password),
data={'follow':''})
else:
warnings.warn("Can only delete a job with phase: 'COMPLETED', 'ERROR', 'ABORTED', or 'PENDING'.")
Expand Down Expand Up @@ -724,7 +732,7 @@ def delete_all_jobs(self,phase=None,regex=None):
if self.table_dict[key] in matching_tables:
result = self.session.delete(CosmoSim.QUERY_URL+"/{}".format(key),
auth=(self.username,
self.password),
self._password),
data={'follow':''})
if not result.ok:
result.raise_for_status()
Expand All @@ -734,7 +742,7 @@ def delete_all_jobs(self,phase=None,regex=None):
if self.job_dict[key] in phase:
result = self.session.delete(CosmoSim.QUERY_URL+"/{}".format(key),
auth=(self.username,
self.password),
self._password),
data={'follow':''})
if not result.ok:
result.raise_for_status()
Expand All @@ -747,7 +755,7 @@ def delete_all_jobs(self,phase=None,regex=None):
if self.table_dict[key] in matching_tables:
result = self.session.delete(CosmoSim.QUERY_URL+"/{}".format(key),
auth=(self.username,
self.password),
self._password),
data={'follow':''})
if not result.ok:
result.raise_for_status()
Expand All @@ -756,7 +764,7 @@ def delete_all_jobs(self,phase=None,regex=None):
for key in self.job_dict.keys():
result = self.session.delete(CosmoSim.QUERY_URL+"/{}".format(key),
auth=(self.username,
self.password),
self._password),
data={'follow':''})
if not result.ok:
result.raise_for_status()
Expand All @@ -772,7 +780,7 @@ def _generate_schema(self):
"""

response = self._request('GET', CosmoSim.SCHEMA_URL,
auth=(self.username,self.password),
auth=(self.username,self._password),
headers={'Accept': 'application/json'},
cache=False)
data = response.json()
Expand Down Expand Up @@ -1027,7 +1035,7 @@ def download(self,jobid=None,filename=None,format=None,cache=True):
if format:
results = self._request('GET',
self.QUERY_URL+"/{}/results".format(jobid),
auth=(self.username,self.password))
auth=(self.username,self._password))
soup = BeautifulSoup(results.content)
urls = [i.get('xlink:href') for i in soup.findAll('uws:result')]
formatlist = [urls[i].split('/')[-1].upper() for i in range(len(urls))]
Expand All @@ -1038,12 +1046,12 @@ def download(self,jobid=None,filename=None,format=None,cache=True):
if filename:
self._download_file(downloadurl,
local_filepath=filename,
auth=(self.username,self.password))
auth=(self.username,self._password))
elif not filename:
if format.upper() == 'CSV':
raw_table_data = self._request('GET',
downloadurl,
auth=(self.username,self.password),
auth=(self.username,self._password),
cache=cache).content
raw_headers = raw_table_data.split('\n')[0]
num_cols = len(raw_headers.split(','))
Expand All @@ -1061,7 +1069,7 @@ def download(self,jobid=None,filename=None,format=None,cache=True):
tmp_downloadurl = urls[formatlist.index('CSV')]
raw_table_data = self._request('GET',
tmp_downloadurl,
auth=(self.username,self.password),
auth=(self.username,self._password),
cache=cache).content
raw_headers = raw_table_data.split('\n')[0]
num_cols = len(raw_headers.split(','))
Expand Down
12 changes: 12 additions & 0 deletions astroquery/cosmosim/tests/setup_package.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from __future__ import absolute_import

import os


def get_package_data():
paths = [os.path.join('data', '*.pickle'),
os.path.join('data', '*.html'),
os.path.join('data', '*.tbl'),
]
return {'astroquery.cosmosim.tests': paths}
44 changes: 44 additions & 0 deletions astroquery/cosmosim/tests/test_cosmosim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst

import os
import tempfile
import shutil
from astropy.tests.helper import pytest, remote_data
# VV temporary
import ipdb
# ^^ temporary
try:
import keyring
HAS_KEYRING = True
except:
HAS_KEYRING = False
try:
from ...cosmosim import CosmoSim
COSMOSIM_IMPORTED = True
except:
COSMOSIM_IMPORTED = False
from ...exceptions import LoginError
SKIP_TESTS = not(HAS_KEYRING and COSMOSIM_IMPORTED)

@pytest.mark.skipif('SKIP_TESTS')
@remote_data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this is remote, it should go into the test_cosmosim_remote file

class TestCosmoSim:
@pytest.fixture()
def temp_dir(self, request):
my_temp_dir = tempfile.mkdtemp()
def fin():
shutil.rmtree(my_temp_dir)
request.addfinalizer(fin)
return my_temp_dir

def test_login(self):
"""
Tests a simple login with public credentials.
"""
cosmosim = CosmoSim()
# wrong login credentials
with pytest.raises(LoginError) as exc:
cosmosim.login(username='public', password='wrong')
assert exc.value.args[0] == 'Authentication failed!'
cosmosim.logout()

23 changes: 19 additions & 4 deletions astroquery/cosmosim/tests/test_cosmosim_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,23 @@

SKIP_TESTS = not HAS_KEYRING

#@pytest.mark.skipif('SKIP_TESTS')
#@remote_data
#class TestEso:
# def __init__():
@pytest.mark.skipif('SKIP_TESTS')
@remote_data
class TestCosmoSim:

@pytest.fixture()
def temp_dir(self, request):
my_temp_dir = tempfile.mkdtemp()
def fin():
shutil.rmtree(my_temp_dir)
request.addfinalizer(fin)
return my_temp_dir

def test_login(self):
cs = CosmoSim()
# wrong login credentials
with pytest.raises(LoginError) as exc:
cs.login(username='public', password='wrong')
assert exc.value.args[0] == 'Authentication failed!'
cs.logout()

2 changes: 1 addition & 1 deletion astroquery/sdss/tests/setup_package.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ def get_package_data():
paths = [os.path.join('data', '*.txt'),
os.path.join('data', '*.fits')]

return {'astroquery.sdss.tests': paths}
return {'astroquery.cosmosim.tests': paths}