From a7b5b171cf975bc772e3cba9446aa5c35a48f86c Mon Sep 17 00:00:00 2001 From: Adrien Date: Thu, 16 Feb 2023 18:33:43 +0100 Subject: [PATCH] Allow users to init config from Transifex, and not just CLI or envars. Disallow pos params in Transifex(). (#14) --- pytransifex/api.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pytransifex/api.py b/pytransifex/api.py index 510d9fd..75ed89e 100644 --- a/pytransifex/api.py +++ b/pytransifex/api.py @@ -335,7 +335,22 @@ class Transifex: client = None - def __new__(cls, defer_login: bool = False): + def __new__(cls, *, defer_login: bool = False, **kwargs): if not cls.client: - cls.client = Client(ApiConfig.from_env(), defer_login) + try: + if kwargs: + config = ApiConfig(**kwargs) + else: + logging.info( + f"As you called 'Transifex' without argument, we'll try defining your project from environment variables." + ) + config = ApiConfig.from_env() + + cls.client = Client(config, defer_login) + + except Exception as error: + available = list(ApiConfig._fields) + msg = f"Unable to define a proper config. API initialization uses the following fields, with only 'project_slug' optional: {available}" + logging.error(f"{msg}:\n{error}") + return cls.client