Skip to content

Commit eceb28f

Browse files
committed
Migrate from iso-639 to iso639-lang library, former one is not maintained anymore
1 parent c9cf343 commit eceb28f

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

Diff for: pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ requires-python = ">=3.8,<3.13"
88
description = "Collection of python tools to re-use common code across scrapers"
99
readme = "README.md"
1010
dependencies = [
11-
"iso-639==0.4.5",
11+
"iso639-lang==2.2.3",
1212
"requests==2.31.0",
1313
"colorthief==0.2.1",
1414
"python-resize-image==1.1.20",

Diff for: src/zimscraperlib/i18n.py

+32-16
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from typing import Dict, Optional, Tuple, Union
99

1010
import babel
11-
from iso639 import languages as iso639_languages
11+
import iso639
12+
import iso639.exceptions
1213

1314
ISO_LEVELS = ["1", "2b", "2t", "3", "5"]
1415

@@ -67,27 +68,42 @@ def get_iso_lang_data(lang: str) -> Tuple[Dict, Union[Dict, None]]:
6768

6869
iso_types = []
6970

70-
for code_type in [f"part{lang_}" for lang_ in ISO_LEVELS] + ["name"]:
71-
try:
72-
iso639_languages.get(**{code_type: lang})
73-
iso_types.append(code_type)
74-
except KeyError:
75-
pass
76-
77-
if not iso_types:
78-
raise NotFound("Not a valid iso language name/code")
79-
80-
language = iso639_languages.get(**{iso_types[0]: lang})
71+
try:
72+
isolang = iso639.Lang(lang)
73+
except iso639.exceptions.InvalidLanguageValue as exc:
74+
raise NotFound("Not a valid iso language name/code") from exc
75+
76+
def replace_types(new_type: str) -> str:
77+
# convert new iso_types from iso639-lang Pypi package to old iso_types from
78+
# iso-639 package, since we were returning these values for a long time
79+
if new_type == "pt1":
80+
return "part1"
81+
elif new_type == "pt2b":
82+
return "part2b"
83+
elif new_type == "pt2t":
84+
return "part2t"
85+
elif new_type == "pt3":
86+
return "part3"
87+
elif new_type == "pt5":
88+
return "part5"
89+
return new_type
90+
91+
for code_type in [f"pt{lang_}" for lang_ in ISO_LEVELS] + ["name"]:
92+
# the `if` condition below is a bit hackish but it is the only way to know
93+
# if the passed value is matching a code type or not with new python-i639
94+
# library and we do not expect weird things to happen here
95+
if str(getattr(isolang, code_type)).lower() == lang.lower():
96+
iso_types.append(replace_types(code_type))
8197

8298
lang_data = {
83-
f"iso-639-{lang_}": getattr(language, f"part{lang_}") for lang_ in ISO_LEVELS
99+
f"iso-639-{lang_}": getattr(isolang, f"pt{lang_}") for lang_ in ISO_LEVELS
84100
}
85-
lang_data.update({"english": language.name, "iso_types": iso_types})
101+
lang_data.update({"english": isolang.name, "iso_types": iso_types})
86102

87-
if language.macro:
103+
if isolang.macro():
88104
return (
89105
lang_data,
90-
get_iso_lang_data(language.macro)[0],
106+
get_iso_lang_data(isolang.macro().name)[0],
91107
) # first item in the returned tuple
92108
return lang_data, None
93109

Diff for: tests/i18n/test_i18n.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,11 @@ def test_selocale_unsupported(tmp_path):
9696
{
9797
"iso-639-1": "",
9898
"iso-639-2b": "afa",
99-
"iso-639-2t": "afa",
99+
"iso-639-2t": "",
100100
"iso-639-3": "",
101101
"iso-639-5": "afa",
102102
"english": "Afro-Asiatic languages",
103-
"iso_types": ["part2b", "part2t", "part5"],
103+
"iso_types": ["part2b", "part5"],
104104
"querytype": "purecode",
105105
"query": "afa",
106106
"native": "Afro-Asiatic languages",
@@ -111,7 +111,7 @@ def test_selocale_unsupported(tmp_path):
111111
{
112112
"iso-639-1": "",
113113
"iso-639-2b": "afa",
114-
"iso-639-2t": "afa",
114+
"iso-639-2t": "",
115115
"iso-639-3": "",
116116
"iso-639-5": "afa",
117117
"english": "Afro-Asiatic languages",

0 commit comments

Comments
 (0)