Skip to content

Commit a495359

Browse files
authored
Use importlib API (#186)
* Don't repeat yourself * Use the new-fangled importlib.resources * Use backport until 3.9 is dead
1 parent 1037e30 commit a495359

File tree

3 files changed

+38
-45
lines changed

3 files changed

+38
-45
lines changed

Lib/gflanguages/__init__.py

+25-35
Original file line numberDiff line numberDiff line change
@@ -25,56 +25,46 @@
2525

2626
from gflanguages import languages_public_pb2
2727
from google.protobuf import text_format
28-
from pkg_resources import resource_filename
28+
from importlib_resources import files
2929

3030
try:
3131
from ._version import version as __version__ # type: ignore
3232
except ImportError:
3333
__version__ = "0.0.0+unknown"
3434

35-
DATA_DIR = resource_filename("gflanguages", "data")
3635

36+
def _load_thing(thing_type, proto_class, base_dir=None):
37+
things = {}
3738

38-
def LoadLanguages(base_dir=DATA_DIR):
39-
if base_dir is None:
40-
base_dir = DATA_DIR
39+
def read_a_thing(contents):
40+
proto = proto_class()
41+
thing = text_format.Parse(contents, proto)
42+
assert thing.id not in things, f"Duplicate {thing_type} id: {thing.id}"
43+
things[thing.id] = thing
4144

42-
languages_dir = os.path.join(base_dir, "languages")
43-
langs = {}
44-
for textproto_file in glob.iglob(os.path.join(languages_dir, "*.textproto")):
45-
with open(textproto_file, "r", encoding="utf-8") as f:
46-
language = text_format.Parse(f.read(), languages_public_pb2.LanguageProto())
47-
assert language.id not in langs, f"Duplicate language id: {language.id}"
48-
langs[language.id] = language
49-
return langs
45+
if base_dir is not None:
46+
thing_dir = os.path.join(base_dir, thing_type)
47+
for textproto_file in glob.iglob(os.path.join(thing_dir, "*.textproto")):
48+
with open(textproto_file, "r", encoding="utf-8") as f:
49+
read_a_thing(f.read())
50+
else:
51+
for textproto_file in files("gflanguages.data").joinpath(thing_type).iterdir():
52+
if not textproto_file.name.endswith(".textproto"):
53+
continue
54+
read_a_thing(textproto_file.read_text(encoding="utf-8"))
55+
return things
5056

5157

52-
def LoadScripts(base_dir=DATA_DIR):
53-
if base_dir is None:
54-
base_dir = DATA_DIR
58+
def LoadLanguages(base_dir=None):
59+
return _load_thing("languages", languages_public_pb2.LanguageProto, base_dir)
5560

56-
scripts_dir = os.path.join(base_dir, "scripts")
57-
scripts = {}
58-
for textproto_file in glob.iglob(os.path.join(scripts_dir, "*.textproto")):
59-
with open(textproto_file, "r", encoding="utf-8") as f:
60-
script = text_format.Parse(f.read(), languages_public_pb2.ScriptProto())
61-
assert script.id not in scripts, f"Duplicate script id: {script.id}"
62-
scripts[script.id] = script
63-
return scripts
6461

62+
def LoadScripts(base_dir=None):
63+
return _load_thing("scripts", languages_public_pb2.ScriptProto, base_dir)
6564

66-
def LoadRegions(base_dir=DATA_DIR):
67-
if base_dir is None:
68-
base_dir = DATA_DIR
6965

70-
regions_dir = os.path.join(base_dir, "regions")
71-
regions = {}
72-
for textproto_file in glob.iglob(os.path.join(regions_dir, "*.textproto")):
73-
with open(textproto_file, "r", encoding="utf-8") as f:
74-
region = text_format.Parse(f.read(), languages_public_pb2.RegionProto())
75-
assert region.id not in regions, f"Duplicate region id: {region.id}"
76-
regions[region.id] = region
77-
return regions
66+
def LoadRegions(base_dir=None):
67+
return _load_thing("regions", languages_public_pb2.RegionProto, base_dir)
7868

7969

8070
def parse(exemplars: str):

pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ authors = [
2222
]
2323

2424
dependencies = [
25-
"protobuf>=3.7.0, <4"
25+
"protobuf>=3.7.0, <4",
26+
"importlib_resources", # Needed for 3.9 and below
2627
]
2728

2829
[project.optional-dependencies]

tests/test_parsable.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
from gflanguages import DATA_DIR
1+
from importlib_resources import files
22
import glob
33
import os
44
import pytest
55
from gflanguages import languages_public_pb2
66
from google.protobuf import text_format
77

88

9-
languages_dir = os.path.join(DATA_DIR, "languages")
9+
languages_dir = files("gflanguages.data").joinpath("languages")
1010
textproto_files = [
11-
os.path.basename(x) for x in glob.iglob(os.path.join(languages_dir, "*.textproto"))
11+
file.name for file in languages_dir.iterdir() if file.name.endswith(".textproto")
1212
]
1313

1414

1515
@pytest.mark.parametrize("lang_code", textproto_files)
1616
def test_parsable(lang_code):
17-
with open(os.path.join(languages_dir, lang_code), "r", encoding="utf-8") as f:
18-
msg = text_format.Parse(f.read(), languages_public_pb2.LanguageProto())
19-
assert msg.id
20-
assert msg.language
21-
assert msg.script
22-
assert msg.population is not None
17+
f = languages_dir.joinpath(lang_code)
18+
msg = text_format.Parse(
19+
f.read_text(encoding="utf-8"), languages_public_pb2.LanguageProto()
20+
)
21+
assert msg.id
22+
assert msg.language
23+
assert msg.script
24+
assert msg.population is not None

0 commit comments

Comments
 (0)