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

fix: use importlib resources #680

Merged
merged 1 commit into from
Oct 5, 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
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
38 changes: 16 additions & 22 deletions plumbum/cli/i18n.py
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
)
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.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",
Expand Down