diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4c28926d..3eac7b20 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 933b2785..c6e7df8f 100644 --- a/plumbum/cli/i18n.py +++ b/plumbum/cli/i18n.py @@ -21,33 +21,27 @@ def get_translation_for( else: import gettext - import os + import sys - # If not installed with setuptools, this might not be available - try: - import pkg_resources - except ImportError: - pkg_resources = None # type: ignore[assignment] - - local_dir = os.path.basename(__file__) + 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 """ if "." in package_name: package_name = ".".join(package_name.split(".")[:-1]) - localedir = None - if pkg_resources is None: - mydir = os.path.join(local_dir, "i18n") - else: - mydir = pkg_resources.resource_filename(package_name, "i18n") + localedir = None - for localedir in mydir, None: - localefile = gettext.find(package_name, localedir) - if localefile: - break + with as_file(files(package_name) / "i18n") as mydir: + for localedir in mydir, None: + localefile = gettext.find(package_name, localedir) + if localefile: + break - return gettext.translation(package_name, localedir=localedir, fallback=True) + return gettext.translation(package_name, localedir=localedir, fallback=True) diff --git a/pyproject.toml b/pyproject.toml index b7057f5b..fd16c5c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,6 +16,7 @@ requires-python = ">=3.6" dynamic = ["version"] dependencies = [ "pywin32; platform_system=='Windows' and platform_python_implementation!='PyPy'", + "importlib_resources; python_version<'3.9'", ] classifiers = [ "Development Status :: 5 - Production/Stable",