Skip to content

Commit 513135b

Browse files
committed
clean up log functions
1 parent 6849dd9 commit 513135b

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

astroquery/eso/core.py

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,6 @@ def __init__(self, timeout=None):
9898
def timeout(self):
9999
return self._timeout
100100

101-
@staticmethod
102-
def log_info(message):
103-
"Wrapper for logging function"
104-
log.info(message)
105-
106-
@staticmethod
107-
def log_warning(message):
108-
"Wrapper for logging function"
109-
log.warning(message)
110-
111-
@staticmethod
112-
def log_error(message):
113-
"Wrapper for logging function"
114-
log.error(message)
115-
116-
@staticmethod
117-
def log_debug(message):
118-
"Wrapper for logging function"
119-
log.debug(message)
120-
121101
@timeout.setter
122102
def timeout(self, value):
123103
if hasattr(value, 'to'):
@@ -152,12 +132,14 @@ def from_cache(self, query_str, cache_timeout):
152132
if not isinstance(cached_table, Table):
153133
cached_table = None
154134
else:
155-
self.log_debug(f"Cache expired for {table_file} ...")
135+
logmsg = (f"Cache expired for {table_file} ...")
136+
log.debug(logmsg)
156137
cached_table = None
157138
except FileNotFoundError:
158139
cached_table = None
159140
if cached_table:
160-
self.log_debug(f"Retrieved data from {table_file} ...")
141+
logmsg = (f"Retrieved data from {table_file} ...")
142+
log.debug(logmsg)
161143
return cached_table
162144

163145
def _authenticate(self, *, username: str, password: str) -> bool:
@@ -171,15 +153,18 @@ def _authenticate(self, *, username: str, password: str) -> bool:
171153
"client_secret": "clientSecret",
172154
"username": username,
173155
"password": password}
174-
self.log_info(f"Authenticating {username} on 'www.eso.org' ...")
156+
logmsg = (f"Authenticating {username} on 'www.eso.org' ...")
157+
log.info(logmsg)
175158
response = self._request('GET', self.AUTH_URL, params=url_params)
176159
if response.status_code == 200:
177160
token = json.loads(response.content)['id_token']
178161
self._auth_info = AuthInfo(username=username, password=password, token=token)
179-
self.log_info("Authentication successful!")
162+
logmsg = ("Authentication successful!")
163+
log.info(logmsg)
180164
return True
181165
else:
182-
self.log_error("Authentication failed!")
166+
logmsg = ("Authentication failed!")
167+
log.error(logmsg)
183168
return False
184169

185170
def _get_auth_info(self, username: str, *, store_password: bool = False,
@@ -232,7 +217,8 @@ def _login(self, *args, username: str = None, store_password: bool = False,
232217

233218
def _get_auth_header(self) -> Dict[str, str]:
234219
if self._auth_info and self._auth_info.expired():
235-
self.log_info("Authentication token has expired! Re-authenticating ...")
220+
logmsg = ("Authentication token has expired! Re-authenticating ...")
221+
log.info(logmsg)
236222
self._authenticate(username=self._auth_info.username,
237223
password=self._auth_info.password)
238224
if self._auth_info and not self._auth_info.expired():
@@ -330,7 +316,8 @@ def print_table_help(self, table_name: str) -> None:
330316
nlines = len(available_cols) + 2
331317
n_ = astropy.conf.max_lines
332318
astropy.conf.max_lines = nlines
333-
self.log_info(f"\nColumns present in the table {table_name}:\n{available_cols}\n")
319+
logmsg = (f"\nColumns present in the table {table_name}:\n{available_cols}\n")
320+
log.info(logmsg)
334321
astropy.conf.max_lines = n_
335322

336323
def _query_on_allowed_values(self,
@@ -527,7 +514,8 @@ def _download_eso_file(self, file_link: str, destination: str,
527514
filename = os.path.join(destination, filename)
528515
part_filename = filename + ".part"
529516
if os.path.exists(part_filename):
530-
self.log_info(f"Removing partially downloaded file {part_filename}")
517+
logmsg = (f"Removing partially downloaded file {part_filename}")
518+
log.info(logmsg)
531519
os.remove(part_filename)
532520
download_required = overwrite or not self._find_cached_file(filename)
533521
if download_required:
@@ -543,23 +531,29 @@ def _download_eso_files(self, file_ids: List[str], destination: Optional[str],
543531
destination = os.path.abspath(destination)
544532
os.makedirs(destination, exist_ok=True)
545533
nfiles = len(file_ids)
546-
self.log_info(f"Downloading {nfiles} files ...")
534+
logmsg = (f"Downloading {nfiles} files ...")
535+
log.info(logmsg)
547536
downloaded_files = []
548537
for i, file_id in enumerate(file_ids, 1):
549538
file_link = self.DOWNLOAD_URL + file_id
550-
self.log_info(f"Downloading file {i}/{nfiles} {file_link} to {destination}")
539+
logmsg = (f"Downloading file {i}/{nfiles} {file_link} to {destination}")
540+
log.info(logmsg)
551541
try:
552542
filename, downloaded = self._download_eso_file(file_link, destination, overwrite)
553543
downloaded_files.append(filename)
554544
if downloaded:
555-
self.log_info(f"Successfully downloaded dataset {file_id} to {filename}")
545+
logmsg = (f"Successfully downloaded dataset {file_id} to {filename}")
546+
log.info(logmsg)
556547
except requests.HTTPError as http_error:
557548
if http_error.response.status_code == 401:
558-
self.log_error(f"Access denied to {file_link}")
549+
logmsg = (f"Access denied to {file_link}")
550+
log.error(logmsg)
559551
else:
560-
self.log_error(f"Failed to download {file_link}. {http_error}")
552+
logmsg = (f"Failed to download {file_link}. {http_error}")
553+
log.error(logmsg)
561554
except RuntimeError as ex:
562-
self.log_error(f"Failed to download {file_link}. {ex}")
555+
logmsg = (f"Failed to download {file_link}. {ex}")
556+
log.error(logmsg)
563557
return downloaded_files
564558

565559
def _unzip_file(self, filename: str) -> str:
@@ -572,12 +566,14 @@ def _unzip_file(self, filename: str) -> str:
572566
if filename.endswith(('fits.Z', 'fits.gz')):
573567
uncompressed_filename = filename.rsplit(".", 1)[0]
574568
if not os.path.exists(uncompressed_filename):
575-
self.log_info(f"Uncompressing file {filename}")
569+
logmsg = (f"Uncompressing file {filename}")
570+
log.info(logmsg)
576571
try:
577572
subprocess.run([self.GUNZIP, filename], check=True)
578573
except Exception as ex:
579574
uncompressed_filename = None
580-
self.log_error(f"Failed to unzip {filename}: {ex}")
575+
logmsg = (f"Failed to unzip {filename}: {ex}")
576+
log.error(logmsg)
581577
return uncompressed_filename or filename
582578

583579
def _unzip_files(self, files: List[str]) -> List[str]:
@@ -598,7 +594,8 @@ def _save_xml(self, payload: bytes, filename: str, destination: str):
598594
destination = os.path.abspath(destination)
599595
os.makedirs(destination, exist_ok=True)
600596
filename = os.path.join(destination, filename)
601-
self.log_info(f"Saving Calselector association tree to {filename}")
597+
logmsg = (f"Saving Calselector association tree to {filename}")
598+
log.info(logmsg)
602599
with open(filename, "wb") as fd:
603600
fd.write(payload)
604601

@@ -707,7 +704,8 @@ def retrieve_data(self, datasets, *, continuation=False, destination=None,
707704

708705
associated_files = []
709706
if with_calib:
710-
self.log_info(f"Retrieving associated '{with_calib}' calibration files ...")
707+
logmsg = (f"Retrieving associated '{with_calib}' calibration files ...")
708+
log.info(logmsg)
711709
try:
712710
# batch calselector requests to avoid possible issues on the ESO server
713711
batch_size = 100
@@ -716,16 +714,20 @@ def retrieve_data(self, datasets, *, continuation=False, destination=None,
716714
associated_files += self.get_associated_files(
717715
sorted_datasets[i:i + batch_size], mode=with_calib)
718716
associated_files = list(set(associated_files))
719-
self.log_info(f"Found {len(associated_files)} associated files")
717+
logmsg = (f"Found {len(associated_files)} associated files")
718+
log.info(logmsg)
720719
except Exception as ex:
721-
self.log_error(f"Failed to retrieve associated files: {ex}")
720+
logmsg = (f"Failed to retrieve associated files: {ex}")
721+
log.error(logmsg)
722722

723723
all_datasets = datasets + associated_files
724-
self.log_info("Downloading datasets ...")
724+
logmsg = ("Downloading datasets ...")
725+
log.info(logmsg)
725726
files = self._download_eso_files(all_datasets, destination, continuation)
726727
if unzip:
727728
files = self._unzip_files(files)
728-
self.log_info("Done!")
729+
logmsg = ("Done!")
730+
log.info(logmsg)
729731
return files[0] if files and len(files) == 1 and return_string else files
730732

731733
@deprecated_renamed_argument(('open_form', 'help'), (None, 'print_help'),

0 commit comments

Comments
 (0)