From b6e44f4f5003485cb7a8748dcc58039b50d44ce9 Mon Sep 17 00:00:00 2001 From: mcarans Date: Wed, 14 Jan 2026 17:45:58 +1300 Subject: [PATCH 1/2] Path aware --- src/hdx/location/adminlevel.py | 9 +++++---- src/hdx/location/country.py | 9 ++++++--- src/hdx/location/currency.py | 9 +++++---- src/hdx/location/wfp_api.py | 3 ++- tests/hdx/location/conftest.py | 6 +++--- tests/hdx/location/test_adminlevel.py | 10 ++++------ tests/hdx/location/test_country.py | 2 +- tests/hdx/location/test_currency.py | 6 ++---- 8 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/hdx/location/adminlevel.py b/src/hdx/location/adminlevel.py index 82ccd81..2d6ae61 100755 --- a/src/hdx/location/adminlevel.py +++ b/src/hdx/location/adminlevel.py @@ -1,6 +1,7 @@ import logging import re from collections.abc import Sequence +from pathlib import Path from typing import Any import hxl @@ -116,7 +117,7 @@ def set_default_admin_url(cls, admin_url: str | None = None) -> None: @staticmethod def get_libhxl_dataset( - url: str = admin_url, retriever: Retrieve | None = None + url: Path | str = admin_url, retriever: Retrieve | None = None ) -> hxl.Dataset: """ Get libhxl Dataset object given a URL which defaults to global p-codes @@ -139,7 +140,7 @@ def get_libhxl_dataset( url_to_use = url try: return hxl.data( - url_to_use, + str(url_to_use), InputOptions(InputOptions(allow_local=True, encoding="utf-8")), ) except (FileNotFoundError, HXLIOException): @@ -247,7 +248,7 @@ def setup_from_libhxl_dataset( def setup_from_url( self, - admin_url: str = admin_url, + admin_url: Path | str = admin_url, countryiso3s: Sequence[str] | None = None, ) -> None: """ @@ -289,7 +290,7 @@ def load_pcode_formats_from_libhxl_dataset( for x in re.finditer("0", pcode): dict_of_sets_add(self.zeroes, countryiso3, x.start()) - def load_pcode_formats(self, formats_url: str = formats_url) -> None: + def load_pcode_formats(self, formats_url: Path | str = formats_url) -> None: """ Load p-code formats from a URL. Defaults to global p-codes dataset on HDX. diff --git a/src/hdx/location/country.py b/src/hdx/location/country.py index 308d3bf..00e90d0 100755 --- a/src/hdx/location/country.py +++ b/src/hdx/location/country.py @@ -3,6 +3,7 @@ import logging import os.path import re +from pathlib import Path import hxl from hdx.utilities.path import script_dir_plus_file @@ -239,14 +240,16 @@ def countriesdata( cls.set_country_name_mappings(country_name_mappings) if cls._use_live: try: - countries = hxl.data(cls._ochaurl, InputOptions(encoding="utf-8")) + countries = hxl.data( + str(cls._ochaurl), InputOptions(encoding="utf-8") + ) except OSError: logger.exception( "Download from OCHA feed failed! Falling back to stored file." ) if countries is None: countries = hxl.data( - cls._ochapath, + str(cls._ochapath), InputOptions(allow_local=True, encoding="utf-8"), ) cls.set_countriesdata(countries) @@ -302,7 +305,7 @@ def set_ocha_url(cls, url: str = None) -> None: cls._ochaurl = url @classmethod - def set_ocha_path(cls, path: str | None = None) -> None: + def set_ocha_path(cls, path: Path | str | None = None) -> None: """ Set local path from which to retrieve OCHA countries data diff --git a/src/hdx/location/currency.py b/src/hdx/location/currency.py index dc15c8f..edd0564 100644 --- a/src/hdx/location/currency.py +++ b/src/hdx/location/currency.py @@ -3,6 +3,7 @@ import logging from copy import deepcopy from datetime import datetime, timezone +from pathlib import Path from hdx.utilities.dateparse import ( now_utc, @@ -50,9 +51,9 @@ class Currency: def setup( cls, retriever: Retrieve | None = None, - primary_rates_url: str = _primary_rates_url, - secondary_rates_url: str = _secondary_rates_url, - secondary_historic_url: str | None = _secondary_historic_url, + primary_rates_url: Path | str = _primary_rates_url, + secondary_rates_url: Path | str = _secondary_rates_url, + secondary_historic_url: Path | str | None = _secondary_historic_url, secondary_historic_rates: dict | None = None, fallback_historic_to_current: bool = False, fallback_current_to_static: bool = False, @@ -100,7 +101,7 @@ def setup( temp_dir = get_temp_dir(cls._user_agent) retriever = Retrieve( downloader, - None, + temp_dir, temp_dir, temp_dir, save=False, diff --git a/src/hdx/location/wfp_api.py b/src/hdx/location/wfp_api.py index 864b110..b40abeb 100644 --- a/src/hdx/location/wfp_api.py +++ b/src/hdx/location/wfp_api.py @@ -1,4 +1,5 @@ import logging +from pathlib import Path from typing import Any from hdx.utilities.base_downloader import DownloadError @@ -65,7 +66,7 @@ def refresh_token(self) -> None: def retrieve( self, - url: str, + url: Path | str, filename: str, log: str, parameters: dict | None = None, diff --git a/tests/hdx/location/conftest.py b/tests/hdx/location/conftest.py index 81718b9..a2d332a 100644 --- a/tests/hdx/location/conftest.py +++ b/tests/hdx/location/conftest.py @@ -1,4 +1,4 @@ -from os.path import join +from pathlib import Path import pytest @@ -7,12 +7,12 @@ @pytest.fixture(scope="session") def fixtures_dir(): - return join("tests", "fixtures") + return Path("tests") / "fixtures" @pytest.fixture(scope="session") def input_dir(fixtures_dir): - return join(fixtures_dir, "wfp") + return fixtures_dir / "wfp" @pytest.fixture(scope="function") diff --git a/tests/hdx/location/test_adminlevel.py b/tests/hdx/location/test_adminlevel.py index 5352da3..aacef2a 100755 --- a/tests/hdx/location/test_adminlevel.py +++ b/tests/hdx/location/test_adminlevel.py @@ -1,7 +1,5 @@ """location Tests""" -from os.path import join - import pytest from hdx.utilities.base_downloader import DownloadError from hdx.utilities.downloader import Download @@ -15,19 +13,19 @@ class TestAdminLevel: @pytest.fixture(scope="function") def config(self, fixtures_dir): - return load_yaml(join(fixtures_dir, "adminlevel.yaml")) + return load_yaml(fixtures_dir / "adminlevel.yaml") @pytest.fixture(scope="function") def config_parent(self, fixtures_dir): - return load_yaml(join(fixtures_dir, "adminlevelparent.yaml")) + return load_yaml(fixtures_dir / "adminlevelparent.yaml") @pytest.fixture(scope="function") def url(self, fixtures_dir): - return join(fixtures_dir, "download-global-pcodes-adm-1-2.csv") + return fixtures_dir / "download-global-pcodes-adm-1-2.csv" @pytest.fixture(scope="function") def formats_url(self, fixtures_dir): - return join(fixtures_dir, "download-global-pcode-lengths.csv") + return fixtures_dir / "download-global-pcode-lengths.csv" def test_adminlevel(self, config): adminone = AdminLevel(config) diff --git a/tests/hdx/location/test_country.py b/tests/hdx/location/test_country.py index 0cc6bf8..f18ae4f 100755 --- a/tests/hdx/location/test_country.py +++ b/tests/hdx/location/test_country.py @@ -996,7 +996,7 @@ def test_use_live_default(self): def test_ocha_feed_file_working(self): countries = hxl.data( - script_dir_plus_file("Countries_UZB_Deleted.csv", TestCountry), + str(script_dir_plus_file("Countries_UZB_Deleted.csv", TestCountry)), InputOptions(allow_local=True, encoding="utf-8"), ) Country.set_countriesdata(countries) diff --git a/tests/hdx/location/test_currency.py b/tests/hdx/location/test_currency.py index e7a7df4..e16498e 100755 --- a/tests/hdx/location/test_currency.py +++ b/tests/hdx/location/test_currency.py @@ -1,7 +1,5 @@ """Currency Tests""" -from os.path import join - import pytest from hdx.utilities.dateparse import parse_date from hdx.utilities.downloader import Download @@ -16,11 +14,11 @@ class TestCurrency: @pytest.fixture(scope="class") def secondary_rates_url(self, fixtures_dir): - return join(fixtures_dir, "secondary_rates.json") + return fixtures_dir / "secondary_rates.json" @pytest.fixture(scope="class") def secondary_historic_url(self, fixtures_dir): - return join(fixtures_dir, "secondary_historic_rates.csv") + return fixtures_dir / "secondary_historic_rates.csv" @pytest.fixture(scope="class", autouse=True) def retrievers(self, fixtures_dir): From f5672176b2775070497e6a21b4e91795182d03b9 Mon Sep 17 00:00:00 2001 From: mcarans Date: Thu, 15 Jan 2026 16:15:24 +1300 Subject: [PATCH 2/2] Path aware --- pyproject.toml | 2 +- requirements.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b913b56..a2c5c78 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -34,7 +34,7 @@ classifiers = [ requires-python = ">=3.10" dependencies = [ - "hdx-python-utilities>=4.0.0", + "hdx-python-utilities>=4.0.1", "libhxl>=5.2.2", "tenacity", ] diff --git a/requirements.txt b/requirements.txt index 6c82074..c602be4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -20,7 +20,7 @@ et-xmlfile==2.0.0 # via openpyxl frictionless==5.18.1 # via hdx-python-utilities -hdx-python-utilities==4.0.0 +hdx-python-utilities==4.0.1 # via hdx-python-country (pyproject.toml) humanize==4.15.0 # via frictionless