Skip to content

Commit

Permalink
Add option to set path for local OCHA data file
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-isoc committed Nov 15, 2024
1 parent 3d3f005 commit 7160f0c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/hdx/location/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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:
"""
Expand Down
14 changes: 14 additions & 0 deletions tests/hdx/location/test_country.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 7160f0c

Please sign in to comment.