diff --git a/src/hdx/location/country.py b/src/hdx/location/country.py index 74e1eaf..6a06e2f 100755 --- a/src/hdx/location/country.py +++ b/src/hdx/location/country.py @@ -2,6 +2,7 @@ import copy import logging +import os.path import re from string import punctuation from typing import Dict, List, Optional, Tuple, Union @@ -73,6 +74,7 @@ class Country: _countriesdata = None _ochaurl_default = "https://docs.google.com/spreadsheets/d/1NjSI2LaS3SqbgYc0HdD8oIb7lofGtiHgoKKATCpwVdY/export?format=csv&gid=1088874596" _ochaurl = _ochaurl_default + _ochapath = None _country_name_overrides = {} _country_name_mappings = {} @@ -237,11 +239,14 @@ def countriesdata( "Download from OCHA feed failed! Falling back to stored file." ) if countries is None: + file_path = script_dir_plus_file( + "Countries & Territories Taxonomy MVP - C&T Taxonomy with HXL Tags.csv", + Country, + ) + if cls._ochapath and os.path.exists(cls._ochapath): + file_path = cls._ochapath countries = hxl.data( - script_dir_plus_file( - "Countries & Territories Taxonomy MVP - C&T Taxonomy with HXL Tags.csv", - Country, - ), + file_path, InputOptions(allow_local=True, encoding="utf-8"), ) cls.set_countriesdata(countries) @@ -278,6 +283,19 @@ def set_ocha_url(cls, url: Optional[str] = None) -> None: url = cls._ochaurl_default cls._ochaurl = url + @classmethod + def set_ocha_path(cls, path: Optional[str] = None) -> None: + """ + Set local path from which to retrieve OCHA countries data + + Args: + path (Optional[str]): Local path from which to retrieve countries data. Defaults to None. + + Returns: + None + """ + cls._ochapath = path + @classmethod def set_country_name_overrides(cls, country_name_overrides: Dict) -> None: """ diff --git a/tests/hdx/location/test_country.py b/tests/hdx/location/test_country.py index df253f3..7929625 100755 --- a/tests/hdx/location/test_country.py +++ b/tests/hdx/location/test_country.py @@ -716,3 +716,17 @@ def test_ocha_feed_file_working(self): Country.set_ocha_url("NOTEXIST") Country._countriesdata = None assert Country.get_iso3_from_iso2("AF") == "AFG" + + def test_ocha_feed_local_file_working(self): + assert ( + Country.get_iso3_country_code("UZBEKISTAN", use_live=False) + == "UZB" + ) + + Country._countriesdata = None + Country.set_ocha_path( + script_dir_plus_file("Countries_UZB_Deleted.csv", TestCountry) + ) + assert ( + Country.get_iso3_country_code("UZBEKISTAN", use_live=False) is None + )