]> git.ipfire.org Git - thirdparty/google/fonts.git/commitdiff
Make importlib_resources optional (#187)
authorNikolaus Waxweiler <madigens@gmail.com>
Fri, 8 Nov 2024 15:51:47 +0000 (15:51 +0000)
committerGitHub <noreply@github.com>
Fri, 8 Nov 2024 15:51:47 +0000 (15:51 +0000)
* Make importlib_resources optional

* Add tox configuration

Lib/gflanguages/__init__.py
pyproject.toml
tests/test_gflanguages_api.py
tests/test_parsable.py
tox.ini [new file with mode: 0644]

index aa6f565d9f0f7c7d7e95e7f3e52254db9bac0cde..4853acbb7b962f39f81c66463ed3829f6b7578d7 100644 (file)
@@ -22,10 +22,15 @@ data on the Google Fonts collection.
 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
index f0866ccc6756045a77fdda340aad0ea33e0c9ca4..d4f9ec549a956eb83f6e26e68561742b44fd8c5f 100644 (file)
@@ -16,6 +16,7 @@ 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 = "simon@simon-cozens.org" }
@@ -23,7 +24,7 @@ authors = [
 
 dependencies = [
    "protobuf>=3.7.0, <4",
-   "importlib_resources",  # Needed for 3.9 and below
+   "importlib_resources ; python_version < '3.10'",
 ]
 
 [project.optional-dependencies]
index 1219534952ac93d0e6619a573530b4f796ab21eb..34f348f27435d229dd57d010ffa7063b35fb703d 100644 (file)
 # 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']
index aeaf3f3a8ee4a9088ee2610e7a605a3f3e0e0f75..031631651817b01b376ab36e4588ee3b92295f38 100644 (file)
@@ -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 = [
diff --git a/tox.ini b/tox.ini
new file mode 100644 (file)
index 0000000..99be7c2
--- /dev/null
+++ b/tox.ini
@@ -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}