refactor: merge PO operations all in one
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
from . import pot, render
|
||||
from . import pofile, render
|
||||
from .cli import create_argparser
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ def main() -> None:
|
||||
try:
|
||||
if args.command == "rc":
|
||||
if args.rc_command == "pot":
|
||||
pot.generate(args.manifest, args.output)
|
||||
pofile.generate(args.manifest, args.output)
|
||||
elif args.rc_command == "render":
|
||||
render.render(args.manifest, args.po, args.template, args.output)
|
||||
except (ValueError, RuntimeError) as exc:
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
import glob
|
||||
from pathlib import Path
|
||||
|
||||
import polib
|
||||
|
||||
|
||||
def collect_po_files(patterns: list[Path]) -> list[Path]:
|
||||
files: list[Path] = []
|
||||
seen: set[Path] = set()
|
||||
for pattern in patterns:
|
||||
matched = sorted(glob.glob(str(pattern)))
|
||||
if not matched:
|
||||
raise ValueError(f"the pattern '{pattern}' matched no PO file")
|
||||
for name in matched:
|
||||
file = Path(name)
|
||||
resolved = file.resolve()
|
||||
if resolved not in seen:
|
||||
seen.add(resolved)
|
||||
files.append(file)
|
||||
return files
|
||||
|
||||
|
||||
def load_po(path: Path) -> tuple[str, dict[str, str]]:
|
||||
try:
|
||||
po = polib.pofile(str(path))
|
||||
except (OSError, IOError) as exc:
|
||||
raise RuntimeError(f"failed to read PO file '{path}': {exc}") from exc
|
||||
locale = po.metadata.get("Language") or path.stem
|
||||
translations = {
|
||||
entry.msgid: entry.msgstr
|
||||
for entry in po
|
||||
if entry.msgstr and "fuzzy" not in entry.flags
|
||||
}
|
||||
return locale, translations
|
||||
@@ -1,3 +1,4 @@
|
||||
import glob
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
@@ -16,6 +17,36 @@ _POT_HEADER = {
|
||||
}
|
||||
|
||||
|
||||
def collect_po_files(patterns: list[Path]) -> list[Path]:
|
||||
files: list[Path] = []
|
||||
seen: set[Path] = set()
|
||||
for pattern in patterns:
|
||||
matched = sorted(glob.glob(str(pattern)))
|
||||
if not matched:
|
||||
raise ValueError(f"the pattern '{pattern}' matched no PO file")
|
||||
for name in matched:
|
||||
file = Path(name)
|
||||
resolved = file.resolve()
|
||||
if resolved not in seen:
|
||||
seen.add(resolved)
|
||||
files.append(file)
|
||||
return files
|
||||
|
||||
|
||||
def load_po(path: Path) -> tuple[str, dict[str, str]]:
|
||||
try:
|
||||
po = polib.pofile(str(path))
|
||||
except (OSError, IOError) as exc:
|
||||
raise RuntimeError(f"failed to read PO file '{path}': {exc}") from exc
|
||||
locale = po.metadata.get("Language") or path.stem
|
||||
translations = {
|
||||
entry.msgid: entry.msgstr
|
||||
for entry in po
|
||||
if entry.msgstr and "fuzzy" not in entry.flags
|
||||
}
|
||||
return locale, translations
|
||||
|
||||
|
||||
def generate(manifest_path: Path, output_path: Path) -> None:
|
||||
manifest: Manifest = load_manifest(manifest_path)
|
||||
po = polib.POFile()
|
||||
@@ -5,8 +5,8 @@ from liquid import Environment, StrictUndefined
|
||||
from liquid.exceptions import LiquidError
|
||||
|
||||
from . import winlang
|
||||
from .catalog import collect_po_files, load_po
|
||||
from .manifest import Manifest, load_manifest
|
||||
from .pofile import collect_po_files, load_po
|
||||
|
||||
|
||||
def _rc_escape(value: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user