~kf5jwc/garretts-plugin-google

0e076230f8866b1b6f2d98f03759371988df3d18 — Kyle Jones 2 years ago c9c743f
Handle errors for credentials files

Moving it out of __init__
M text_synthesizer_plugin_google/__init__.py => text_synthesizer_plugin_google/__init__.py +4 -11
@@ 2,26 2,19 @@

__version__ = "0.1.0"

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

from google.auth.exceptions import DefaultCredentialsError as _DefaultCredentialsError

from text_synthesizer import Plugin as __plugin, Type as __Type

def __exception_catcher(fn):
    def func(*args, **kwargs):
        try:
            fn(*args, **kwargs)
        except _DefaultCredentialsError:
            raise PluginError("Default credentials were not found!")
from .google import synthesize_text as __synthesize
from .exception_handler import exception_catcher as __exception_catcher


# Load the credentials file into the env where TTS expects
_credentials_file = "GOOGLE_APPLICATION_CREDENTIALS"
__environ[_credentials_file] = __Type.plugin_file(_credentials_file)

    return func

# Standard
@__plugin.register(name="Google Standard A")

A text_synthesizer_plugin_google/exception_handler.py => text_synthesizer_plugin_google/exception_handler.py +15 -0
@@ 0,0 1,15 @@
from google.auth.exceptions import DefaultCredentialsError
from text_synthesizer import PluginFileMissing, PluginError

from . import _credentials_file

def exception_catcher(fn):
    def func(*args, **kwargs):
        try:
            fn(*args, **kwargs)
        except DefaultCredentialsError:
            raise PluginError("Valid credentials for Google services were not found!")
            raise PluginFileMissing(_credentials_file, "Please select your credentials file. You can find out how to acquire this file from https://developers.google.com/accounts/docs/application-default-credentials.")

    return func