From 45975a4b1155e5730c13dc5fdd11aa95550b6d67 Mon Sep 17 00:00:00 2001 From: insolor <2442833+insolor@users.noreply.github.com> Date: Thu, 24 Oct 2024 00:01:59 +0300 Subject: [PATCH] Remove some of the obsolete files --- .../create_pot/from_speech.py | 42 -------------- df_translation_toolkit/translate/batch.py | 58 ------------------- tests/test_translate_raws_batch.py | 30 ---------- 3 files changed, 130 deletions(-) delete mode 100644 df_translation_toolkit/create_pot/from_speech.py delete mode 100644 df_translation_toolkit/translate/batch.py delete mode 100644 tests/test_translate_raws_batch.py diff --git a/df_translation_toolkit/create_pot/from_speech.py b/df_translation_toolkit/create_pot/from_speech.py deleted file mode 100644 index f2d8ea8..0000000 --- a/df_translation_toolkit/create_pot/from_speech.py +++ /dev/null @@ -1,42 +0,0 @@ -import sys -from collections.abc import Iterable, Iterator, Sequence -from pathlib import Path -from typing import TextIO - -import typer - -from df_translation_toolkit.utils.po_utils import TranslationItem, save_pot - - -def extract_from_speech_file(file: TextIO, source_path: str) -> Iterator[TranslationItem]: - for i, line in enumerate(file, 1): - text = line.rstrip("\n") - if text: - yield TranslationItem(text=text, source_file=source_path, line_number=i) - - -def extract_translatables(files: Iterable[Path]) -> Iterator[TranslationItem]: - for file_path in files: - if file_path.is_file(): - print("File:", file_path.name, file=sys.stderr) - with file_path.open() as file: - yield from extract_from_speech_file(file, file_path.name) - - -def create_pot_file(pot_file: typer.FileBinaryWrite, files: Sequence[Path]) -> None: - save_pot( - pot_file, - extract_translatables(files), - ) - - -def main( - path: Path, - pot_file: typer.FileBinaryWrite, -) -> None: - files = (file for file in path.glob("*.txt") if file.is_file()) - create_pot_file(pot_file, sorted(files)) - - -if __name__ == "__main__": - typer.run(main) diff --git a/df_translation_toolkit/translate/batch.py b/df_translation_toolkit/translate/batch.py deleted file mode 100644 index f6635a2..0000000 --- a/df_translation_toolkit/translate/batch.py +++ /dev/null @@ -1,58 +0,0 @@ -from collections.abc import Callable, Iterable, Iterator, Sequence -from pathlib import Path -from typing import NamedTuple - -import typer - -from df_translation_toolkit.translate.translate_plain_text import translate_plain_text -from df_translation_toolkit.translate.translate_raws import translate_raws - - -class Pattern(NamedTuple): - directory: str - po_filename: str - function: Callable[[Path, Path, str], Iterable[str]] - - -patterns = [ - Pattern("raw/objects", "raw-objects", translate_raws), - Pattern("data/speech", "speech", lambda *args: translate_plain_text(*args, join_paragraphs=False)), - Pattern("raw/objects/text", "text", lambda *args: translate_plain_text(*args, join_paragraphs=False)), -] - - -def translate_files( # noqa: PLR0913 - base_path: Path, - po_directory: Path, - encoding: str, - po_name_postfix: str = "", - *, - translate: bool = True, - directory_patterns: Sequence[Pattern] = tuple(patterns), -) -> Iterator[str]: - for cur_dir in base_path.rglob("*"): - if cur_dir.is_dir(): - for directory, po_filename, function in directory_patterns: - if cur_dir.match("*/" + directory): - yield f"Matched {directory} pattern" - - filename = f"{po_filename}_{po_name_postfix}.po" - po_file_path = po_directory / filename - - if translate: - yield from function(po_file_path, cur_dir, encoding) - - -def main( - base_path: Path, - po_directory: Path, - encoding: str, - po_name_postfix: str = "", - translate: bool = True, # noqa: FBT001, FBT002 -) -> None: - for message in translate_files(base_path, po_directory, encoding, po_name_postfix, translate): - print(message) - - -if __name__ == "__main__": - typer.run(main) diff --git a/tests/test_translate_raws_batch.py b/tests/test_translate_raws_batch.py deleted file mode 100644 index ad279c7..0000000 --- a/tests/test_translate_raws_batch.py +++ /dev/null @@ -1,30 +0,0 @@ -from pathlib import PurePath -from unittest.mock import Mock - -from df_translation_toolkit.translate.batch import Pattern, translate_files - - -def test_translate_files(): - def function(po_filename, path, encoding): - yield po_filename, path, encoding - - directory_name = "directory" - - po_filename = "po_filename" - patterns = [Pattern(directory_name, po_filename, function=function)] - - base_dir_mock = Mock("base_dir") - directory_mock = Mock("directory") - directory_mock.is_dir = lambda: True - directory_mock.match = lambda pattern: pattern == "*/" + directory_name - base_dir_mock.rglob = lambda *_args: [directory_mock] - po_directory = PurePath("po_directory") - encoding = "utf-8" - postfix = "postfix" - - assert list( - translate_files(base_dir_mock, po_directory, encoding, po_name_postfix=postfix, directory_patterns=patterns), - ) == [ - f"Matched {directory_name} pattern", - (po_directory / f"{po_filename}_{postfix}.po", directory_mock, encoding), - ]