Skip to content

Commit 72510b4

Browse files
authored
Merge pull request #2807 from esdc-esac-esa-int/ESA_ejwst-login_fixes
Fixes in JWST login
2 parents 7956e9a + 40c4098 commit 72510b4

File tree

10 files changed

+232
-63
lines changed

10 files changed

+232
-63
lines changed

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ esa.hubble
7575
- Product types in download_product method have been modified to: PRODUCT, SCIENCE_PRODUCT or POSTCARD. [#2597]
7676
- Added ``proposal`` keyword argument to several methods now allows to filter by Proposal ID. [#2797]
7777

78+
esa.jwt
79+
^^^^^^^
80+
81+
- Fixes in ``login`` and ``set_token`` methods. [#2807]
82+
7883
alma
7984
^^^^
8085

astroquery/esa/jwst/core.py

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ def set_token(self, token):
670670
subContext = conf.JWST_TOKEN
671671
data = urlencode({"token": token})
672672
connHandler = self.__jwsttap._TapPlus__getconnhandler()
673-
response = connHandler.execute_secure(subContext, data, True)
673+
response = connHandler.execute_secure(subcontext=subContext, data=data, verbose=True)
674674
if response.status == 403:
675675
print("ERROR: MAST tokens cannot be assigned or requested by anonymous users")
676676
elif response.status == 500:
@@ -690,7 +690,7 @@ def get_status_messages(self):
690690
if response.status == 200:
691691
for line in response:
692692
string_message = line.decode("utf-8")
693-
print(string_message[string_message.index('=')+1:])
693+
print(string_message[string_message.index('=') + 1:])
694694
except OSError:
695695
print("Status messages could not be retrieved")
696696

@@ -725,7 +725,7 @@ def get_product_list(self, *, observation_id=None,
725725
if observation_id is None:
726726
raise ValueError(self.REQUESTED_OBSERVATION_ID)
727727
plane_ids, max_cal_level = self._get_plane_id(observation_id=observation_id)
728-
if (cal_level == 3 and cal_level > max_cal_level):
728+
if cal_level == 3 and cal_level > max_cal_level:
729729
raise ValueError("Requesting upper levels is not allowed")
730730
list = self._get_associated_planes(plane_ids=plane_ids,
731731
cal_level=cal_level,
@@ -778,7 +778,7 @@ def _get_plane_id(self, observation_id):
778778
job.get_results().reverse()
779779
max_cal_level = job.get_results()["calibrationlevel"][0]
780780
for row in job.get_results():
781-
if (row["calibrationlevel"] == max_cal_level):
781+
if row["calibrationlevel"] == max_cal_level:
782782
planeids.append(
783783
JwstClass.get_decoded_string(row["planeid"]))
784784
return planeids, max_cal_level
@@ -898,8 +898,7 @@ def get_product(self, *, artifact_id=None, file_name=None):
898898
output_file_name = self._query_get_product(artifact_id=artifact_id)
899899
err_msg = str(artifact_id)
900900
except Exception as exx:
901-
raise ValueError('Cannot retrieve product for artifact_id '
902-
+ artifact_id + ': %s' % str(exx))
901+
raise ValueError(f"Cannot retrieve product for artifact_id {artifact_id}: {exx}")
903902
else:
904903
output_file_name = str(file_name)
905904
err_msg = str(file_name)
@@ -911,16 +910,14 @@ def get_product(self, *, artifact_id=None, file_name=None):
911910
params_dict['ARTIFACTID'] = (self._query_get_product(
912911
file_name=file_name))
913912
except Exception as exx:
914-
raise ValueError('Cannot retrieve product for file_name '
915-
+ file_name + ': %s' % str(exx))
913+
raise ValueError(f"Cannot retrieve product for file_name {file_name}: {exx}")
916914

917915
try:
918916
self.__jwsttap.load_data(params_dict=params_dict,
919917
output_file=output_file_name)
920918
except Exception as exx:
921919
log.info("error")
922-
raise ValueError('Error retrieving product for '
923-
+ err_msg + ': %s' % str(exx))
920+
raise ValueError(f"Error retrieving product for {err_msg}: {exx}")
924921
return output_file_name
925922

926923
def _query_get_product(self, *, artifact_id=None, file_name=None):
@@ -1002,8 +999,7 @@ def get_obs_products(self, *, observation_id=None, cal_level="ALL",
1002999
self.__jwsttap.load_data(params_dict=params_dict,
10031000
output_file=output_file_full_path)
10041001
except Exception as exx:
1005-
raise ValueError('Cannot retrieve products for observation '
1006-
+ observation_id + ': %s' % str(exx))
1002+
raise ValueError(f"Cannot retrieve products for observation {observation_id}: {exx}")
10071003

10081004
files = []
10091005
self.__extract_file(output_file_full_path=output_file_full_path,
@@ -1066,8 +1062,7 @@ def __set_dirs(self, output_file, observation_id):
10661062
try:
10671063
os.makedirs(output_dir, exist_ok=True)
10681064
except OSError as err:
1069-
raise OSError("Creation of the directory %s failed: %s"
1070-
% (output_dir, err.strerror))
1065+
raise OSError(f"Creation of the directory {output_dir} failed: {err.strerror}")
10711066
return output_file_full_path, output_dir
10721067

10731068
def __set_additional_parameters(self, param_dict, cal_level,
@@ -1086,10 +1081,9 @@ def __set_additional_parameters(self, param_dict, cal_level,
10861081

10871082
def __get_quantity_input(self, value, msg):
10881083
if value is None:
1089-
raise ValueError("Missing required argument: '"+str(msg)+"'")
1084+
raise ValueError(f"Missing required argument: '{msg}'")
10901085
if not (isinstance(value, str) or isinstance(value, units.Quantity)):
1091-
raise ValueError(
1092-
str(msg) + " must be either a string or astropy.coordinates")
1086+
raise ValueError(f"{msg} must be either a string or units.Quantity")
10931087
if isinstance(value, str):
10941088
q = Quantity(value)
10951089
return q
@@ -1099,8 +1093,7 @@ def __get_quantity_input(self, value, msg):
10991093
def __get_coord_input(self, value, msg):
11001094
if not (isinstance(value, str) or isinstance(value,
11011095
commons.CoordClasses)):
1102-
raise ValueError(
1103-
str(msg) + " must be either a string or astropy.coordinates")
1096+
raise ValueError(f"{msg} must be either a string or astropy.coordinates")
11041097
if isinstance(value, str):
11051098
c = commons.parse_coordinates(value)
11061099
return c
@@ -1113,7 +1106,7 @@ def __get_observationid_condition(self, *, value=None):
11131106
if (not isinstance(value, str)):
11141107
raise ValueError("observation_id must be string")
11151108
else:
1116-
condition = " AND observationid LIKE '"+value.lower()+"' "
1109+
condition = f" AND observationid LIKE '{value.lower()}' "
11171110
return condition
11181111

11191112
def __get_callevel_condition(self, cal_level):
@@ -1122,8 +1115,7 @@ def __get_callevel_condition(self, cal_level):
11221115
if (isinstance(cal_level, str) and cal_level == 'Top'):
11231116
condition = " AND max_cal_level=calibrationlevel "
11241117
elif (isinstance(cal_level, int)):
1125-
condition = " AND calibrationlevel=" +\
1126-
str(cal_level)+" "
1118+
condition = f" AND calibrationlevel={str(cal_level)} "
11271119
else:
11281120
raise ValueError("cal_level must be either "
11291121
"'Top' or an integer")
@@ -1139,15 +1131,13 @@ def __get_public_condition(self, only_public):
11391131

11401132
def __get_plane_dataproducttype_condition(self, *, prod_type=None):
11411133
condition = ""
1142-
if (prod_type is not None):
1143-
if (not isinstance(prod_type, str)):
1134+
if prod_type is not None:
1135+
if not isinstance(prod_type, str):
11441136
raise ValueError("prod_type must be string")
1145-
elif (str(prod_type).lower() not in self.PLANE_DATAPRODUCT_TYPES):
1146-
raise ValueError("prod_type must be one of: "
1147-
+ str(', '.join(self.PLANE_DATAPRODUCT_TYPES)))
1137+
elif str(prod_type).lower() not in self.PLANE_DATAPRODUCT_TYPES:
1138+
raise ValueError("prod_type must be one of: {str(', '.join(self.PLANE_DATAPRODUCT_TYPES))}")
11481139
else:
1149-
condition = " AND dataproducttype ILIKE '%"+prod_type.lower() + \
1150-
"%' "
1140+
condition = f" AND dataproducttype ILIKE '%{prod_type.lower()}%' "
11511141
return condition
11521142

11531143
def __get_instrument_name_condition(self, *, value=None):
@@ -1156,10 +1146,9 @@ def __get_instrument_name_condition(self, *, value=None):
11561146
if (not isinstance(value, str)):
11571147
raise ValueError("instrument_name must be string")
11581148
elif (str(value).upper() not in self.INSTRUMENT_NAMES):
1159-
raise ValueError("instrument_name must be one of: "
1160-
+ str(', '.join(self.INSTRUMENT_NAMES)))
1149+
raise ValueError(f"instrument_name must be one of: {str(', '.join(self.INSTRUMENT_NAMES))}")
11611150
else:
1162-
condition = " AND instrument_name ILIKE '%"+value.upper()+"%' "
1151+
condition = f" AND instrument_name ILIKE '%{value.upper()}%' "
11631152
return condition
11641153

11651154
def __get_filter_name_condition(self, *, value=None):
@@ -1169,7 +1158,7 @@ def __get_filter_name_condition(self, *, value=None):
11691158
raise ValueError("filter_name must be string")
11701159

11711160
else:
1172-
condition = " AND energy_bandpassname ILIKE '%"+value+"%' "
1161+
condition = f" AND energy_bandpassname ILIKE '%{value}%' "
11731162
return condition
11741163

11751164
def __get_proposal_id_condition(self, *, value=None):
@@ -1179,7 +1168,7 @@ def __get_proposal_id_condition(self, *, value=None):
11791168
raise ValueError("proposal_id must be string")
11801169

11811170
else:
1182-
condition = " AND proposal_id ILIKE '%"+value+"%' "
1171+
condition = f" AND proposal_id ILIKE '%{value}%' "
11831172
return condition
11841173

11851174
def __get_artifact_producttype_condition(self, *, product_type=None):
@@ -1188,10 +1177,9 @@ def __get_artifact_producttype_condition(self, *, product_type=None):
11881177
if (not isinstance(product_type, str)):
11891178
raise ValueError("product_type must be string")
11901179
elif (product_type not in self.ARTIFACT_PRODUCT_TYPES):
1191-
raise ValueError("product_type must be one of: "
1192-
+ str(', '.join(self.ARTIFACT_PRODUCT_TYPES)))
1180+
raise ValueError(f"product_type must be one of: {str(', '.join(self.ARTIFACT_PRODUCT_TYPES))}")
11931181
else:
1194-
condition = " AND producttype ILIKE '%"+product_type+"%'"
1182+
condition = f" AND producttype ILIKE '%{product_type}%'"
11951183
return condition
11961184

11971185
@staticmethod
@@ -1217,7 +1205,7 @@ def gzip_uncompress_and_rename_single_file(input_file):
12171205
os.remove(input_file)
12181206
if file.lower().endswith(".gz"):
12191207
# remove .gz
1220-
new_file_name = file[:len(file)-3]
1208+
new_file_name = file[:len(file) - 3]
12211209
output = output_dir + os.sep + new_file_name
12221210
else:
12231211
output = input_file

astroquery/esa/jwst/tests/DummyTapHandler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ def check_method(self, method):
4848
if method == self.__invokedMethod:
4949
return
5050
else:
51-
raise ValueError(f"Method '+{str(method)}"
52-
+ "' not invoked. (Invoked method is '"
53-
+ f"{str(self.__invokedMethod)}"+"')")
51+
raise ValueError(f"Method '+{str(method)}' not invoked. (Invoked method is '{str(self.__invokedMethod)}')")
5452

5553
def check_parameters(self, parameters, method_name):
5654
print("FOUND")

astroquery/esa/jwst/tests/test_jwstdata.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111
import os
1212
import pytest
13+
from requests import HTTPError
1314

1415
from astroquery.esa.jwst.tests.DummyTapHandler import DummyTapHandler
1516
from astroquery.esa.jwst.core import JwstClass
@@ -57,3 +58,11 @@ def test_get_product(self):
5758
parameters['verbose'] = False
5859
jwst.get_product(artifact_id='00000000-0000-0000-8740-65e2827c9895')
5960
dummyTapHandler.check_call('load_data', parameters)
61+
62+
63+
@pytest.mark.remote_data
64+
def test_login_error():
65+
jwst = JwstClass()
66+
with pytest.raises(HTTPError) as err:
67+
jwst.login(user="dummy", password="dummy")
68+
assert "Unauthorized" in err.value.args[0]

0 commit comments

Comments
 (0)