Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/location/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from ..coordinates import Coordinates
from ..utils import countrycodes
from ..utils import countries
from ..utils.populations import country_population


Expand Down Expand Up @@ -31,7 +31,7 @@ def country_code(self):
:returns: The country code.
:rtype: str
"""
return (countrycodes.country_code(self.country) or countrycodes.default_code).upper()
return (countries.country_code(self.country) or countries.default_country_code).upper()

@property
def country_population(self):
Expand Down
4 changes: 2 additions & 2 deletions app/services/location/jhu.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ...coordinates import Coordinates
from ...location import TimelinedLocation
from ...timeline import Timeline
from ...utils import countrycodes
from ...utils import countries
from ...utils import date as date_util
from . import LocationService

Expand Down Expand Up @@ -80,7 +80,7 @@ def get_category(category):
{
# General info.
"country": country,
"country_code": countrycodes.country_code(country),
"country_code": countries.country_code(country),
"province": item["Province/State"],
# Coordinates.
"coordinates": {"lat": item["Lat"], "long": item["Long"],},
Expand Down
19 changes: 9 additions & 10 deletions app/utils/countrycodes.py → app/utils/countries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
LOGGER = logging.getLogger(__name__)

# Default country code.
default_code = "XX"
default_country_code = "XX"

# Mapping of country names to alpha-2 codes according to
# https://en.wikipedia.org/wiki/ISO_3166-1.
# As a reference see also https://github.com/TakahikoKawasaki/nv-i18n (in Java)
# fmt: off
is_3166_1 = {
country_name__country_code = {
"Afghanistan" : "AF",
"Åland Islands" : "AX",
"Albania" : "AL",
Expand Down Expand Up @@ -269,13 +269,13 @@
"Iraq-Saudi Arabia Neutral Zone" : "XE",
"Spratly Islands" : "XS",

# TODO "Disputed Territory" conflicts with `default_code`
# TODO "Disputed Territory" conflicts with `default_country_code`
# "Disputed Territory" : "XX",
}

# Mapping of alternative names, spelling, typos to the names of countries used
# by the ISO 3166-1 norm
synonyms = {
country_alias__country_name = {
"Mainland China" : "China",
"Czechia" : "Czech Republic",
"Channel Islands" : "United Kingdom",
Expand Down Expand Up @@ -372,16 +372,15 @@ def country_code(country):
Return two letter country code (Alpha-2) according to https://en.wikipedia.org/wiki/ISO_3166-1
Defaults to "XX".
"""
# Look in synonyms if not found.
if not country in is_3166_1 and country in synonyms:
country = synonyms[country]
if not country in country_name__country_code and country in country_alias__country_name:
country = country_alias__country_name[country]

# Get country or fallback to default_code.
country_code = is_3166_1.get(country, default_code)
country_code = country_name__country_code.get(country, default_country_code)

# Default picked?
if country_code == default_code:
LOGGER.warning(f"No country_code found for '{country}'. Using '{country_code}'!")
if country_code == default_country_code:
LOGGER.warning(f"No country code found for '{country}'. Using '{country_code}'!")

# Return.
return country_code
2 changes: 1 addition & 1 deletion app/utils/populations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import requests
from cachetools import TTLCache, cached

from .countrycodes import country_code
from .countries import country_code

LOGGER = logging.getLogger(__name__)

Expand Down
12 changes: 6 additions & 6 deletions tests/test_countrycodes.py → tests/test_countries.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from app.utils import countrycodes
from app.utils import countries


"""
Expand All @@ -18,13 +18,13 @@
("BlaBla", "XX"),
],
)
def test_countrycodes_is_3166_1(country_name, expected_country_code):
assert countrycodes.country_code(country_name) == expected_country_code
def test_countries_country_name__country_code(country_name, expected_country_code):
assert countries.country_code(country_name) == expected_country_code


@pytest.mark.parametrize(
"country_name_synonym, expected_country_code",
"country_name_alias, expected_country_code",
[("Deutschland", "DE"), ("Iran (Islamic Republic of)", "IR"), ("British Virgin Islands", "VG")],
)
def test_countrycodes_synonym(country_name_synonym, expected_country_code):
assert countrycodes.country_code(country_name_synonym) == expected_country_code
def test_country_name_alias(country_name_alias, expected_country_code):
assert countries.country_code(country_name_alias) == expected_country_code