Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor committed Jun 1, 2024
1 parent 1f86173 commit e204ba7
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion df_translation_toolkit/convert/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import df_translation_toolkit.convert.cli
from df_translation_toolkit.convert import cli

__all__ = ["cli"]
4 changes: 1 addition & 3 deletions df_translation_toolkit/convert/hardcoded_po_to_csv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#! python3
from collections.abc import Iterable
from pathlib import Path
from typing import TextIO
Expand All @@ -13,8 +12,7 @@
def prepare_dictionary(dictionary: Iterable[tuple[str, str]]) -> Iterable[tuple[str, str]]:
for original_string, translation in dictionary:
if original_string and translation and translation != original_string:
translation = fix_spaces(original_string, translation)
yield original_string, cleanup_string(translation)
yield original_string, cleanup_string(fix_spaces(original_string, translation))


def convert(po_file: TextIO, csv_file: TextIO) -> None:
Expand Down
5 changes: 3 additions & 2 deletions df_translation_toolkit/convert/objects_po_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@


def get_translations_from_tag_parts(
original_parts: list[str], translation_parts: list[str]
original_parts: list[str],
translation_parts: list[str],
) -> Iterator[tuple[str, str]]:
tag_translations = defaultdict(list)

Expand Down Expand Up @@ -66,7 +67,7 @@ def prepare_dictionary(dictionary: Iterable[tuple[str, str]], errors_file: TextI
print(error_text, end="\n\n", file=errors_file)


def convert(po_file: TextIO, csv_file: TextIO, error_file: TextIO = None):
def convert(po_file: TextIO, csv_file: TextIO, error_file: TextIO | None = None) -> None:
dictionary = simple_read_po(po_file)
csv_writer = csv_utils.writer(csv_file)

Expand Down
2 changes: 1 addition & 1 deletion df_translation_toolkit/create_mod/from_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def get_dictionaries(translation_path: Path, language: str) -> Dictionaries:
if file.is_file() and file.stat().st_mtime > mtime:
po_files[po_file] = file
if not po_files[po_file].is_file():
raise Exception(f"Unable to find {po_file} po file for language {language}")
raise ValueError(f"Unable to find {po_file} po file for language {language}")

with open(po_files["objects"], encoding="utf-8") as pofile:
dictionary_object: Mapping[tuple[str, str | None], str] = {
Expand Down
6 changes: 3 additions & 3 deletions df_translation_toolkit/create_pot/from_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from df_translation_toolkit.utils.po_utils import TranslationItem, save_pot


def extract_from_speech_file(file: TextIO, source_path: str):
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:
Expand All @@ -23,7 +23,7 @@ def extract_translatables(files: Iterable[Path]) -> Iterator[TranslationItem]:
yield from extract_from_speech_file(file, file_path.name)


def create_pot_file(pot_file: typer.FileBinaryWrite, files: Sequence[Path]):
def create_pot_file(pot_file: typer.FileBinaryWrite, files: Sequence[Path]) -> None:
save_pot(
pot_file,
extract_translatables(files),
Expand All @@ -33,7 +33,7 @@ def create_pot_file(pot_file: typer.FileBinaryWrite, files: Sequence[Path]):
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))

Expand Down
13 changes: 7 additions & 6 deletions df_translation_toolkit/create_pot/from_steam_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ def get_translatable_strings(file_path: Path, source_encoding: str) -> tuple[str
object_type = get_raw_object_type(file_path, source_encoding)
if object_type in dont_translate:
return None
elif object_type == "TEXT_SET":

if object_type == "TEXT_SET":
key = object_type
data = extract_from_vanilla_text(file_path, source_encoding)
else:
Expand All @@ -42,14 +43,15 @@ def get_translatable_strings(file_path: Path, source_encoding: str) -> tuple[str
return key, data


def iterable_is_empty(iterable: Iterable):
def iterable_is_empty(iterable: Iterable) -> bool:
iterator = iter(iterable)
try:
next(iterator)
return False
except StopIteration:
return True

return False


def file_is_translatable(file_path: Path, source_encoding: str) -> bool:
result = get_translatable_strings(file_path, source_encoding)
Expand All @@ -59,7 +61,7 @@ def file_is_translatable(file_path: Path, source_encoding: str) -> bool:
dont_translate = {"LANGUAGE"}


def main(vanilla_path: Path, destination_path: Path, source_encoding: str = "cp437"):
def main(vanilla_path: Path, destination_path: Path, source_encoding: str = "cp437") -> None:
assert vanilla_path.exists(), "Source path doesn't exist"
assert destination_path.exists(), "Destination path doesn't exist"

Expand All @@ -72,9 +74,8 @@ def main(vanilla_path: Path, destination_path: Path, source_encoding: str = "cp4
result = get_translatable_strings(file_path, source_encoding)
if not result:
continue
else:
group, data = result

group, data = result
results[group].extend(data)

for group, data in results.items():
Expand Down
2 changes: 1 addition & 1 deletion df_translation_toolkit/parse/parse_plain_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def local_is_translatable(s: str) -> bool:

paragraph: list[str] = []

# FIXME: join_paragraphs must only affect on paragraph joining, not line skipping
# FIXME(@insolor): join_paragraphs must only affect on paragraph joining, not line skipping
# so the first line must be skipped before the text is fed to the function
if join_paragraphs:
# The first line contains file name, skip it
Expand Down
11 changes: 7 additions & 4 deletions df_translation_toolkit/parse/parse_raws.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from df_translation_toolkit.utils.po_utils import TranslationItem


def all_caps(string: str):
def all_caps(string: str) -> bool:
return len(string) > 1 and string.isupper()


Expand Down Expand Up @@ -119,11 +119,14 @@ def get_from_dict_with_context(
) -> str | None:
if (key, context) in dictionary:
return dictionary[(key, context)]
elif (key, None) in dictionary:

if (key, None) in dictionary:
return dictionary[(key, None)]

return None


def get_tag_translation(dictionary: Mapping[tuple[str, str | None], str], item: FilePartInfo):
def get_tag_translation(dictionary: Mapping[tuple[str, str | None], str], item: FilePartInfo) -> str:
tag = item.tag
tag_parts = item.tag_parts
context = item.context
Expand All @@ -148,7 +151,7 @@ def get_tag_translation(dictionary: Mapping[tuple[str, str | None], str], item:
return tag


def translate_raw_file(file: Iterable[str], dictionary: Mapping[tuple[str, str | None], str]):
def translate_raw_file(file: Iterable[str], dictionary: Mapping[tuple[str, str | None], str]) -> Iterator[str]:
prev_line_number = 1
modified_line_parts: list[str] = []
for item in parse_raw_file(file):
Expand Down
2 changes: 1 addition & 1 deletion df_translation_toolkit/translate/translate_plain_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def translate_plain_text_file(
with source_file_path.open() as source_file:
with destination_file_path.open("w", encoding=encoding) as destination_file:
yield destination_file_path.name
for text_block, is_translatable, _ in parse_plain_text_file(source_file, join_paragraphs):
for text_block, _, _ in parse_plain_text_file(source_file, join_paragraphs):
text_block = text_block.rstrip("\n")
if text_block in dictionary:
translation = dictionary[text_block]
Expand Down
3 changes: 2 additions & 1 deletion df_translation_toolkit/utils/backup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import shutil
from collections.abc import Iterator
from contextlib import contextmanager
from pathlib import Path


@contextmanager
def backup(file: str | Path, overwrite: bool = False):
def backup(file: str | Path, overwrite: bool = False) -> Iterator[Path]:
file = Path(file)
backup_path = file.with_suffix(".bak")

Expand Down
2 changes: 1 addition & 1 deletion df_translation_toolkit/utils/csv_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def reader(file: TextIO, **kwargs):
return csv.reader(file, dialect="unix", lineterminator="\r\n", **kwargs)


def write_csv(file_path: Path, encoding: str, data: list[list[str]]):
def write_csv(file_path: Path, encoding: str, data: list[list[str]]) -> None:
with file_path.open("w", encoding=encoding, newline="") as file:
csv_writer = writer(file)
csv_writer.writerows(data)
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ select = ["ALL"]
ignore = [
"D",
"RUF001",
"ANN002",
"ANN003",
"ANN101",
"PTH123",
"T201",
Expand Down

0 comments on commit e204ba7

Please sign in to comment.