Chris commited on
Commit
5ef2374
·
1 Parent(s): e6758ae

CLI -lang switch fails

Browse files
Files changed (2) hide show
  1. AUTHORS.rst +1 -0
  2. deep_translator/__main__.py +19 -56
AUTHORS.rst CHANGED
@@ -12,3 +12,4 @@ Contributors
12
 
13
  @prataffel
14
  @senk8
 
 
12
 
13
  @prataffel
14
  @senk8
15
+ @nothead31
deep_translator/__main__.py CHANGED
@@ -14,66 +14,32 @@ from .papago import PapagoTranslator
14
  CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
15
 
16
  @click.command(name='Deep Translator', context_settings=CONTEXT_SETTINGS, no_args_is_help=True)
17
- @click.argument(
18
- 'translator',
19
- required=True,
20
- default='google',
21
- type=str)
22
- @click.option(
23
- "--source",
24
- "-src",
25
- required=True,
26
- type=str,
27
- help="source language to translate from")
28
- @click.option(
29
- "--target",
30
- "-tgt",
31
- required=True,
32
- type=str,
33
- help="target language to translate to")
34
- @click.option(
35
- "--text",
36
- "-txt",
37
- type=str,
38
- required = True,
39
- prompt="Enter the text you want to translate",
40
- help="text you want to translate")
41
- @click.option(
42
- "--api-key",
43
- type=str,
44
- help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators")
45
- @click.option(
46
- "--languages",
47
- "-lang",
48
- is_flag=True,
49
- help="list all the languages available with the translator."
50
- " Run with deep_translator <translator service> -lang",)
51
  def main(translator, source, target, text, api_key, languages):
52
  """
53
  Use TRANSLATOR to translate source material into another language.
54
- Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.\n
55
- \f
56
- function responsible for parsing terminal arguments and provide them for
57
- further use in the translation process
58
  """
59
  api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
60
  if translator in api_key_required and not api_key:
61
  click.echo(
62
  "This translator requires an api key provided through --api-key"
63
  )
64
- elif languages:
65
- print_supported_languages(translator, api_key)
66
  else:
67
  translate(translator, source, target, text, api_key)
68
 
69
  def translate(translator, source, target, text, api_key):
70
  """
71
- function used to provide translations from the parsed terminal arguments
72
  @param translator: translator name parsed from terminal arguments
73
- @param source: source language parsed from terminal arguments
74
- @param target: target language parsed from terminal arguments
75
- @param text: text that will be translated parsed from terminal arguments
76
- @param api_key: api key for translators that requires them
77
  @return: None
78
  """
79
  if translator == "google":
@@ -92,36 +58,33 @@ def translate(translator, source, target, text, api_key):
92
  translator = YandexTranslator(
93
  source=source,
94
  target=target,
95
- api_key=api_key
96
- )
97
  elif translator == "microsoft":
98
  translator = MicrosoftTranslator(
99
  source=source,
100
  target=target,
101
- api_key=api_key
102
- )
103
  elif translator == "papago":
104
  translator = PapagoTranslator(
105
  source=source,
106
  target=target,
107
- api_key=api_key
108
- )
109
  else:
110
  click.echo(
111
  "The given translator is not supported."
112
- " Please use a translator supported by the deep_translator tool"
113
- )
114
  return
115
 
116
  res = translator.translate(text)
117
  click.echo(f" | Translation from {source} to {target} |")
118
  click.echo(f"Translated text: \n {res}")
119
 
 
120
  def print_supported_languages(requested_translator, api_key):
121
  """
122
- function used to print the languages supported by the translator service
123
- from the parsed terminal arguments
124
- @param args: parsed terminal arguments
125
  @return: None
126
  """
127
  translator = None
 
14
  CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])
15
 
16
  @click.command(name='Deep Translator', context_settings=CONTEXT_SETTINGS, no_args_is_help=True)
17
+ @click.argument('translator', required=True, default='google', type=str)
18
+ @click.option("--source", "-src", required=True, type=str, help="source language to translate from")
19
+ @click.option("--target", "-tgt", required=True, type=str, help="target language to translate to")
20
+ @click.option("--text", "-txt", type=str,required = True,prompt="Enter the text you want to translate",help="text you want to translate")
21
+ @click.option("--api-key",type=str,help="required for DeepL, QCRI, Yandex, Microsoft and Papago translators")
22
+ # TODO: This is the option that needs to change to a subcommand.
23
+ # @click.option("--languages","-lang",is_flag=True,help="list all the languages available with the translator.\nRun with deep_translator <translator service> -lang",)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def main(translator, source, target, text, api_key, languages):
25
  """
26
  Use TRANSLATOR to translate source material into another language.
27
+ Available translators include: Google, MyMemory, QCRI, Linguee, Pons, Yandex, Microsoft (Bing), and Papago.
 
 
 
28
  """
29
  api_key_required = ["deepl", "qcri", "yandex", "microsoft", "papago"]
30
  if translator in api_key_required and not api_key:
31
  click.echo(
32
  "This translator requires an api key provided through --api-key"
33
  )
34
+ # elif languages: #TODO: This needs to be moved to its own sub-command. I have to figure out how sub-commands work in Click.
35
+ # print_supported_languages(translator, api_key)
36
  else:
37
  translate(translator, source, target, text, api_key)
38
 
39
  def translate(translator, source, target, text, api_key):
40
  """
41
+ Directory function to send arguments to the correct translator.
42
  @param translator: translator name parsed from terminal arguments
 
 
 
 
43
  @return: None
44
  """
45
  if translator == "google":
 
58
  translator = YandexTranslator(
59
  source=source,
60
  target=target,
61
+ api_key=api_key)
 
62
  elif translator == "microsoft":
63
  translator = MicrosoftTranslator(
64
  source=source,
65
  target=target,
66
+ api_key=api_key)
 
67
  elif translator == "papago":
68
  translator = PapagoTranslator(
69
  source=source,
70
  target=target,
71
+ api_key=api_key)
 
72
  else:
73
  click.echo(
74
  "The given translator is not supported."
75
+ " Please use a translator supported by the deep_translator tool")
 
76
  return
77
 
78
  res = translator.translate(text)
79
  click.echo(f" | Translation from {source} to {target} |")
80
  click.echo(f"Translated text: \n {res}")
81
 
82
+ # TODO: This is the actual function that needs to be converted to a subcommand.
83
  def print_supported_languages(requested_translator, api_key):
84
  """
85
+ Retrieve the list of available languages from the given translator.
86
+ @param requested_translator: Translator given by the user.
87
+ @param api_key: Optional API key given by the user. Required for some translators.
88
  @return: None
89
  """
90
  translator = None