-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy path__main__.py
90 lines (76 loc) · 2.61 KB
/
__main__.py
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
# Copyright 2023 DeepL SE (https://www.deepl.com)
# Use of this source code is governed by an MIT
# license that can be found in the LICENSE file.
import argparse
import deepl
import os
from translate_json import translate_json
env_auth_key = "DEEPL_AUTH_KEY"
env_server_url = "DEEPL_SERVER_URL"
def get_parser(prog_name):
"""Constructs and returns the argument parser."""
parser = argparse.ArgumentParser(
prog=prog_name,
description="Translate strings in JSON using the DeepL API "
"(https://www.deepl.com/docs-api).",
epilog="If you encounter issues while using this example, please "
"report them at https://github.com/DeepLcom/deepl-python/issues",
)
parser.add_argument(
"--auth-key",
default=None,
help="authentication key as given in your DeepL account; the "
f"{env_auth_key} environment variable is used as secondary fallback",
)
parser.add_argument(
"--server-url",
default=None,
metavar="URL",
help=f"alternative server URL for testing; the {env_server_url} "
f"environment variable may be used as secondary fallback",
)
parser.add_argument(
"--to",
"--target-lang",
dest="target_lang",
required=True,
help="language into which the JSON strings should be translated",
)
parser.add_argument(
"--from",
"--source-lang",
dest="source_lang",
help="language of the JSON strings to be translated",
)
parser.add_argument(
"json",
nargs="+",
type=str,
help="JSON to be translated. Wrap JSON in quotes to prevent "
"the shell from splitting on whitespace.",
)
return parser
def main():
# Create a parser, reusing most of the arguments from the main CLI
parser = get_parser(prog_name=None)
args = parser.parse_args()
auth_key = args.auth_key or os.getenv(env_auth_key)
server_url = args.server_url or os.getenv(env_server_url)
if auth_key is None:
raise Exception(
f"Please provide authentication key via the {env_auth_key} "
"environment variable or --auth_key argument"
)
# Create a Translator object, and call get_usage() to validate connection
translator = deepl.Translator(auth_key, server_url=server_url)
translator.get_usage()
for json in args.json:
output = translate_json(
json,
translator=translator,
source_lang=args.source_lang,
target_lang=args.target_lang,
)
print(output)
if __name__ == "__main__":
main()