First commit of the project

This commit is contained in:
2026-11-20 21:35:24 +01:00
parent 4384ba380c
commit 71a162acc9
37 changed files with 4576 additions and 14 deletions
+142
View File
@@ -0,0 +1,142 @@
"""Shared test data and helpers for the ki18n test suite.
Imported by ``conftest.py`` and the test modules (pytest puts the ``test``
directory on ``sys.path`` because it is not a package).
"""
from __future__ import annotations
import json
from pathlib import Path
from ki18n.ki18n import Ki18n
PROJECT_ROOT = Path(__file__).resolve().parent.parent
#: Logger used by the library for warnings about ignored user resources.
LOGGER = "ki18n.ki18n"
# ---------------------------------------------------------------------------
# Example translations
# ---------------------------------------------------------------------------
EN = {
"language": "English",
"greeting": "Hello",
"farewell": "Goodbye",
"only.en": "Only in English",
"welcome": "Welcome, {name}!",
"files.found": {
"zero": "No files",
"one": "One file",
"many": "#count files",
"none": "Count missing",
},
}
DE = {
"language": "Deutsch",
"greeting": "Hallo",
"farewell": "Tschüss",
"welcome": "Willkommen, {name}!",
"files.found": {
"zero": "Keine Dateien",
"one": "Eine Datei",
"many": "#count Dateien",
},
}
#: Keys used by ``HelpScreen`` and ``LanguageSelectScreen``.
DIALOG_EN = {
"title_select_language": "Select language",
"title_help": "Example help",
"btn_close": "Close",
"btn_cancel": "Cancel",
"msg_help_missing": "Help file is missing!",
}
DIALOG_DE = {
"title_select_language": "Sprache wählen",
"title_help": "Beispiel-Hilfe",
"btn_close": "Schließen",
"btn_cancel": "Abbrechen",
"msg_help_missing": "Hilfe-Datei fehlt!",
}
def write_language(
root: Path,
language_dir: str,
strings: dict | str | bytes | None = None,
help_text: str | None = None,
) -> Path:
"""Create ``<root>/i18n/<language_dir>/`` with ``strings.json``/``help.md``.
``strings`` may be a ``dict`` (written as JSON), a ``str`` (raw text, e.g.
broken JSON), ``bytes`` (raw bytes) or ``None`` (no file).
"""
directory = root / "i18n" / language_dir
directory.mkdir(parents=True, exist_ok=True)
if isinstance(strings, dict):
(directory / "strings.json").write_text(
json.dumps(strings, ensure_ascii=False, indent=2), encoding="utf-8"
)
elif isinstance(strings, str):
(directory / "strings.json").write_text(strings, encoding="utf-8")
elif isinstance(strings, bytes):
(directory / "strings.json").write_bytes(strings)
if help_text is not None:
(directory / "help.md").write_text(help_text, encoding="utf-8")
return directory
class Env:
"""An isolated application + user-data environment below ``base``."""
def __init__(self, base: Path) -> None:
self.tmp = base
#: "Installation directory" of the test application (name: ``myapp``)
self.app_dir = base / "myapp"
self.app_dir.mkdir()
#: Per-user data directory of the application
self.user_dir = base / "userdata" / "myapp"
self.user_dir.mkdir(parents=True)
#: Pseudo home / XDG directory for tests without an explicit local_root
self.data_home = base / "datahome"
self.data_home.mkdir()
# -- Creating resources -------------------------------------------------
def install(self, language_dir, strings=None, help_text=None) -> Path:
"""Installed resource (``<project_root>/i18n/...``)."""
return write_language(self.app_dir, language_dir, strings, help_text)
def user(self, language_dir, strings=None, help_text=None) -> Path:
"""User resource (``<local_root>/i18n/...``)."""
return write_language(self.user_dir, language_dir, strings, help_text)
def install_defaults(self, with_help: bool = False) -> None:
"""Install ``en`` and ``de`` with the example data."""
self.install("en", EN, "# Help\n" if with_help else None)
self.install("de", DE, "# Hilfe\n" if with_help else None)
def installed_file(self, language_dir: str, name: str) -> Path:
"""Path of a file in an installed language folder."""
return self.app_dir / "i18n" / language_dir / name
# -- Creating managers --------------------------------------------------
def make(self, default: str = "en", **kwargs) -> Ki18n:
"""Create the manager using the isolated directories."""
kwargs.setdefault("project_root", self.app_dir)
kwargs.setdefault("local_root", self.user_dir)
return Ki18n(default, **kwargs)
def remake(self, default: str = "en", **kwargs) -> Ki18n:
"""Discard the singleton and create the manager again.
Needed when a test creates further resources after the first ``make()``
that must be discovered during construction.
"""
Ki18n._instance = None
return self.make(default, **kwargs)