53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
"""Python Gettext detector."""
|
|
|
|
from pathlib import Path
|
|
from typing import Optional
|
|
|
|
from i18n_guardectors.base import Detector
|
|
|
|
|
|
class PythonGettextDetector(Detector):
|
|
"""Detect Python gettext module usage."""
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "python-gettext"
|
|
|
|
@property
|
|
def patterns(self) -> list:
|
|
return ["*.py"]
|
|
|
|
def detect(self, path: Path) -> Optional[str]:
|
|
"""Check for Python gettext usage."""
|
|
for py_file in path.rglob("*.py"):
|
|
try:
|
|
content = py_file.read_text(encoding="utf-8")
|
|
if any(
|
|
pattern in content
|
|
for pattern in [
|
|
"import gettext",
|
|
"from gettext import",
|
|
"gettext.gettext",
|
|
"gettext.ngettext",
|
|
]
|
|
):
|
|
return self.name
|
|
except OSError:
|
|
continue
|
|
|
|
locale_dir = path / "locale"
|
|
if locale_dir.exists():
|
|
for _ in locale_dir.rglob("*/LC_MESSAGES/*.mo"):
|
|
return self.name
|
|
|
|
return None
|
|
|
|
def get_i18n_functions(self) -> list:
|
|
"""Get Python gettext function names."""
|
|
return [
|
|
"_",
|
|
"gettext",
|
|
"ngettext",
|
|
"pgettext",
|
|
]
|