~kf5jwc/garretts-plugin-google

3f2266bd881fe96e3d89fd4665056d60e3f2ae08 — Kyle Jones 2 years ago 5a16959
Add an exception catcher to produce normalized errors
1 files changed, 25 insertions(+), 1 deletions(-)

M text_synthesizer_plugin_google/__init__.py
M text_synthesizer_plugin_google/__init__.py => text_synthesizer_plugin_google/__init__.py +25 -1
@@ 2,67 2,91 @@

__version__ = "0.1.0"

from text_synthesizer import Plugin as __plugin
from text_synthesizer import Plugin as __plugin, PluginError
from .google import synthesize_text as __synthesize
from typing import BinaryIO as _BinaryIO

from google.auth.exceptions import DefaultCredentialsError as _DefaultCredentialsError


def __exception_catcher(fn):
    def func(*args, **kwargs):
        try:
            fn(*args, **kwargs)
        except _DefaultCredentialsError:
            raise PluginError("Default credentials were not found!")

    return func

# Standard
@__plugin.register(name="Google Standard A")
@__exception_catcher
def standard_a(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-A")


@__plugin.register(name="Google Standard B")
@__exception_catcher
def standard_b(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-B")


@__plugin.register(name="Google Standard C")
@__exception_catcher
def standard_c(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-C")


@__plugin.register(name="Google Standard D")
@__exception_catcher
def standard_d(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-D")


@__plugin.register(name="Google Standard E")
@__exception_catcher
def standard_e(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-E")


@__plugin.register(name="Google Standard F")
@__exception_catcher
def standard_f(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Standard-F")


# Wavenet
@__plugin.register(name="Google Wavenet A")
@__exception_catcher
def wavenet_a(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-A")


@__plugin.register(name="Google Wavenet B")
@__exception_catcher
def wavenet_b(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-B")


@__plugin.register(name="Google Wavenet C")
@__exception_catcher
def wavenet_c(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-C")


@__plugin.register(name="Google Wavenet D")
@__exception_catcher
def wavenet_d(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-D")


@__plugin.register(name="Google Wavenet E")
@__exception_catcher
def wavenet_e(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-E")


@__plugin.register(name="Google Wavenet F")
@__exception_catcher
def wavenet_f(text: str) -> _BinaryIO:
    return __synthesize(text, name="en-US-Wavenet-F")