diff --git a/src/hdx_cli_toolkit/cli.py b/src/hdx_cli_toolkit/cli.py index f341dc9..bd6c53f 100644 --- a/src/hdx_cli_toolkit/cli.py +++ b/src/hdx_cli_toolkit/cli.py @@ -399,6 +399,19 @@ def show_configuration(approved_tag_list: bool = False, organization: str = "hdx secret_part = censor_secret(secret_part) row = key_part + ': "' + secret_part print(row, flush=True) + else: + click.secho( + f"No user configuration file at {user_hdx_config_yaml}. ", + bold=True, + color=True, + fg="red", + ) + print( + "Unless API keys are supplied by environment variables a configuration file " + f"should be saved to {user_hdx_config_yaml} containing at least: \n\n" + 'hdx_key: "[API Key obtained from ' + 'https://data.humdata.org/user/[your username]]/api-tokens]"\n' + ) user_agent_config_yaml = os.path.join(os.path.expanduser("~"), ".useragents.yaml") if os.path.exists(user_agent_config_yaml): @@ -406,7 +419,19 @@ def show_configuration(approved_tag_list: bool = False, organization: str = "hdx with open(user_agent_config_yaml, encoding="utf-8") as config_file: user_agents_file_contents = config_file.read() print(user_agents_file_contents, flush=True) - + else: + click.secho( + f"No user agents file found at {user_agent_config_yaml}", + color=True, + fg="red", + ) + print( + f"The user agents file should be saved to {user_agent_config_yaml} " + "and contain at least the following:\n\n" + "hdx-cli-toolkit:\n" + " preprefix: [your_organization]\n" + " user_agent: hdx_cli_toolkit_[your_intitials]\n" + ) # Check Environment variables environment_variables = ["HDX_KEY", "HDX_KEY_STAGE", "HDX_SITE", "HDX_URL"] click.secho( @@ -430,11 +455,12 @@ def show_configuration(approved_tag_list: bool = False, organization: str = "hdx # Check API keys statuses = check_api_key(organization=organization, hdx_sites=None) - for status in statuses: - color = "green" - if "API key not valid" in status: - color = "red" - click.secho(f"{status}", fg=color, color=True) + if statuses is not None: + for status in statuses: + color = "green" + if "API key not valid" in status: + color = "red" + click.secho(f"{status}", fg=color, color=True) @hdx_toolkit.command(name="quickcharts") diff --git a/src/hdx_cli_toolkit/hdx_utilities.py b/src/hdx_cli_toolkit/hdx_utilities.py index 3dd8ff1..a37e4c8 100644 --- a/src/hdx_cli_toolkit/hdx_utilities.py +++ b/src/hdx_cli_toolkit/hdx_utilities.py @@ -42,7 +42,12 @@ def hdx_error_handler(f): def inner(*args, **kwargs): try: return f(*args, **kwargs) - except (HDXError, ckanapi.errors.ValidationError): + except ( + HDXError, + ckanapi.errors.ValidationError, + FileNotFoundError, + ConfigurationError, + ): traceback_message = traceback.format_exc() message = parse_hdxerror_traceback(traceback_message) if message != "unknown": @@ -67,6 +72,12 @@ def parse_hdxerror_traceback(traceback_message: str): message = "No Resources Error" elif "{'dataset_date': ['Invalid old HDX date" in traceback_message: message = "Invalid Dataset Date Error" + elif ".useragents.yaml" in traceback_message and "FileNotFoundError" in traceback_message: + message = "No .useragents.yaml file provided Error" + elif "There is no HDX configuration!" in traceback_message: + message = "There is no HDX configuration Error" + elif "No HDX API key supplied as a parameter or in configuration" in traceback_message: + message = "No HDX API key supplied Error" return message @@ -524,17 +535,14 @@ def download_hdx_datasets( @hdx_error_handler def configure_hdx_connection(hdx_site: str, verbose: bool = True): Configuration._configuration = None - try: - Configuration.create( - user_agent_config_yaml=os.path.join(os.path.expanduser("~"), ".useragents.yaml"), - user_agent_lookup="hdx-cli-toolkit", - hdx_site=hdx_site, - hdx_read_only=False, - ) - if verbose: - print(f"Connected to HDX site {Configuration.read().get_hdx_site_url()}", flush=True) - except ConfigurationError: - print(traceback.format_exc(), flush=True) + Configuration.create( + user_agent_config_yaml=os.path.join(os.path.expanduser("~"), ".useragents.yaml"), + user_agent_lookup="hdx-cli-toolkit", + hdx_site=hdx_site, + hdx_read_only=False, + ) + if verbose: + print(f"Connected to HDX site {Configuration.read().get_hdx_site_url()}", flush=True) def get_approved_tag_list() -> list[str]: @@ -595,6 +603,7 @@ def remove_extras_key_from_dataset( return output_row +@hdx_error_handler def check_api_key(organization: str = "hdx", hdx_sites: Optional[str] = None) -> list[str]: if hdx_sites is None: hdx_sites = ["stage", "prod"]