Skip to content

Commit

Permalink
Use ruff (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor committed Jul 5, 2024
1 parent 1240cd9 commit b3145cb
Show file tree
Hide file tree
Showing 35 changed files with 440 additions and 337 deletions.
3 changes: 1 addition & 2 deletions df_translation_toolkit/cli.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import typer

import df_translation_toolkit.convert as convert
from df_translation_toolkit import create_pot
from df_translation_toolkit import convert, create_pot

app = typer.Typer()
app.add_typer(convert.cli.app, name="convert")
Expand Down
4 changes: 3 additions & 1 deletion df_translation_toolkit/convert/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
import df_translation_toolkit.convert.cli
from df_translation_toolkit.convert import cli

__all__ = ["cli"]
16 changes: 8 additions & 8 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,11 +12,10 @@
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):
def convert(po_file: TextIO, csv_file: TextIO) -> None:
dictionary = simple_read_po(po_file)
csv_writer = csv_utils.writer(csv_file)

Expand All @@ -29,14 +27,16 @@ def convert(po_file: TextIO, csv_file: TextIO):


@app.command()
def main(po_file: Path, csv_file: Path, encoding: str):
def main(po_file: Path, csv_file: Path, encoding: str) -> None:
"""
Convert a po file into a csv file in a specified encoding
"""

with po_file.open("r", encoding="utf-8") as pofile:
with csv_file.open("w", newline="", encoding=encoding, errors="replace") as outfile:
convert(pofile, outfile)
with (
po_file.open("r", encoding="utf-8") as pofile,
csv_file.open("w", newline="", encoding=encoding, errors="replace") as outfile,
):
convert(pofile, outfile)


if __name__ == "__main__":
Expand Down
28 changes: 16 additions & 12 deletions df_translation_toolkit/convert/objects_po_to_csv.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import defaultdict
from collections.abc import Iterable
from collections.abc import Iterable, Iterator
from pathlib import Path
from typing import TextIO

Expand All @@ -15,12 +15,15 @@
from df_translation_toolkit.validation.validation_models import ValidationException, ValidationProblem


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

prev_original = None
prev_translation = None
for original, translation in zip(original_parts, translation_parts):
for original, translation in zip(original_parts, translation_parts, strict=False):
original: str
if all_caps(original) or original.isdecimal():
if original == "STP" and translation != original and not all_caps(translation):
Expand All @@ -35,7 +38,7 @@ def get_translations_from_tag_parts(original_parts: list[str], translation_parts
yield original, translations[0]


def get_translations_from_tag(original_tag: str, translation_tag: str):
def get_translations_from_tag(original_tag: str, translation_tag: str) -> Iterator[tuple[str, str]]:
validation_problems = list(validate_tag(original_tag, translation_tag))
if ValidationProblem.contains_errors(validation_problems):
raise ValidationException(validation_problems)
Expand All @@ -55,16 +58,15 @@ def prepare_dictionary(dictionary: Iterable[tuple[str, str]], errors_file: TextI
if original_string_tag and translation_tag and translation_tag != original_string_tag:
try:
for original_string, translation in get_translations_from_tag(original_string_tag, translation_tag):
translation = fix_spaces(original_string, translation)
yield original_string, cleanup_string(translation)
yield original_string, cleanup_string(fix_spaces(original_string, translation))
except ValidationException as ex:
error_text = f"Problematic tag pair: {original_string_tag!r}, {translation_tag!r}\nProblems:\n{ex}"
logger.error("\n" + error_text)
if errors_file:
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 All @@ -76,16 +78,18 @@ def convert(po_file: TextIO, csv_file: TextIO, error_file: TextIO = None):


@app.command()
def main(po_file: Path, csv_file: Path, encoding: str, append: bool = False, errors_file: Path = None):
def main(po_file: Path, csv_file: Path, encoding: str, append: bool = False, errors_file: Path | None = None) -> None: # noqa: FBT001, FBT002
"""
Convert a po file into a csv file in a specified encoding
"""

with open(po_file, "r", encoding="utf-8") as pofile:
with open(po_file, encoding="utf-8") as pofile:
mode = "a" if append else "w"
with open(csv_file, mode, newline="", encoding=encoding, errors="replace") as outfile:
with maybe_open(errors_file, "w", encoding="utf-8") as errors_file:
convert(pofile, outfile, errors_file)
with (
open(csv_file, mode, newline="", encoding=encoding, errors="replace") as outfile,
maybe_open(errors_file, "w", encoding="utf-8") as errors_file,
):
convert(pofile, outfile, errors_file)


if __name__ == "__main__":
Expand Down
1 change: 0 additions & 1 deletion df_translation_toolkit/create_mod/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
import df_translation_toolkit.create_mod.cli
14 changes: 10 additions & 4 deletions df_translation_toolkit/create_mod/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def fetch_po_from_git(language: str, destination_path: Path) -> None:
resources: list[str] = ["objects", "text_set"]
for resource in resources:
response = requests.get(f"{PO_URL}/{resource}/{language.lower()}.po")
response = requests.get(f"{PO_URL}/{resource}/{language.lower()}.po", timeout=60)
response.raise_for_status()
file_path = Path(destination_path / f"{resource}_{language.lower()}.po")
with file_path.open("w", encoding="utf-8") as file:
Expand All @@ -24,14 +24,20 @@ def fetch_po_from_git(language: str, destination_path: Path) -> None:

@logger.catch
def main(vanilla_path: Path, destination_path: Path, encoding: str, languages: list[str]) -> None:
assert vanilla_path.exists(), "Source path doesn't exist"
assert destination_path.exists(), "Destination path doesn't exist"
if not vanilla_path.exists():
msg = "Source path doesn't exist"
raise ValueError(msg)

if not destination_path.exists():
msg = "Destination path doesn't exist"
raise ValueError(msg)

for language in languages:
try:
fetch_po_from_git(language, destination_path)
except HTTPError as e:
raise Exception(f"Unable to download po file for language {language}. Error: {e.code}, {e.reason}")
msg = f"Unable to download po file for language {language}. Error: {e.code}, {e.reason}"
raise Exception(msg) from e # noqa: TRY002
Path.mkdir(destination_path / language.lower(), parents=True, exist_ok=True)
template_from_vanilla(vanilla_path, destination_path / language.lower())
from_template(destination_path / language.lower(), destination_path, language, encoding)
Expand Down
40 changes: 26 additions & 14 deletions df_translation_toolkit/create_mod/from_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def create_single_localized_mod(
translated_files = len(list((template_path / "objects").glob("*.txt")))
logger.info(f"Translated: {translated_files} files")
language_name = dictionaries.language_name
create_info(template_path / "info.txt", source_encoding, destination_encoding, language_name)
create_info(template_path / "info.txt", destination_encoding, language_name)

svg_template_path = Path(__file__).parent / "templates" / "preview_template.svg"
generate_preview(
Expand All @@ -54,11 +54,18 @@ def localize_directory(
with backup(file_path) as bak_name:
if object_type == "TEXT_SET":
yield from translate_plain_text_file(
bak_name, file_path, dictionaries.dictionary_textset, destination_encoding, False
bak_name,
file_path,
dictionaries.dictionary_textset,
destination_encoding,
join_paragraphs=False,
)
else:
yield from translate_single_raw_file(
bak_name, file_path, dictionaries.dictionary_object, destination_encoding
bak_name,
file_path,
dictionaries.dictionary_object,
destination_encoding,
)


Expand All @@ -68,11 +75,10 @@ def fill_info_template(template_path: Path, **kwargs: str | list[str] | dict[str
template = jinja2.Template(template_text)

rendered = template.render(**kwargs)
result = "\n".join(filter(bool, rendered.splitlines()))
return result
return "\n".join(filter(bool, rendered.splitlines()))


def create_info(info_file: Path, source_encoding: str, destination_encoding: str, language: str) -> None:
def create_info(info_file: Path, destination_encoding: str, language: str) -> None:
with info_file.open("w", encoding=destination_encoding) as dest:
info_template_path = Path(__file__).parent / "templates" / "info_template.txt"
rendered = fill_info_template(
Expand All @@ -84,7 +90,7 @@ def create_info(info_file: Path, source_encoding: str, destination_encoding: str
steam_title=f"{language.upper()} Translation",
steam_description=f"Translation to {language.upper()} language for vanilla mods",
steam_tags=["ui", "qol", "translation"],
steam_key_value_tags=dict(language=language),
steam_key_value_tags={"language": language},
)
print(rendered, file=dest)

Expand All @@ -97,13 +103,14 @@ 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}")
msg = f"Unable to find {po_file} po file for language {language}"
raise ValueError(msg)

with open(po_files["objects"], "r", encoding="utf-8") as pofile:
with open(po_files["objects"], encoding="utf-8") as pofile:
dictionary_object: Mapping[tuple[str, str | None], str] = {
(item.id, item.context): item.string for item in read_po(pofile)
}
with open(po_files["text_set"], "r", encoding="utf-8") as po_file:
with open(po_files["text_set"], encoding="utf-8") as po_file:
dictionary_textset: Mapping[str, str] = {item.id: item.string for item in read_po(po_file) if item.id}
return Dictionaries(language.lower(), dictionary_object, dictionary_textset)

Expand All @@ -116,8 +123,13 @@ def main(
destination_encoding: str,
source_encoding: str = "cp437",
) -> None:
assert template_path.exists(), "Source path doesn't exist"
assert translation_path.exists(), "Translation path doesn't exist"
if not template_path.exists():
msg = "Source path doesn't exist"
raise ValueError(msg)

if not translation_path.exists():
msg = "Translation path doesn't exist"
raise ValueError(msg)

dictionaries = get_dictionaries(translation_path, language)

Expand All @@ -132,13 +144,13 @@ def main(
for bak_file in template_path.glob("**/*.bak"):
try:
bak_file.unlink()
except Exception:
except Exception: # noqa: PERF203, BLE001
logger.error(f"Error occurred while removing {bak_file.resolve()}")

template_path.rename(template_path.parent / f"{template_path.name}_translation")
logger.warning(
"All done! Consider to change info.txt file and made unique preview.png "
"before uploading to steam or sharing the mod."
"before uploading to steam or sharing the mod.",
)


Expand Down
8 changes: 2 additions & 6 deletions df_translation_toolkit/create_mod/generate_preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import typer


# from cairosvg import svg2png


def generate_preview(template_text: str, title: str, description: str, destination_path: Path):
def generate_preview(template_text: str, title: str, description: str, destination_path: Path) -> None:
template = jinja2.Template(template_text)
result_svg = template.render(title=title, description=description)
# svg2png(bytestring=result_svg, write_to=str(destination_path))
destination_path.write_text(result_svg)


def main(template_path: Path, title: str, description: str, destination_path: Path):
def main(template_path: Path, title: str, description: str, destination_path: Path) -> None:
with template_path.open() as template_file:
template = template_file.read()
generate_preview(template, title, description, destination_path)
Expand Down
9 changes: 7 additions & 2 deletions df_translation_toolkit/create_mod/template_from_vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@ def main(
vanilla_path: Path,
destination_path: Path,
) -> None:
assert vanilla_path.exists(), "Source path doesn't exist"
assert destination_path.exists(), "Destination path doesn't exist"
if not vanilla_path.exists():
msg = "Source path doesn't exist"
raise ValueError(msg)

if not destination_path.exists():
msg = "Destination path doesn't exist"
raise ValueError(msg)

total_dirs = 0
total_files = 0
Expand Down
1 change: 0 additions & 1 deletion df_translation_toolkit/create_pot/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
import df_translation_toolkit.create_pot.cli
4 changes: 2 additions & 2 deletions df_translation_toolkit/create_pot/from_raw_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def extract_translatables_from_raws_batch(raw_files: Iterable[Path], source_enco
yield from extract_from_raw_file(file_name, source_encoding)


def create_pot_file(pot_file: BinaryIO, raw_files: Iterable[Path], source_encoding: str):
def create_pot_file(pot_file: BinaryIO, raw_files: Iterable[Path], source_encoding: str) -> None:
save_pot(
pot_file,
extract_translatables_from_raws_batch(raw_files, source_encoding),
)


def main(raws_path: Path, pot_file: typer.FileBinaryWrite, source_encoding: str = "cp437"):
def main(raws_path: Path, pot_file: typer.FileBinaryWrite, source_encoding: str = "cp437") -> None:
raw_files = (file for file in raws_path.glob("*.txt") if not file.name.startswith("language_"))
create_pot_file(pot_file, sorted(raw_files), source_encoding)

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
Loading

0 comments on commit b3145cb

Please sign in to comment.