From 3d04f77d73cf8d77a6591ec0a74827f24ab40434 Mon Sep 17 00:00:00 2001 From: Shettland Date: Wed, 18 Oct 2023 16:46:46 +0200 Subject: [PATCH] Introduced new auxiliary functions to help credential extraction and validation --- bu_isciii/utils.py | 55 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) mode change 100644 => 100755 bu_isciii/utils.py diff --git a/bu_isciii/utils.py b/bu_isciii/utils.py old mode 100644 new mode 100755 index 6e3fa6bc..a7608160 --- a/bu_isciii/utils.py +++ b/bu_isciii/utils.py @@ -8,6 +8,7 @@ import questionary import rich +import yaml import bu_isciii import bu_isciii.config_json @@ -217,13 +218,16 @@ def get_sftp_folder(resolution_info): user_sftp_file = open(json_file) json_data = json.load(user_sftp_file) user_sftp_file.close() + sftp_folders_list = [] for user_sftp in json_data: if user_sftp == service_user: sftp_folders_list = json_data[user_sftp] data_path = bu_isciii.config_json.ConfigJson().get_configuration("global")[ "data_path" ] - + if not sftp_folders_list: + print(f"User {service_user} does not have an assigned sftp folder. Aborting...") + exit() if len(sftp_folders_list) == 1: sftp_folder = os.path.join(data_path, "sftp", sftp_folders_list[0]) sftp_final_folder = sftp_folders_list[0] @@ -387,5 +391,54 @@ def ask_date(previous_date=None, posterior_date=None, initial_year=2010): return datetime.date(int(year), int(chosen_month_number), int(day)) +def get_yaml_config(): + """Extract the fields from the config yaml file described in configuration.json""" + yaml_path = bu_isciii.config_json.ConfigJson().get_find("global", "yaml_conf_path") + try: + yaml_file = os.path.expanduser(yaml_path) + except Exception: + print("Yaml file path could not be resolved") + return {} + yaml_fields = process_yaml_file(yaml_file) + if yaml_fields: + return yaml_fields + else: + print("Could not extract fields from config file", yaml_file) + return {} + + +def validate_api_credentials(cred_fields): + """Validate that API credentials are present in dictionary""" + user, password = cred_fields.get("api_user"), cred_fields.get("api_password") + if user and password: + return True + # This could include a test to the API to check that the credentials are correct + """ + conf_api = bu_isciii.config_json.ConfigJson().get_configuration( + "xtutatis_api_settings" + ) + rest_api = bu_isciii.drylab_api.RestServiceApi( + conf_api["server"], conf_api["api_url"], user, password, + ) + # This doesn't work, returns error 404 + status = rest_api.basic_authentication() + return status + """ + else: + print("Missing credentials in yaml file given") + return + + +def process_yaml_file(yaml_file): + """Read the yaml file and return a dictionary with key/value pairs""" + if os.path.exists(yaml_file): + with open(yaml_file, "r") as cred_file: + yaml_fields = yaml.safe_load(cred_file) + return yaml_fields + else: + print(f"Config file {yaml_file} does not exist") + return + + def validate_date(date_previous, date_posterior): return