Skip to content

Commit

Permalink
Add mineos_translate CLI command
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolCat467 committed Feb 12, 2024
1 parent b4d0e8e commit dffae87
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Files created by program
cache/
Upload/
mineos_cache/
mineos_upload/

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ version = {attr = "localization_translation.__init__.__version__"}
[tool.setuptools.package-data]
localization_translation = ["py.typed"]

[project.scripts]
mineos_translate = "localization_translation.cli:main"

[tool.mypy]
mypy_path = "src"
check_untyped_defs = true
Expand Down
2 changes: 1 addition & 1 deletion src/localization_translation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

__title__ = "Localization Translation Utility"
__author__ = "CoolCat467"
__version__ = "0.0.0"
__version__ = "2.3.0"
__license__ = "GNU General Public License Version 3"


Expand Down
189 changes: 189 additions & 0 deletions src/localization_translation/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
"""CLI - Command Line Interface."""

# Programmed by CoolCat467

from __future__ import annotations

# CLI - Command Line Interface
# Copyright (C) 2024 CoolCat467
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

__title__ = "CLI - Command Line Interface"
__author__ = "CoolCat467"
__license__ = "GNU General Public License Version 3"


import argparse
import logging
from typing import TYPE_CHECKING

import httpx
import trio

from localization_translation.mineos_auto_trans import (
__version__,
translate_broken_values,
translate_lolcat,
translate_main,
translate_new_value,
)

if TYPE_CHECKING:
from collections.abc import Sequence


class ListAction(argparse.Action):
"""List supported badges action."""

__slots__ = ()

def __init__(
self,
option_strings: Sequence[str],
dest: str = argparse.SUPPRESS,
default: str = argparse.SUPPRESS,
help: str = "list supported badges and exit", # noqa: A002 # `help` is shadowing a Python builtin
) -> None:
"""Initialize this action."""
super().__init__(
option_strings=option_strings,
dest=dest,
default=default,
nargs=0,
help=help,
)

def __call__(
self,
parser: argparse.ArgumentParser,
_namespace: argparse.Namespace,
_values: str | Sequence[object] | None,
_option_string: str | None = None,
) -> None:
"""Print badges and exit."""
parser.exit()


class DumpAction(argparse.Action):
"""Dump badge data action."""

__slots__ = ()

def __init__(
self,
option_strings: Sequence[str],
dest: str = argparse.SUPPRESS,
default: str = argparse.SUPPRESS,
help: str = "dump badge data", # noqa: A002 # `help` is shadowing a Python builtin
) -> None:
"""Initialize this action."""
super().__init__(
option_strings=option_strings,
dest=dest,
default=default,
nargs=0,
help=help,
)

def __call__(
self,
parser: argparse.ArgumentParser,
_namespace: argparse.Namespace,
_values: str | Sequence[object] | None,
_option_string: str | None = None,
) -> None:
"""Print json dump of badges and exit."""
parser.exit()


async def async_run(
parser: argparse.ArgumentParser,
args: argparse.Namespace,
) -> None:
"""Async entry point."""
async with httpx.AsyncClient(http2=True) as client:
if args.unhandled:
print("Translating Unhandled Values...")
await translate_main(client)
elif args.broken:
print("Translating Broken Values...")
await translate_broken_values(client)
elif args.lolcat:
print("Translating Lolcat...")
await translate_lolcat(client)
elif args.filename and args.key:
print(f"Translating new key {args.key!r} in file {args.filename!r}...")
await translate_new_value(client, args.key, args.filename)
parser.exit(0)


def main() -> None:
"""Handle command line interface."""
parser = argparse.ArgumentParser()
parser.add_argument(
"-V",
"--version",
action="version",
version=f"%(prog)s {__version__}",
)
group = parser.add_mutually_exclusive_group()

group.add_argument(
"-u",
"--unhandled",
action="store_true",
help="Translate unhandled languages",
)
group.add_argument(
"-b",
"--broken",
action="store_true",
help="Translate broken values",
)
group.add_argument(
"-l",
"--lolcat",
action="store_true",
help="Translate to lolcat",
)
new_key_group = parser.add_argument_group()
new_key_group.add_argument(
"-f",
"--filename",
dest="filename",
)
new_key_group.add_argument(
"-k",
"--key",
dest="key",
)

args = parser.parse_args()

logging.basicConfig(level=logging.WARNING)

is_new_key = args.filename and args.key
if not (args.unhandled or args.broken or args.lolcat or is_new_key):
parser.print_help()
parser.exit(1)
if is_new_key and (args.unhandled or args.broken or args.lolcat):
parser.print_help()
parser.exit(1, "Translate new key and other translate type is mutually exclusive.")
trio.run(async_run, parser, args, strict_exception_groups=True)


if __name__ == "__main__":
print(f"{__title__} v{__version__}\nProgrammed by {__author__}.\n")
main()
12 changes: 8 additions & 4 deletions src/localization_translation/lolcat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from __future__ import annotations

__title__ = "Lolcat Scraper"
Expand All @@ -28,10 +29,11 @@
from html.parser import HTMLParser
from typing import TYPE_CHECKING, Any

import extricate
import mechanicalsoup
import trio

from localization_translation import extricate

if TYPE_CHECKING:
from collections.abc import Sequence

Expand Down Expand Up @@ -84,7 +86,7 @@ def handle_endtag(self, tag: str) -> None:

def translate_sentence(sentence: str) -> str:
"""Translate sentence."""
browser = mechanicalsoup.StatefulBrowser()
browser = mechanicalsoup.StatefulBrowser(soup_config={"features": "html.parser"})
browser.open("https://funtranslations.com/lolcat")
browser.select_form("form#textform")
browser["text"] = sentence
Expand Down Expand Up @@ -124,9 +126,11 @@ def translate_block(sentences: list[str], threshold: int = 2048) -> list[str]:
block += to_translate.pop() + sep
if not block:
break
block = block[: -len(sep)]
block = block.removesuffix(sep)
response = translate_sentence(block)
result += response.split(sep)
result.extend(response.split(sep))
## result.append(translate_sentence(to_translate.pop()))
assert len(result) == len(sentences), f"{len(result)} != {len(sentences)}"
return result


Expand Down
31 changes: 14 additions & 17 deletions src/localization_translation/mineos_auto_trans.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@
import os
from typing import TYPE_CHECKING, Any

import convert
import extricate
import httpx
import languages
import lolcat
import translate
import trio

from localization_translation import convert, extricate, languages, lolcat, translate

if TYPE_CHECKING:
from collections.abc import Awaitable, Callable

Expand Down Expand Up @@ -310,9 +307,9 @@ async def abstract_translate(

async def translate_main(client: httpx.AsyncClient) -> None:
"""Translate with google translate."""
here_folder = os.path.split(__file__)[0]
base_lang = os.path.join(here_folder, "Upload")
cache_folder = os.path.join(here_folder, "cache")
here_folder = os.getcwd()
base_lang = os.path.join(here_folder, "mineos_upload")
cache_folder = os.path.join(here_folder, "mineos_cache")

def get_unhandled(handled: set, folder: str) -> set[str]:
# return [v for k, v in languages.LANGCODES.items() if not k in handled]
Expand All @@ -328,9 +325,9 @@ async def trans_coro(english: dict, to_lang: str, folder: str) -> dict:

async def translate_new_value(client: httpx.AsyncClient, key: str, folder: str) -> None:
"""Translate with google translate."""
here_folder = os.path.split(__file__)[0]
base_lang = os.path.join(here_folder, "Upload")
cache_folder = os.path.join(here_folder, "cache")
here_folder = os.getcwd()
base_lang = os.path.join(here_folder, "mineos_upload")
cache_folder = os.path.join(here_folder, "mineos_cache")

def get_unhandled(handled: set, lang_folder: str) -> set[str]:
if lang_folder == folder:
Expand Down Expand Up @@ -372,9 +369,9 @@ def fix_translation(original: str, value: str) -> str:

async def translate_broken_values(client: httpx.AsyncClient) -> None:
"""Translate with google translate."""
here_folder = os.path.split(__file__)[0]
base_lang = os.path.join(here_folder, "Upload")
cache_folder = os.path.join(here_folder, "cache")
here_folder = os.getcwd()
base_lang = os.path.join(here_folder, "mineos_upload")
cache_folder = os.path.join(here_folder, "mineos_cache")

def get_unhandled(handled: set[str], lang_folder: str) -> set[str]:
if "Localizations" not in lang_folder:
Expand Down Expand Up @@ -425,9 +422,9 @@ async def translate_lolcat(client: httpx.AsyncClient) -> None:
"""Translate with website."""
# https://funtranslations.com/lolcat

here_folder = os.path.split(__file__)[0]
base_lang = os.path.join(here_folder, "Upload")
cache_folder = os.path.join(here_folder, "cache")
here_folder = os.getcwd()
base_lang = os.path.join(here_folder, "mineos_upload")
cache_folder = os.path.join(here_folder, "mineos_cache")

def get_unhandled(handled: set[str], folder: str) -> set[str]:
return {"lolcat"} # if not 'lolcat' in handled else set()
Expand Down
3 changes: 2 additions & 1 deletion src/localization_translation/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@
from typing import TYPE_CHECKING, Any, Final, TypeVar, assert_never
from urllib.parse import urlencode

import agents
import httpx
import trio

from localization_translation import agents

if TYPE_CHECKING:
from collections.abc import Awaitable, Sequence

Expand Down

0 comments on commit dffae87

Please sign in to comment.