Skip to content

Commit

Permalink
Fix for HAPI so that Retrieve object is passed and hence get_libhxl_d…
Browse files Browse the repository at this point in the history
…ataset method can be static (#54)
  • Loading branch information
mcarans authored Oct 6, 2024
1 parent 30b440d commit c41f067
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 4 additions & 0 deletions documentation/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ object has been set up with parents). Keys take the form "STRING_TO_REPLACE",
*admin_fuzzy_dont* is a list of names for which fuzzy matching should not be
tried

A Retrieve object can be passed in the *retriever* parameter that enables
saving data downloaded to a file or loading previously saved data depending
on how the Retrieve object is configured.

Once an AdminLevel object is constructed, one of three setup methods must be
called: *setup_from_admin_info*, *setup_from_libhxl_dataset* or
*setup_from_url*.
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ frictionless==5.18.0
# via hdx-python-utilities
hdx-python-utilities==3.7.4
# via hdx-python-country (pyproject.toml)
humanize==4.10.0
humanize==4.11.0
# via frictionless
identify==2.6.1
# via pre-commit
Expand Down Expand Up @@ -82,7 +82,7 @@ ply==3.11
# via
# jsonpath-ng
# libhxl
pre-commit==3.8.0
pre-commit==4.0.0
# via hdx-python-country (pyproject.toml)
pydantic==2.9.2
# via frictionless
Expand Down Expand Up @@ -127,7 +127,7 @@ requests-file==2.1.0
# via hdx-python-utilities
rfc3986==2.0.0
# via frictionless
rich==13.9.1
rich==13.9.2
# via typer
rpds-py==0.20.0
# via
Expand All @@ -139,7 +139,7 @@ ruamel-yaml-clib==0.2.8
# via ruamel-yaml
shellingham==1.5.4
# via typer
simpleeval==0.9.13
simpleeval==1.0.0
# via frictionless
six==1.16.0
# via
Expand Down
18 changes: 11 additions & 7 deletions src/hdx/location/adminlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,24 @@ def set_default_admin_url(cls, admin_url: Optional[str] = None) -> None:
admin_url = cls._admin_url_default
cls._admin_url = admin_url

def get_libhxl_dataset(self, url: str = _admin_url) -> hxl.Dataset:
@staticmethod
def get_libhxl_dataset(
url: str = _admin_url, retriever: Optional[Retrieve] = None
) -> hxl.Dataset:
"""
Get libhxl Dataset object given a URL which defaults to global p-codes
dataset on HDX.
Args:
admin_url (str): URL from which to load data.
url (str): URL from which to load data. Defaults to internal admin url.
retriever (Optional[Retrieve]): Retriever object to use for loading file. Defaults to None.
Returns:
None
hxl.Dataset: HXL Dataset object
"""
if self.retriever:
if retriever:
try:
url_to_use = self.retriever.download_file(url)
url_to_use = retriever.download_file(url)
except DownloadError:
logger.exception(
f"Setup of libhxl Dataset object with {url} failed!"
Expand Down Expand Up @@ -271,7 +275,7 @@ def setup_from_url(
Returns:
None
"""
admin_info = self.get_libhxl_dataset(admin_url)
admin_info = self.get_libhxl_dataset(admin_url, self.retriever)
self.setup_from_libhxl_dataset(admin_info, countryiso3s)

def load_pcode_formats(self, formats_url: str = _formats_url) -> None:
Expand All @@ -284,7 +288,7 @@ def load_pcode_formats(self, formats_url: str = _formats_url) -> None:
Returns:
None
"""
formats_info = self.get_libhxl_dataset(formats_url)
formats_info = self.get_libhxl_dataset(formats_url, self.retriever)
for row in formats_info:
pcode_format = [int(row.get("#country+len"))]
for admin_no in range(1, 4):
Expand Down

0 comments on commit c41f067

Please sign in to comment.