File size: 3,253 Bytes
d3c12a3 fa90b1d c228ada 7ade3be d3c12a3 7a05cd7 d3c12a3 5c85230 37d3379 d3c12a3 7ade3be c228ada cb15d3a 37d3379 78d0ff1 37d3379 7ade3be d3c12a3 37d3379 78d0ff1 d03a2fc 78d0ff1 d3c12a3 7ade3be d3c12a3 cb15d3a d3c12a3 37d3379 cb15d3a 78d0ff1 d3c12a3 7ade3be c228ada cb15d3a c228ada 78d0ff1 d3c12a3 78d0ff1 c228ada d3c12a3 7ade3be c228ada |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
"""Console script for deep_translator."""
__copyright__ = "Copyright (C) 2020 Nidhal Baccouri"
import argparse
from typing import Optional
from deep_translator.engines import __engines__
class CLI(object):
translators_dict = __engines__
translator = None
def __init__(self, custom_args: Optional[list] = None):
self.custom_args = custom_args
self.args = self.parse_args()
translator_class = self.translators_dict.get(
self.args.translator, None
)
if not translator_class:
raise Exception(
f"Translator {self.args.translator} is not supported."
f"Supported translators: {list(self.translators_dict.keys())}"
)
self.translator = translator_class(
source=self.args.source, target=self.args.target
)
def translate(self) -> None:
"""
function used to provide translations from the parsed terminal arguments
@return: None
"""
res = self.translator.translate(self.args.text)
print(f"Translation from {self.args.source} to {self.args.target}")
print("-" * 50)
print(f"Translation result: {res}")
def get_supported_languages(self) -> None:
"""
function used to return the languages supported by the translator service
from the parsed terminal arguments
@return: None
"""
translator_supported_languages = (
self.translator.get_supported_languages(as_dict=True)
)
print(f"Languages supported by '{self.args.translator}' are :\n")
print(translator_supported_languages)
def parse_args(self) -> argparse.Namespace:
"""
function responsible for parsing terminal arguments and provide
them for further use in the translation process
"""
parser = argparse.ArgumentParser(
add_help=True,
description="Official CLI for deep-translator",
usage="dt --help",
)
parser.add_argument(
"--translator",
"-trans",
default="google",
type=str,
help="name of the translator you want to use",
)
parser.add_argument(
"--source",
"-src",
default="auto",
type=str,
help="source language to translate from",
)
parser.add_argument(
"--target", "-tg", type=str, help="target language to translate to"
)
parser.add_argument(
"--text", "-txt", type=str, help="text you want to translate"
)
parser.add_argument(
"--languages",
"-lang",
action="store_true",
help="all the languages available with the translator"
"Run the command deep_translator -trans <translator service> -lang",
)
parsed_args = (
parser.parse_args(self.custom_args)
if self.custom_args
else parser.parse_args()
)
return parsed_args
def run(self) -> None:
if self.args.languages:
self.get_supported_languages()
else:
self.translate()
|