Skip to content

Commit

Permalink
Merge pull request #34 from OCHA-DAP/wfp_fx
Browse files Browse the repository at this point in the history
Add WFP API
  • Loading branch information
mcarans authored Mar 1, 2024
2 parents b93ca54 + 4236d7e commit dd16290
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 80 deletions.
4 changes: 2 additions & 2 deletions .config/pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repos:
- id: end-of-file-fixer
- id: check-ast
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.14
rev: v0.3.0
hooks:
# Run the linter.
- id: ruff
Expand All @@ -17,7 +17,7 @@ repos:
- id: ruff-format
args: [--config, .config/ruff.toml]
- repo: https://github.com/jazzband/pip-tools
rev: 7.3.0
rev: 7.4.0
hooks:
- id: pip-compile
name: pip-compile requirements.txt
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/run-python-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ jobs:
python -m pip install --upgrade pip
pip install --upgrade hatch
- name: Test with hatch/pytest
env:
WFP_KEY: ${{ secrets.WFP_KEY }}
WFP_SECRET: ${{ secrets.WFP_SECRET }}
run: |
hatch run test:test
- name: Check styling
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ Homepage = "https://github.com/OCHA-DAP/hdx-python-country"
[project.optional-dependencies]
test = ["pytest", "pytest-cov"]
dev = ["pre-commit"]
wfp = ["data-bridges-client@git+https://github.com/WFP-VAM/DataBridgesAPI@dev#egg=data-bridges-client"]


#########
# Hatch #
#########

[tool.hatch.metadata]
allow-direct-references = true

# Build

[tool.hatch.build.targets.wheel]
Expand All @@ -76,7 +80,7 @@ version_scheme = "python-simplified-semver"
# Tests

[tool.hatch.envs.test]
features = ["test"]
features = ["test", "wfp"]

[tool.hatch.envs.test.scripts]
test = """
Expand Down
47 changes: 38 additions & 9 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
# This file is autogenerated by pip-compile with Python 3.11
# by the following command:
#
# pip-compile --all-extras --output-file=requirements.txt --resolver=backtracking pyproject.toml
# pip-compile --all-extras --output-file=requirements.txt pyproject.toml
#
annotated-types==0.6.0
# via pydantic
anyio==4.3.0
# via httpx
attrs==23.2.0
# via
# frictionless
# jsonlines
# jsonschema
certifi==2024.2.2
# via requests
# via
# httpcore
# httpx
# requests
cfgv==3.4.0
# via pre-commit
chardet==5.2.0
Expand All @@ -24,7 +29,11 @@ click==8.1.7
colorama==0.4.6
# via typer
coverage[toml]==7.4.3
# via pytest-cov
# via
# coverage
# pytest-cov
data-bridges-client @ git+https://github.com/WFP-VAM/DataBridgesAPI@dev
# via hdx-python-country (pyproject.toml)
distlib==0.3.8
# via virtualenv
et-xmlfile==1.1.0
Expand All @@ -33,14 +42,23 @@ filelock==3.13.1
# via virtualenv
frictionless==5.16.1
# via hdx-python-utilities
h11==0.14.0
# via httpcore
hdx-python-utilities==3.6.5
# via hdx-python-country (pyproject.toml)
httpcore==1.0.4
# via httpx
httpx==0.27.0
# via data-bridges-client
humanize==4.9.0
# via frictionless
identify==2.5.35
# via pre-commit
idna==3.6
# via requests
# via
# anyio
# httpx
# requests
ijson==3.2.3
# via hdx-python-utilities
iniconfig==2.0.0
Expand Down Expand Up @@ -87,8 +105,10 @@ ply==3.11
# libhxl
pre-commit==3.6.2
# via hdx-python-country (pyproject.toml)
pydantic==2.6.2
# via frictionless
pydantic==2.6.3
# via
# data-bridges-client
# frictionless
pydantic-core==2.16.3
# via pydantic
pygments==2.17.2
Expand All @@ -105,6 +125,7 @@ pytest-cov==4.1.0
# via hdx-python-country (pyproject.toml)
python-dateutil==2.8.2
# via
# data-bridges-client
# frictionless
# hdx-python-utilities
# libhxl
Expand All @@ -128,7 +149,7 @@ requests-file==2.0.0
# via hdx-python-utilities
rfc3986==2.0.0
# via frictionless
rich==13.7.0
rich==13.7.1
# via typer
ruamel-yaml==0.18.6
# via hdx-python-utilities
Expand All @@ -142,6 +163,10 @@ six==1.16.0
# via
# isodate
# python-dateutil
sniffio==1.3.1
# via
# anyio
# httpx
stringcase==1.2.0
# via frictionless
structlog==24.1.0
Expand All @@ -153,9 +178,12 @@ tabulate==0.9.0
text-unidecode==1.3
# via python-slugify
typer[all]==0.9.0
# via frictionless
# via
# frictionless
# typer
typing-extensions==4.10.0
# via
# data-bridges-client
# frictionless
# pydantic
# pydantic-core
Expand All @@ -164,8 +192,9 @@ unidecode==1.3.8
# via
# libhxl
# pyphonetics
urllib3==2.2.1
urllib3==2.0.7
# via
# data-bridges-client
# libhxl
# requests
validators==0.22.0
Expand Down
16 changes: 16 additions & 0 deletions src/hdx/location/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
from datetime import datetime

from ._version import version as __version__ # noqa: F401
from hdx.utilities.dateparse import get_timestamp_from_datetime


def get_int_timestamp(date: datetime) -> int:
"""
Get integer timestamp from datetime object
Args:
date (datetime): datetime object
Returns:
int: Integer timestamp
"""
return int(round(get_timestamp_from_datetime(date)))
8 changes: 4 additions & 4 deletions src/hdx/location/adminlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(
"admin_name_replacements", {}
)
self.admin_fuzzy_dont = admin_config.get("admin_fuzzy_dont", list())
self.pcodes = list()
self.pcodes = []
self.pcode_lengths = {}
self.name_to_pcode = {}
self.pcode_to_name = {}
Expand Down Expand Up @@ -634,7 +634,7 @@ def output_matches(self) -> List[str]:
Returns:
List[str]: List of matches
"""
output = list()
output = []
for match in sorted(self.matches):
line = f"{match[0]} - {match[1]}: Matching ({match[4]}) {match[2]} to {match[3]} on map"
logger.info(line)
Expand All @@ -647,7 +647,7 @@ def output_ignored(self) -> List[str]:
Returns:
List[str]: List of ignored
"""
output = list()
output = []
for ignored in sorted(self.ignored):
if len(ignored) == 2:
line = f"{ignored[0]} - Ignored {ignored[1]}!"
Expand All @@ -663,7 +663,7 @@ def output_errors(self) -> List[str]:
Returns:
List[str]: List of errors
"""
output = list()
output = []
for error in sorted(self.errors):
if len(error) == 2:
line = f"{error[0]} - Could not find {error[1]} in map names!"
Expand Down
55 changes: 28 additions & 27 deletions src/hdx/location/country.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Country location"""

import copy
import logging
import re
Expand Down Expand Up @@ -69,8 +70,8 @@ class Country:
_countriesdata = None
_ochaurl_default = "https://docs.google.com/spreadsheets/d/1NjSI2LaS3SqbgYc0HdD8oIb7lofGtiHgoKKATCpwVdY/export?format=csv&gid=1088874596"
_ochaurl = _ochaurl_default
_country_name_overrides = dict()
_country_name_mappings = dict()
_country_name_overrides = {}
_country_name_mappings = {}

@classmethod
def _add_countriesdata(cls, iso3: str, hxlcountry: hxl.Row) -> Dict:
Expand Down Expand Up @@ -131,24 +132,24 @@ def add_country_to_set(colname, idval, iso3):
if regionname:
add_country_to_set("regioncodes2countries", regionid, iso3)
cls._countriesdata["regioncodes2names"][regionid] = regionname
cls._countriesdata["regionnames2codes"][
regionname.upper()
] = regionid
cls._countriesdata["regionnames2codes"][regionname.upper()] = (
regionid
)
if sub_regionname:
add_country_to_set("regioncodes2countries", sub_regionid, iso3)
cls._countriesdata["regioncodes2names"][
cls._countriesdata["regioncodes2names"][sub_regionid] = (
sub_regionname
)
cls._countriesdata["regionnames2codes"][sub_regionname.upper()] = (
sub_regionid
] = sub_regionname
cls._countriesdata["regionnames2codes"][
sub_regionname.upper()
] = sub_regionid
)
if intermediate_regionname:
add_country_to_set(
"regioncodes2countries", intermediate_regionid, iso3
)
cls._countriesdata["regioncodes2names"][
intermediate_regionid
] = intermediate_regionname
cls._countriesdata["regioncodes2names"][intermediate_regionid] = (
intermediate_regionname
)
cls._countriesdata["regionnames2codes"][
intermediate_regionname.upper()
] = intermediate_regionid
Expand All @@ -167,21 +168,21 @@ def set_countriesdata(cls, countries: str) -> None:
Returns:
None
"""
cls._countriesdata = dict()
cls._countriesdata["countries"] = dict()
cls._countriesdata["iso2iso3"] = dict()
cls._countriesdata["m49iso3"] = dict()
cls._countriesdata["countrynames2iso3"] = dict()
cls._countriesdata["regioncodes2countries"] = dict()
cls._countriesdata["regioncodes2names"] = dict()
cls._countriesdata["regionnames2codes"] = dict()
cls._countriesdata["aliases"] = dict()
cls._countriesdata["currencies"] = dict()
cls._countriesdata = {}
cls._countriesdata["countries"] = {}
cls._countriesdata["iso2iso3"] = {}
cls._countriesdata["m49iso3"] = {}
cls._countriesdata["countrynames2iso3"] = {}
cls._countriesdata["regioncodes2countries"] = {}
cls._countriesdata["regioncodes2names"] = {}
cls._countriesdata["regionnames2codes"] = {}
cls._countriesdata["aliases"] = {}
cls._countriesdata["currencies"] = {}

for key, value in cls._country_name_mappings.items():
cls._countriesdata["countrynames2iso3"][
key.upper()
] = value.upper()
cls._countriesdata["countrynames2iso3"][key.upper()] = (
value.upper()
)

for country in countries:
iso3 = country.get("#country+code+v_iso3")
Expand Down Expand Up @@ -891,4 +892,4 @@ def get_countries_in_region(

if exception is not None:
raise exception
return list()
return []
Loading

0 comments on commit dd16290

Please sign in to comment.