diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 59fd1f39..65ebb43a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -32,7 +32,7 @@ repos: - id: mypy files: plumbum args: [] - additional_dependencies: [typed-ast, types-paramiko, types-setuptools, pytest] + additional_dependencies: [typed-ast, types-paramiko, types-setuptools, pytest, importlib-resources] - repo: https://github.com/abravalheri/validate-pyproject rev: "v0.16" diff --git a/plumbum/cli/i18n.py b/plumbum/cli/i18n.py index 57294f5f..7c2ba6fb 100644 --- a/plumbum/cli/i18n.py +++ b/plumbum/cli/i18n.py @@ -1,9 +1,6 @@ from __future__ import annotations import locale -import os - -DIR = os.path.abspath(os.path.dirname(__file__)) # High performance method for English (no translation needed) loc = locale.getlocale()[0] @@ -26,33 +23,30 @@ def get_translation_for( else: import gettext + import sys - # If not installed with setuptools, this might not be available - try: - import pkg_resources - except ImportError: - pkg_resources = None # type: ignore[assignment] + if sys.version_info < (3, 9): + from importlib_resources import as_file, files + else: + from importlib.resources import as_file, files def get_translation_for(package_name: str) -> gettext.NullTranslations: # type: ignore[misc] - """Find and return gettext translation for package - (Try to find folder manually if setuptools does not exist) + """ + Find and return gettext translation for package """ assert loc is not None if "." in package_name: package_name = ".".join(package_name.split(".")[:-1]) - localedir = None - if pkg_resources is None: - mydir = os.path.join(DIR, "i18n") - else: - mydir = pkg_resources.resource_filename(package_name, "i18n") + localedir = None - for localedir in mydir, None: - localefile = gettext.find(package_name, localedir, languages=[loc]) - if localefile: - break + with as_file(files(package_name) / "i18n") as mydir: + for localedir in mydir, None: + localefile = gettext.find(package_name, localedir, languages=[loc]) + if localefile: + break - return gettext.translation( - package_name, localedir=localedir, languages=[loc], fallback=True - ) + return gettext.translation( + package_name, localedir=localedir, languages=[loc], fallback=True + ) diff --git a/pyproject.toml b/pyproject.toml index 306a175a..cd1f6be6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ requires-python = ">=3.8" dynamic = ["version"] dependencies = [ "pywin32; platform_system=='Windows' and platform_python_implementation!='PyPy'", + "importlib_resources; python_version<'3.9'", ] classifiers = [ "Development Status :: 5 - Production/Stable",