From f74a3b756bc7caf7fd68b07cd167661ff44fb998 Mon Sep 17 00:00:00 2001 From: Ian Hopkinson Date: Sat, 23 Mar 2024 15:42:20 +0000 Subject: [PATCH] Move over update function --- src/hdx_cli_toolkit/cli.py | 53 ++------------------------- src/hdx_cli_toolkit/hdx_utilities.py | 55 ++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 49 deletions(-) diff --git a/src/hdx_cli_toolkit/cli.py b/src/hdx_cli_toolkit/cli.py index 8e95cb6..ccdac87 100644 --- a/src/hdx_cli_toolkit/cli.py +++ b/src/hdx_cli_toolkit/cli.py @@ -4,17 +4,13 @@ import json import os import time -import traceback from collections.abc import Callable -import yaml import click from click.decorators import FC from hdx.api.configuration import ConfigurationError -from hdx.data.hdxobject import HDXError -from hdx.data.dataset import Dataset from hdx.utilities.path import script_dir_plus_file from hdx_cli_toolkit.utilities import ( @@ -30,6 +26,7 @@ add_quickcharts, get_organizations_from_hdx, get_users_from_hdx, + update_values_in_hdx, configure_hdx_connection, update_resource_in_hdx, get_filtered_datasets, @@ -176,51 +173,9 @@ def update( f"{'Time to update/seconds':<25.25}", flush=True, ) - n_changed = 0 - n_failures = 0 - for dataset in filtered_datasets: - t0 = time.time() - old_value = str(dataset[key]) - dataset[key] = conversion_func(value) - if old_value != str(dataset[key]): - n_changed += 1 - else: - print( - f"{dataset['name']:<70.70}{old_value:<20.20}{str(dataset[key]):<20.20}" - f"{'No update required':<25.25}", - flush=True, - ) - continue - try: - dataset.update_in_hdx( - update_resources=False, - hxl_update=False, - operation="patch", - batch_mode="KEEP_OLD", - skip_validation=True, - ignore_check=True, - ) - print( - f"{dataset['name']:<70.70}{old_value:<20.20}{str(dataset[key]):<20.20}" - f"{time.time()-t0:0.2f}", - 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) - n_failures += 1 - - print( - f"{dataset['name']:<70.70}{old_value:<20.20}{old_value:<20.20}" - f"{time.time()-t0:0.2f}", - flush=True, - ) + n_changed, n_failures = update_values_in_hdx( + filtered_datasets, key, value, conversion_func, hdx_site=hdx_site + ) print(f"Changed {n_changed} values", flush=True) print(f"{n_failures} failures as evidenced by HDXError", flush=True) diff --git a/src/hdx_cli_toolkit/hdx_utilities.py b/src/hdx_cli_toolkit/hdx_utilities.py index 687a88c..b454d1e 100644 --- a/src/hdx_cli_toolkit/hdx_utilities.py +++ b/src/hdx_cli_toolkit/hdx_utilities.py @@ -5,10 +5,13 @@ import fnmatch import json import os +import time +import traceback import yaml from hdx.api.configuration import Configuration, ConfigurationError from hdx.data.organization import Organization from hdx.data.resource_view import ResourceView +from hdx.data.hdxobject import HDXError from hdx.data.dataset import Dataset from hdx.data.showcase import Showcase from hdx.data.resource import Resource @@ -75,6 +78,58 @@ def get_filtered_datasets( return filtered_datasets +def update_values_in_hdx( + filtered_datasets: list[Dataset], key, value, conversion_func, hdx_site: str = "stage" +): + n_changed = 0 + n_failures = 0 + for dataset in filtered_datasets: + t0 = time.time() + old_value = str(dataset[key]) + dataset[key] = conversion_func(value) + if old_value != str(dataset[key]): + n_changed += 1 + else: + print( + f"{dataset['name']:<70.70}{old_value:<20.20}{str(dataset[key]):<20.20}" + f"{'No update required':<25.25}", + flush=True, + ) + continue + try: + dataset.update_in_hdx( + update_resources=False, + hxl_update=False, + operation="patch", + batch_mode="KEEP_OLD", + skip_validation=True, + ignore_check=True, + ) + print( + f"{dataset['name']:<70.70}{old_value:<20.20}{str(dataset[key]):<20.20}" + f"{time.time()-t0:0.2f}", + 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) + n_failures += 1 + + print( + f"{dataset['name']:<70.70}{old_value:<20.20}{old_value:<20.20}" + f"{time.time()-t0:0.2f}", + flush=True, + ) + + return n_changed, n_failures + + def get_organizations_from_hdx(organization: str, hdx_site: str = "stage"): configure_hdx_connection(hdx_site) filtered_organizations = []