/
cli.py
197 lines (176 loc) · 5.4 KB
/
cli.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# -*- coding: utf-8 -*-
from gtts import gTTS, gTTSError, __version__
from gtts.lang import tts_langs
import click
import logging
import logging.config
# Click settings
CONTEXT_SETTINGS = {
'help_option_names': ['-h', '--help']
}
# Logger settings
LOGGER_SETTINGS = {
'version': 1,
'formatters': {
'default': {
'format': '%(name)s - %(levelname)s - %(message)s'
}
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'default'
}
},
'loggers': {
'gtts': {
'handlers': ['console'],
'level': 'WARNING'
}
}
}
# Logger
logging.config.dictConfig(LOGGER_SETTINGS)
log = logging.getLogger('gtts')
def sys_encoding():
"""Charset to use for --file <path>|- (stdin)"""
return 'utf8'
def validate_text(ctx, param, text):
"""Validation callback for the <text> argument.
Ensures <text> (arg) and <file> (opt) are mutually exclusive
"""
if not text and 'file' not in ctx.params:
# No <text> and no <file>
raise click.BadParameter(
"<text> or -f/--file <file> required")
if text and 'file' in ctx.params:
# Both <text> and <file>
raise click.BadParameter(
"<text> and -f/--file <file> can't be used together")
return text
def validate_lang(ctx, param, lang):
"""Validation callback for the <lang> option.
Ensures <lang> is a supported language unless the <nocheck> flag is set
"""
if ctx.params['nocheck']:
return lang
try:
if lang not in tts_langs():
raise click.UsageError(
"'%s' not in list of supported languages.\n"
"Use --all to list languages or "
"add --nocheck to disable language check." % lang)
else:
# The language is valid.
# No need to let gTTS re-validate.
ctx.params['nocheck'] = True
except RuntimeError as e:
# Only case where the <nocheck> flag can be False
# Non-fatal. gTTS will try to re-validate.
log.debug(str(e), exc_info=True)
return lang
def print_languages(ctx, param, value):
"""Callback for <all> flag.
Prints formatted sorted list of supported languages and exits
"""
if not value or ctx.resilient_parsing:
return
try:
langs = tts_langs()
langs_str_list = sorted("{}: {}".format(k, langs[k]) for k in langs)
click.echo(' ' + '\n '.join(langs_str_list))
except RuntimeError as e: # pragma: no cover
log.debug(str(e), exc_info=True)
raise click.ClickException("Couldn't fetch language list.")
ctx.exit()
def set_debug(ctx, param, debug):
"""Callback for <debug> flag.
Sets logger level to DEBUG
"""
if debug:
log.setLevel(logging.DEBUG)
return
@click.command(context_settings=CONTEXT_SETTINGS)
@click.argument('text', metavar='<text>', required=False, callback=validate_text)
@click.option(
'-f',
'--file',
metavar='<file>',
# For py2.7/unicode. If encoding not None Click uses io.open
type=click.File(encoding=sys_encoding()),
help="Read from <file> instead of <text>.")
@click.option(
'-o',
'--output',
metavar='<file>',
type=click.File(mode='wb'),
help="Write to <file> instead of stdout.")
@click.option(
'-s',
'--slow',
default=False,
is_flag=True,
help="Read more slowly.")
@click.option(
'-l',
'--lang',
metavar='<lang>',
default='en',
show_default=True,
callback=validate_lang,
help="IETF language tag. Language to speak in. List documented tags with --all.")
@click.option(
'--nocheck',
default=False,
is_flag=True,
is_eager=True, # Prioritize <nocheck> to ensure it gets set before <lang>
help="Disable strict IETF language tag checking. Allow undocumented tags.")
@click.option(
'--all',
default=False,
is_flag=True,
is_eager=True,
expose_value=False,
callback=print_languages,
help="Print all documented available IETF language tags and exit.")
@click.option(
'--debug',
default=False,
is_flag=True,
is_eager=True, # Prioritize <debug> to see debug logs of callbacks
expose_value=False,
callback=set_debug,
help="Show debug information.")
@click.version_option(version=__version__)
def tts_cli(text, file, output, slow, lang, nocheck):
""" Read <text> to mp3 format using Google Translate's Text-to-Speech API
(set <text> or --file <file> to - for standard input)
"""
# stdin for <text>
if text == '-':
text = click.get_text_stream('stdin').read()
# stdout (when no <output>)
if not output:
output = click.get_binary_stream('stdout')
# <file> input (stdin on '-' is handled by click.File)
if file:
try:
text = file.read()
except UnicodeDecodeError as e: # pragma: no cover
log.debug(str(e), exc_info=True)
raise click.FileError(
file.name,
"<file> must be encoded using '%s'." %
sys_encoding())
# TTS
try:
tts = gTTS(
text=text,
lang=lang,
slow=slow,
lang_check=not nocheck)
tts.write_to_fp(output)
except (ValueError, AssertionError) as e:
raise click.UsageError(str(e))
except gTTSError as e:
raise click.ClickException(str(e))