From 1652736d55f8b24f69c82c6d2a5696bf16a10e07 Mon Sep 17 00:00:00 2001 From: Mubashshir Date: Fri, 17 Nov 2023 23:09:57 +0600 Subject: [PATCH] Replace pkg_resources with importlib pkg_resources is deprecated in favor of importlib.{resources,metadata} Closes #46 See-also: https://setuptools.pypa.io/en/latest/pkg_resources.html See-also: https://docs.python.org/3/library/importlib.resources.html See-also: https://docs.python.org/3/library/importlib.metadata.html Signed-off-by: Mubashshir --- babelfish/compat.py | 37 ++++++++++++++++++++++++++++++++ babelfish/converters/__init__.py | 2 +- babelfish/country.py | 2 +- babelfish/language.py | 2 +- babelfish/script.py | 2 +- pyproject.toml | 2 ++ tests/test_converters.py | 2 +- 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 babelfish/compat.py diff --git a/babelfish/compat.py b/babelfish/compat.py new file mode 100644 index 0000000..a546b98 --- /dev/null +++ b/babelfish/compat.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# +# Copyright (c) 2013 the BabelFish authors. All rights reserved. +# Use of this source code is governed by the 3-clause BSD license +# that can be found in the LICENSE file. +# +from sys import version_info as _python + +if _python >= (3, 9): + # introduced in python 3.9 + from importlib.resources import files +else: + from importlib_resources import files + + +if _python >= (3, 10): + # .select() was introduced in 3.10 + from importlib.metadata import entry_points, EntryPoint as _EntryPoint +else: + from importlib_metadata import entry_points, EntryPoint as _EntryPoint + + +def resource_stream(pkg, path): + return files(pkg).joinpath(f'{path}').open('rb') + + +def iter_entry_points(group, **kwargs): + return entry_points().select(group=group, **kwargs) + + +class EntryPoint(_EntryPoint): + @staticmethod + def parse(eps): + return EntryPoint(*map(str.strip, eps.split('=')), None) + + def resolve(self): + return self.load() diff --git a/babelfish/converters/__init__.py b/babelfish/converters/__init__.py index d27f849..13c0aa0 100644 --- a/babelfish/converters/__init__.py +++ b/babelfish/converters/__init__.py @@ -2,7 +2,7 @@ # Use of this source code is governed by the 3-clause BSD license # that can be found in the LICENSE file. # -from pkg_resources import iter_entry_points, EntryPoint +from ..compat import iter_entry_points, EntryPoint from ..exceptions import LanguageConvertError, LanguageReverseError try: diff --git a/babelfish/country.py b/babelfish/country.py index 4c24b52..6185856 100644 --- a/babelfish/country.py +++ b/babelfish/country.py @@ -7,9 +7,9 @@ from __future__ import unicode_literals from collections import namedtuple from functools import partial -from pkg_resources import resource_stream # @UnresolvedImport from .converters import ConverterManager from . import basestr +from .compat import resource_stream COUNTRIES = {} diff --git a/babelfish/language.py b/babelfish/language.py index b4b2519..fe5bfb6 100644 --- a/babelfish/language.py +++ b/babelfish/language.py @@ -7,12 +7,12 @@ from __future__ import unicode_literals from collections import namedtuple from functools import partial -from pkg_resources import resource_stream # @UnresolvedImport from .converters import ConverterManager from .country import Country from .exceptions import LanguageConvertError from .script import Script from . import basestr +from .compat import resource_stream LANGUAGES = set() diff --git a/babelfish/script.py b/babelfish/script.py index 4b59ce0..f073e95 100644 --- a/babelfish/script.py +++ b/babelfish/script.py @@ -6,8 +6,8 @@ # from __future__ import unicode_literals from collections import namedtuple -from pkg_resources import resource_stream # @UnresolvedImport from . import basestr +from .compat import resource_stream #: Script code to script name mapping SCRIPTS = {} diff --git a/pyproject.toml b/pyproject.toml index d53e050..2e253b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,6 +33,8 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.6" +importlib-resources = {version = "^5.0", python = "<3.9"} +importlib-metadata = {version = "^4.6", python = "<3.10"} [tool.poetry.dev-dependencies] pytest = "^6.2.4" diff --git a/tests/test_converters.py b/tests/test_converters.py index 7b76d5d..59c06a3 100644 --- a/tests/test_converters.py +++ b/tests/test_converters.py @@ -1,7 +1,7 @@ from babelfish.converters import LanguageReverseConverter import pytest -from pkg_resources import resource_stream from babelfish import language_converters +from babelfish.compat import resource_stream from babelfish.exceptions import LanguageConvertError, LanguageReverseError from babelfish.language import Language