Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace pkg_resources with importlib #47

Merged
merged 1 commit into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading