Skip to content

Commit

Permalink
Merge pull request #24 from OCHA-DAP/gh18_testing_and_errors
Browse files Browse the repository at this point in the history
GH18 Improve testing, error handling and coincidently introduce console colour
  • Loading branch information
IanHopkinson authored Apr 12, 2024
2 parents 82a2d2e + 6b1a157 commit bcc3053
Show file tree
Hide file tree
Showing 13 changed files with 605 additions and 55 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This toolkit provides a commandline interface to the [Humanitarian Data Exchange

```
configuration Print configuration information to terminal
download Download dataset resources from HDX
get_organization_metadata Get an organization id and other metadata
get_user_metadata Get user id and other metadata
list List datasets in HDX
Expand Down Expand Up @@ -110,9 +111,4 @@ New features should be developed against a GitHub issue on a separate branch wit

## Publication

Publication to PyPI is done using `hatch` which requires the following environment variables:

```
HATCH_INDEX_USER - set to `__token__`
HATCH_INDEX_AUTH - API key provided by PyPI
```
Publication to PyPI is done automatically when a release is created.
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.4.1"
version = "2024.4.2"
description = "HDX CLI tool kit for commandline interaction with HDX"
readme = {file = "README.md", content-type = "text/markdown"}
license = {file = "LICENSE"}
Expand Down
12 changes: 7 additions & 5 deletions src/hdx_cli_toolkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,8 +422,9 @@ def showcase(
print(f"Adding showcase defined at '{attributes_file_path}'")
t0 = time.time()
statuses = add_showcase(showcase_name, hdx_site, attributes_file_path)
for status in statuses:
print(status, flush=True)
if statuses:
for status in statuses:
print(status, flush=True)

print(f"Showcase update took {time.time() - t0:.2f} seconds")

Expand Down Expand Up @@ -483,10 +484,11 @@ def update_resource(
statuses = update_resource_in_hdx(
dataset_name, resource_name, hdx_site, resource_file_path, live, description=description
)
for status in statuses:
print(status, flush=True)
if statuses:
for status in statuses:
print(status, flush=True)

print(f"Resource update took {time.time() - t0:.2f} seconds")
print(f"Resource update took {time.time() - t0:.2f} seconds")


@hdx_toolkit.command(name="download")
Expand Down
39 changes: 31 additions & 8 deletions src/hdx_cli_toolkit/hdx_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@


import fnmatch
import functools
import json
import os
import time
import traceback

from pathlib import Path

import click
import yaml

from hdx.api.configuration import Configuration, ConfigurationError
Expand All @@ -24,6 +26,25 @@
from hdx_cli_toolkit.utilities import read_attributes


def hdx_error_handler(f):
@functools.wraps(f)
def inner(*args, **kwargs):
try:
return f(*args, **kwargs)
except HDXError:
if "Authorization Error" in traceback.format_exc():
click.secho(
"Could not perform operation on HDX because of an authorization error",
fg="red",
color=True,
)
else:
print(traceback.format_exc)

return inner


@hdx_error_handler
def get_filtered_datasets(
organization: str = "",
dataset_filter: str = "*",
Expand Down Expand Up @@ -82,6 +103,7 @@ def get_filtered_datasets(
return filtered_datasets


@hdx_error_handler
def update_values_in_hdx(
filtered_datasets: list[Dataset], key, value, conversion_func, hdx_site: str = "stage"
):
Expand Down Expand Up @@ -115,14 +137,7 @@ def update_values_in_hdx(
flush=True,
)
except (HDXError, KeyError):
if "Authorization Error" in traceback.format_exc():
print(
f"Could not update {dataset['name']} on '{hdx_site}' "
"because of an Authorization Error",
flush=True,
)
else:
print(f"Could not update {dataset['name']} on '{hdx_site}'", flush=True)
print(f"Could not update {dataset['name']} on '{hdx_site}'", flush=True)
n_failures += 1

print(
Expand All @@ -134,6 +149,7 @@ def update_values_in_hdx(
return n_changed, n_failures


@hdx_error_handler
def get_organizations_from_hdx(organization: str, hdx_site: str = "stage"):
configure_hdx_connection(hdx_site)
filtered_organizations = []
Expand All @@ -146,6 +162,7 @@ def get_organizations_from_hdx(organization: str, hdx_site: str = "stage"):
return filtered_organizations


@hdx_error_handler
def get_users_from_hdx(user: str, hdx_site: str = "stage"):
configure_hdx_connection(hdx_site=hdx_site)

Expand All @@ -154,6 +171,7 @@ def get_users_from_hdx(user: str, hdx_site: str = "stage"):
return user_list


@hdx_error_handler
def decorate_dataset_with_extras(
dataset: Dataset, hdx_site: str = "stage", verbose: bool = False
) -> dict:
Expand Down Expand Up @@ -194,6 +212,7 @@ def decorate_dataset_with_extras(
return output_dict


@hdx_error_handler
def add_showcase(showcase_name: str, hdx_site: str, attributes_file_path: str) -> list[str]:
configure_hdx_connection(hdx_site)
statuses = []
Expand All @@ -220,6 +239,7 @@ def add_showcase(showcase_name: str, hdx_site: str, attributes_file_path: str) -
return statuses


@hdx_error_handler
def update_resource_in_hdx(
dataset_name: str,
resource_name: str,
Expand Down Expand Up @@ -312,6 +332,7 @@ def update_resource_in_hdx(
return statuses


@hdx_error_handler
def add_quickcharts(dataset_name, hdx_site, resource_name, hdx_hxl_preview_file_path):
configure_hdx_connection(hdx_site=hdx_site)
status = "Successful"
Expand Down Expand Up @@ -348,6 +369,7 @@ def add_quickcharts(dataset_name, hdx_site, resource_name, hdx_hxl_preview_file_
return status


@hdx_error_handler
def download_hdx_datasets(
dataset_filter: str,
resource_filter: str = "*",
Expand Down Expand Up @@ -381,6 +403,7 @@ def download_hdx_datasets(
return download_paths


@hdx_error_handler
def configure_hdx_connection(hdx_site: str, verbose: bool = True):
try:
Configuration.create(
Expand Down
Loading

0 comments on commit bcc3053

Please sign in to comment.