Skip to content

Commit

Permalink
Move over update function
Browse files Browse the repository at this point in the history
  • Loading branch information
IanHopkinson committed Mar 23, 2024
1 parent ee65ed8 commit f74a3b7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 49 deletions.
53 changes: 4 additions & 49 deletions src/hdx_cli_toolkit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions src/hdx_cli_toolkit/hdx_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit f74a3b7

Please sign in to comment.