Skip to content

Commit

Permalink
Replace pkg_resources with importlib
Browse files Browse the repository at this point in the history
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 <ahmubashshir@gmail.com>
  • Loading branch information
ahmubashshir authored and Diaoul committed May 9, 2024
1 parent ce44f5b commit 1652736
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
37 changes: 37 additions & 0 deletions babelfish/compat.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion babelfish/converters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion babelfish/country.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
2 changes: 1 addition & 1 deletion babelfish/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion babelfish/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_converters.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 1652736

Please sign in to comment.