From 128cab077c8660d6ac4437c6ee618ef6179b3678 Mon Sep 17 00:00:00 2001 From: Timmy Welch Date: Thu, 23 Nov 2023 14:21:21 -0800 Subject: [PATCH] Replace pycountry with isocodes isocodes is updated more often and doesn't depend on deprecated packages --- comicapi/utils.py | 30 +++++++++++++++++------------- setup.cfg | 2 +- tests/utils_test.py | 3 ++- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/comicapi/utils.py b/comicapi/utils.py index e53d41c..f835738 100644 --- a/comicapi/utils.py +++ b/comicapi/utils.py @@ -305,21 +305,19 @@ _countries: dict[str | None, str | None] = defaultdict(lambda: None) def countries() -> dict[str | None, str | None]: if not _countries: - import pycountry + import isocodes - for c in pycountry.countries: - if "alpha_2" in c._fields: - _countries[c.alpha_2] = c.name + for alpha_2, c in isocodes.countries.by_alpha_2: + _countries[alpha_2] = c["name"] return _countries def languages() -> dict[str | None, str | None]: if not _languages: - import pycountry + import isocodes - for lng in pycountry.languages: - if "alpha_2" in lng._fields: - _languages[lng.alpha_2] = lng.name + for alpha_2, lng in isocodes.extendend_languages._sorted_by_index(index="alpha_2"): + _languages[alpha_2] = lng["name"] return _languages @@ -330,15 +328,21 @@ def get_language_from_iso(iso: str | None) -> str | None: def get_language_iso(string: str | None) -> str | None: if string is None: return None - import pycountry + import isocodes # Return current string if all else fails lang = string.casefold() - try: - return getattr(pycountry.languages.lookup(string), "alpha_2", None) - except LookupError: - pass + found = None + for lng in isocodes.extendend_languages.items: + for x in ("alpha_2", "alpha_3", "bibliographic", "common_name", "name"): + if x in lng and lng[x].casefold() == lang: + found = lng + if found: + break + + if found: + return found.get("alpha_2", None) return lang diff --git a/setup.cfg b/setup.cfg index 5b986ce..73fc1c0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,10 +38,10 @@ install_requires = beautifulsoup4>=4.1 chardet>=5.1.0,<6 importlib-metadata>=3.3.0 + isocodes>2023.08.29 natsort>=8.1.0 pathvalidate pillow>=9.1.0,<10 - pycountry pyrate-limiter>=2.6,<3 rapidfuzz>=2.12.0 requests==2.* diff --git a/tests/utils_test.py b/tests/utils_test.py index 7115ab9..d572bf9 100644 --- a/tests/utils_test.py +++ b/tests/utils_test.py @@ -124,7 +124,8 @@ language_values = [ ("english", "en"), ("ENGLISH", "en"), ("EnglisH", "en"), - ("", ""), + ("", ""), # Does not match a language + ("unknown", "unknown"), # Does not match a language ("aaa", None), # does not have a 2-letter code (None, None), ]