Skip to content

Commit

Permalink
Merge pull request #38 from OCHA-DAP/gh37_warn_for_config
Browse files Browse the repository at this point in the history
GH37 Provide better error handling and information when configuration files are missing
  • Loading branch information
IanHopkinson authored Aug 6, 2024
2 parents f80c4ed + 51470b9 commit 22b7d2f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "hdx_cli_toolkit"
version = "2024.7.2"
version = "2024.8.1"
description = "HDX CLI tool kit for commandline interaction with HDX"
readme = {file = "README.md", content-type = "text/markdown"}
license = {file = "LICENSE"}
Expand Down
38 changes: 32 additions & 6 deletions src/hdx_cli_toolkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,39 @@ 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):
click.secho(f"User agents file found at {user_agent_config_yaml}", bold=True)
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(
Expand All @@ -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")
Expand Down
33 changes: 21 additions & 12 deletions src/hdx_cli_toolkit/hdx_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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

Expand Down Expand Up @@ -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]:
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit 22b7d2f

Please sign in to comment.