Skip to content

Commit 6e0838b

Browse files
authored
Make importlib_resources optional (#187)
* Make importlib_resources optional * Add tox configuration
1 parent 28d8c5a commit 6e0838b

File tree

5 files changed

+54
-22
lines changed

5 files changed

+54
-22
lines changed

Lib/gflanguages/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@
2222
import glob
2323
import os
2424
import unicodedata
25+
import sys
2526

2627
from gflanguages import languages_public_pb2
2728
from google.protobuf import text_format
28-
from importlib_resources import files
29+
30+
if sys.version_info < (3, 10):
31+
from importlib_resources import files
32+
else:
33+
from importlib.resources import files
2934

3035
try:
3136
from ._version import version as __version__ # type: ignore

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ dynamic = ["version"]
1616

1717
name = "gflanguages"
1818
description = "A python API for evaluating language support in the Google Fonts collection."
19+
requires-python = ">=3.8"
1920
readme = "README.md"
2021
authors = [
2122
{ name = "Simon Cozens", email = "[email protected]" }
2223
]
2324

2425
dependencies = [
2526
"protobuf>=3.7.0, <4",
26-
"importlib_resources", # Needed for 3.9 and below
27+
"importlib_resources ; python_version < '3.10'",
2728
]
2829

2930
[project.optional-dependencies]

tests/test_gflanguages_api.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,45 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
#
17-
from pkg_resources import resource_filename
17+
18+
import sys
19+
20+
if sys.version_info < (3, 10):
21+
import importlib_resources as importlib_resources
22+
else:
23+
import importlib.resources as importlib_resources
1824

1925
from gflanguages import (LoadLanguages,
2026
LoadRegions,
2127
LoadScripts)
2228

23-
DATA_DIR = resource_filename("gflanguages", "data")
29+
DATA_DIR = importlib_resources.files("gflanguages") / "data"
2430

2531

2632
def test_LoadLanguages():
27-
for langs in [LoadLanguages(),
28-
LoadLanguages(None),
29-
LoadLanguages(DATA_DIR)]:
30-
numerals = langs["yi_Hebr"].exemplar_chars.numerals
31-
assert numerals == '- , . % + 0 1 2 3 4 5 6 7 8 9'
33+
with importlib_resources.as_file(DATA_DIR) as data_path:
34+
for langs in [LoadLanguages(),
35+
LoadLanguages(None),
36+
LoadLanguages(data_path)]:
37+
numerals = langs["yi_Hebr"].exemplar_chars.numerals
38+
assert numerals == '- , . % + 0 1 2 3 4 5 6 7 8 9'
3239

3340

3441
def test_LoadScripts():
35-
for scripts in [LoadScripts(),
36-
LoadScripts(None),
37-
LoadScripts(DATA_DIR)]:
38-
scripts = LoadScripts()
39-
assert scripts["Tagb"].name == 'Tagbanwa'
42+
with importlib_resources.as_file(DATA_DIR) as data_path:
43+
for scripts in [LoadScripts(),
44+
LoadScripts(None),
45+
LoadScripts(data_path)]:
46+
scripts = LoadScripts()
47+
assert scripts["Tagb"].name == 'Tagbanwa'
4048

4149

4250
def test_LoadRegions():
43-
for regions in [LoadRegions(),
44-
LoadRegions(None),
45-
LoadRegions(DATA_DIR)]:
46-
regions = LoadRegions()
47-
br = regions["BR"]
48-
assert br.name == 'Brazil'
49-
assert br.region_group == ['Americas']
51+
with importlib_resources.as_file(DATA_DIR) as data_path:
52+
for regions in [LoadRegions(),
53+
LoadRegions(None),
54+
LoadRegions(data_path)]:
55+
regions = LoadRegions()
56+
br = regions["BR"]
57+
assert br.name == 'Brazil'
58+
assert br.region_group == ['Americas']

tests/test_parsable.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from importlib_resources import files
21
import glob
32
import os
43
import pytest
4+
import sys
55
from gflanguages import languages_public_pb2
66
from google.protobuf import text_format
77

8+
if sys.version_info < (3, 10):
9+
from importlib_resources import files
10+
else:
11+
from importlib.resources import files
812

913
languages_dir = files("gflanguages.data").joinpath("languages")
1014
textproto_files = [

tox.ini

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[tox]
2+
env_list = py3{8,9,10,11,12,13}
3+
minversion = 4.23.2
4+
5+
[testenv]
6+
description = run the tests with pytest
7+
package = wheel
8+
wheel_build_env = .pkg
9+
deps =
10+
pytest>=6
11+
extras = dev
12+
commands =
13+
pytest {tty:--color=yes} {posargs}

0 commit comments

Comments
 (0)