From 73a24b4b330e6a28bde54d16d45ff3bfc9f4f7a9 Mon Sep 17 00:00:00 2001 From: Michael Tauraso Date: Fri, 10 Jan 2025 15:46:38 -0800 Subject: [PATCH] Adding credentials file support --- .gitignore | 2 ++ src/fibad/download.py | 30 +++++++++++++++++++++++++---- src/fibad/fibad_default_config.toml | 13 +++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index b7d6804..f815974 100644 --- a/.gitignore +++ b/.gitignore @@ -153,5 +153,7 @@ _html/ data/ results/ example_model.pth + # Common location to stash personal or notebook example config fibad_config.toml +credentials.ini diff --git a/src/fibad/download.py b/src/fibad/download.py index ae49d20..e8bcbaf 100644 --- a/src/fibad/download.py +++ b/src/fibad/download.py @@ -4,6 +4,7 @@ from threading import Thread from typing import Optional, Union +import toml from astropy.io import fits from astropy.table import Table, hstack @@ -49,12 +50,33 @@ def run(self): logger.info("Download command Start") - username = self.config["download"]["username"] - password = self.config["download"]["password"] + credentials_file = self.config["download"]["credentials_file"] + credentials_file_path = Path(credentials_file) if credentials_file else None + + credentials_configured = bool( + self.config["download"]["username"] or self.config["download"]["password"] + ) + + if credentials_file_path.exists() and credentials_configured: + msg = f"Credentials file {credentials_file} found in addition to username/password in your " + msg += "fibad config. Credentials must be provided in only one place." + raise RuntimeError(msg) + + if credentials_file_path.exists(): + credentials = toml.load(credentials_file_path) + username = credentials.get("username", False) + password = credentials.get("password", False) + else: + username = self.config["download"]["username"] + password = self.config["download"]["password"] if not username or not password: - msg = "Please define a username and password to the HSC cutout service in your fibad config " - msg += "file to use the downloader. Accounts can be created at the following url: \n" + msg = "Please define a username and password to the HSC cutout service in credentials " + msg += f"file: {credentials_file} to use the downloader. The format of the credentials file is:\n" + msg += '\nusername = ""\n' + msg += 'password = ""\n\n' + msg += "Please do not check your credentials into git or other version control systems.\n" + msg += "Accounts can be created at the following url: \n" msg += " https://hsc-release.mtk.nao.ac.jp/datasearch/new_user/new " raise RuntimeError(msg) diff --git a/src/fibad/fibad_default_config.toml b/src/fibad/fibad_default_config.toml index b8d84fe..7444f60 100644 --- a/src/fibad/fibad_default_config.toml +++ b/src/fibad/fibad_default_config.toml @@ -28,8 +28,21 @@ sh = "22asec" filter = ["HSC-G", "HSC-R", "HSC-I", "HSC-Z", "HSC-Y"] type = "coadd" rerun = "pdr3_wide" + +# Credentials for the downloader. Either provide username/password in a fibad config file +# or with a credentials.ini file. The format for credentials.ini is as follows: +# +# username = "" +# password = "" +# +# It is preferred to use a credentials.ini file to avoid sensitive user credentials being +# committed to source control systems like git. username = false password = false +# Location of the credentials.ini file. Defaults to credentials.ini in the current directory +# but can be configured to a common location for batch processing. +credentials_file = "./credentials.ini" + num_sources = -1 # Values below 1 here indicate all sources in the catalog will be downloaded offset = 0 concurrent_connections = 4