-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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) |