Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GoogleTLService and some other requirements excluding easytl.py needs #5

Merged
merged 9 commits into from
Apr 25, 2024
Prev Previous commit
Next Next commit
added some of the service wide values and got the redefine client stu…
…ff up, as well as the credential setter
  • Loading branch information
Bikatr7 committed Apr 25, 2024
commit 834d547a4dd37d06ca25ffb7fb093b4ea755e7f6
52 changes: 52 additions & 0 deletions src/easytl/googletl_service.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@
## Use of this source code is governed by an GNU Lesser General Public License v2.1
## license that can be found in the LICENSE file.

## built-in libraries
import asyncio
import typing

## third-party libraries
from google.cloud import translate_v2 as translate
from google.cloud.translate_v2 import Client
@@ -22,21 +26,69 @@ class GoogleTLService:

_format:str = 'text'

_semaphore_value:int = 15
_semaphore:asyncio.Semaphore = asyncio.Semaphore(_semaphore_value)

_rate_limit_delay:float | None = None

_decorator_to_use:typing.Union[typing.Callable, None] = None

_log_directory:str | None = None


##-------------------start-of-_set_credentials()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@staticmethod
def _set_credentials(key_path:str):

"""

Set the credentials for the Google Translate API.

Parameters:
key_path (str): The path to the JSON key file.

"""

GoogleTLService._credentials = service_account.Credentials.from_service_account_file(key_path)


##-------------------start-of-_redefine_client()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@staticmethod
def _redefine_client():

"""

Redefine the Google Translate client with the new credentials.

"""

GoogleTLService._translator = translate.Client(credentials=GoogleTLService._credentials)

##-------------------start-of-_redefine_client_decorator()---------------------------------------------------------------------------------------------------------------------------------------------------------------------------

@staticmethod
def _redefine_client_decorator(func):

"""

Wraps a function to redefine the GoogleTL client before doing anything that requires the client.

Parameters:
func (callable) : The function to wrap.

Returns:
wrapper (callable) : The wrapped function.

"""

def wrapper(*args, **kwargs):
GoogleTLService._redefine_client()
return func(*args, **kwargs)

return wrapper