Skip to content

Commit

Permalink
Make importlib_resources optional (#187)
Browse files Browse the repository at this point in the history
* Make importlib_resources optional

* Add tox configuration
  • Loading branch information
madig authored Nov 8, 2024
1 parent 28d8c5a commit 6e0838b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
7 changes: 6 additions & 1 deletion Lib/gflanguages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@
import glob
import os
import unicodedata
import sys

from gflanguages import languages_public_pb2
from google.protobuf import text_format
from importlib_resources import files

if sys.version_info < (3, 10):
from importlib_resources import files
else:
from importlib.resources import files

try:
from ._version import version as __version__ # type: ignore
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ dynamic = ["version"]

name = "gflanguages"
description = "A python API for evaluating language support in the Google Fonts collection."
requires-python = ">=3.8"
readme = "README.md"
authors = [
{ name = "Simon Cozens", email = "[email protected]" }
]

dependencies = [
"protobuf>=3.7.0, <4",
"importlib_resources", # Needed for 3.9 and below
"importlib_resources ; python_version < '3.10'",
]

[project.optional-dependencies]
Expand Down
47 changes: 28 additions & 19 deletions tests/test_gflanguages_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,45 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
from pkg_resources import resource_filename

import sys

if sys.version_info < (3, 10):
import importlib_resources as importlib_resources
else:
import importlib.resources as importlib_resources

from gflanguages import (LoadLanguages,
LoadRegions,
LoadScripts)

DATA_DIR = resource_filename("gflanguages", "data")
DATA_DIR = importlib_resources.files("gflanguages") / "data"


def test_LoadLanguages():
for langs in [LoadLanguages(),
LoadLanguages(None),
LoadLanguages(DATA_DIR)]:
numerals = langs["yi_Hebr"].exemplar_chars.numerals
assert numerals == '- , . % + 0 1 2 3 4 5 6 7 8 9'
with importlib_resources.as_file(DATA_DIR) as data_path:
for langs in [LoadLanguages(),
LoadLanguages(None),
LoadLanguages(data_path)]:
numerals = langs["yi_Hebr"].exemplar_chars.numerals
assert numerals == '- , . % + 0 1 2 3 4 5 6 7 8 9'


def test_LoadScripts():
for scripts in [LoadScripts(),
LoadScripts(None),
LoadScripts(DATA_DIR)]:
scripts = LoadScripts()
assert scripts["Tagb"].name == 'Tagbanwa'
with importlib_resources.as_file(DATA_DIR) as data_path:
for scripts in [LoadScripts(),
LoadScripts(None),
LoadScripts(data_path)]:
scripts = LoadScripts()
assert scripts["Tagb"].name == 'Tagbanwa'


def test_LoadRegions():
for regions in [LoadRegions(),
LoadRegions(None),
LoadRegions(DATA_DIR)]:
regions = LoadRegions()
br = regions["BR"]
assert br.name == 'Brazil'
assert br.region_group == ['Americas']
with importlib_resources.as_file(DATA_DIR) as data_path:
for regions in [LoadRegions(),
LoadRegions(None),
LoadRegions(data_path)]:
regions = LoadRegions()
br = regions["BR"]
assert br.name == 'Brazil'
assert br.region_group == ['Americas']
6 changes: 5 additions & 1 deletion tests/test_parsable.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
from importlib_resources import files
import glob
import os
import pytest
import sys
from gflanguages import languages_public_pb2
from google.protobuf import text_format

if sys.version_info < (3, 10):
from importlib_resources import files
else:
from importlib.resources import files

languages_dir = files("gflanguages.data").joinpath("languages")
textproto_files = [
Expand Down
13 changes: 13 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[tox]
env_list = py3{8,9,10,11,12,13}
minversion = 4.23.2

[testenv]
description = run the tests with pytest
package = wheel
wheel_build_env = .pkg
deps =
pytest>=6
extras = dev
commands =
pytest {tty:--color=yes} {posargs}

0 comments on commit 6e0838b

Please sign in to comment.