Skip to content

Commit

Permalink
fix: use importlib resources
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Apr 29, 2024
1 parent 06da1f6 commit 9ed0386
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
34 changes: 14 additions & 20 deletions plumbum/cli/i18n.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 9ed0386

Please sign in to comment.