From 1574f629858aaef75685045809cf02eed62de063 Mon Sep 17 00:00:00 2001 From: euberdeveloper Date: Mon, 12 Jul 2021 22:43:06 +0200 Subject: [PATCH 1/2] feat(cli): add env vars layer, between cli default values and passed args Signed-off-by: euberdeveloper --- app/default_values.py | 119 ++++++++++++++++++++++++++++++++++++++++++ app/main.py | 32 ++++++------ 2 files changed, 136 insertions(+), 15 deletions(-) create mode 100644 app/default_values.py diff --git a/app/default_values.py b/app/default_values.py new file mode 100644 index 0000000..b2a2ea3 --- /dev/null +++ b/app/default_values.py @@ -0,0 +1,119 @@ +import os + +_prefix = 'LT_' + +def _get_value_str(name, default_value): + env_value = os.environ.get(name) + return default_value if env_value is None else env_value + +def _get_value_int(name, default_value): + try: + return int(os.environ[name]) + except: + return default_value + +def _get_value_bool(name, default_value): + env_value = os.environ.get(name) + if env_value in ['FALSE', 'False', 'false', '0']: + return False + if env_value in ['TRUE', 'True', 'true', '1']: + return True + return default_value + +def _get_value(name, default_value, value_type): + env_name = _prefix + name + if value_type == 'str': + return _get_value_str(env_name, default_value) + if value_type == 'int': + return _get_value_int(env_name, default_value) + if value_type == 'bool': + return _get_value_bool(env_name, default_value) + return default_value + +_default_options_objects = [ + { + 'name': 'HOST', + 'default_value': '127.0.0.1', + 'value_type': 'str' + }, + { + 'name': 'PORT', + 'default_value': 5000, + 'value_type': 'int' + }, + { + 'name': 'CHAR_LIMIT', + 'default_value': -1, + 'value_type': 'int' + }, + { + 'name': 'REQ_LIMIT', + 'default_value': -1, + 'value_type': 'int' + }, + { + 'name': 'DAILY_REQ_LIMIT', + 'default_value': -1, + 'value_type': 'int' + }, + { + 'name': 'REQ_FLOOD_THRESHOLD', + 'default_value': -1, + 'value_type': 'int' + }, + { + 'name': 'BATCH_LIMIT', + 'default_value': -1, + 'value_type': 'int' + }, + { + 'name': 'GA_ID', + 'default_value': None, + 'value_type': 'str' + }, + { + 'name': 'DEBUG', + 'default_value': False, + 'value_type': 'bool' + }, + { + 'name': 'SSL', + 'default_value': None, + 'value_type': 'bool' + }, + { + 'name': 'FRONTEND_LANGUAGE_SOURCE', + 'default_value': 'en', + 'value_type': 'str' + }, + { + 'name': 'FRONTEND_LANGUAGE_TARGET', + 'default_value': 'es', + 'value_type': 'str' + }, + { + 'name': 'FRONTEND_TIMEOUT', + 'default_value': 500, + 'value_type': 'int' + }, + { + 'name': 'API_KEYS', + 'default_value': False, + 'value_type': 'bool' + }, + { + 'name': 'REQUIRE_API_KEY_ORIGIN', + 'default_value': '', + 'value_type': 'str' + }, + { + 'name': 'LOAD_ONLY', + 'default_value': None, + 'value_type': 'str' + } +] + + +DEFAULT_ARGUMENTS = { obj['name']:_get_value(**obj) for obj in _default_options_objects} + +print(DEFAULT_ARGUMENTS) \ No newline at end of file diff --git a/app/main.py b/app/main.py index 9057946..45d9fde 100644 --- a/app/main.py +++ b/app/main.py @@ -3,6 +3,7 @@ import sys import operator from app.app import create_app +from app.default_values import DEFAULT_ARGUMENTS as DEFARGS def main(): @@ -10,40 +11,40 @@ def main(): description="LibreTranslate - Free and Open Source Translation API" ) parser.add_argument( - "--host", type=str, help="Hostname (%(default)s)", default="127.0.0.1" + "--host", type=str, help="Hostname (%(default)s)", default=DEFARGS['HOST'] ) - parser.add_argument("--port", type=int, help="Port (%(default)s)", default=5000) + parser.add_argument("--port", type=int, help="Port (%(default)s)", default=DEFARGS['PORT']) parser.add_argument( "--char-limit", - default=-1, + default=DEFARGS['CHAR_LIMIT'], type=int, metavar="", help="Set character limit (%(default)s)", ) parser.add_argument( "--req-limit", - default=-1, + default=DEFARGS['REQ_LIMIT'], type=int, metavar="", help="Set the default maximum number of requests per minute per client (%(default)s)", ) parser.add_argument( "--daily-req-limit", - default=-1, + default=DEFARGS['DAILY_REQ_LIMI'], type=int, metavar="", help="Set the default maximum number of requests per day per client, in addition to req-limit. (%(default)s)", ) parser.add_argument( "--req-flood-threshold", - default=-1, + default=DEFARGS['REQ_FLOOD_THRESHOLD'], type=int, metavar="", help="Set the maximum number of request limit offences per 4 weeks that a client can exceed before being banned. (%(default)s)", ) parser.add_argument( "--batch-limit", - default=-1, + default=DEFARGS['BATCH_LIMIT'], type=int, metavar="", help="Set maximum number of texts to translate in a batch request (%(default)s)", @@ -51,52 +52,53 @@ def main(): parser.add_argument( "--ga-id", type=str, - default=None, + default=DEFARGS['GA_ID'], metavar="", help="Enable Google Analytics on the API client page by providing an ID (%(default)s)", ) parser.add_argument( - "--debug", default=False, action="store_true", help="Enable debug environment" + "--debug", default=DEFARGS['DEBUG'], action="store_true", help="Enable debug environment" ) parser.add_argument( - "--ssl", default=None, action="store_true", help="Whether to enable SSL" + "--ssl", default=DEFARGS['SSL'], action="store_true", help="Whether to enable SSL" ) parser.add_argument( "--frontend-language-source", type=str, - default="en", + default=DEFARGS['FRONTEND_LANGUAGE_SOURCE'], metavar="", help="Set frontend default language - source (%(default)s)", ) parser.add_argument( "--frontend-language-target", type=str, - default="es", + default=DEFARGS['FRONTEND_LANGUAGE_TARGET'], metavar="", help="Set frontend default language - target (%(default)s)", ) parser.add_argument( "--frontend-timeout", type=int, - default=500, + default=DEFARGS['FRONTEND_TIMEOUT'], metavar="", help="Set frontend translation timeout (%(default)s)", ) parser.add_argument( "--api-keys", - default=False, + default=DEFARGS['API_KEYS'], action="store_true", help="Enable API keys database for per-user rate limits lookup", ) parser.add_argument( "--require-api-key-origin", type=str, - default="", + default=DEFARGS['REQUIRE_API_KEY_ORIGIN'], help="Require use of an API key for programmatic access to the API, unless the request origin matches this domain", ) parser.add_argument( "--load-only", type=operator.methodcaller("split", ","), + default=DEFARGS['LOAD_ONLY'], metavar="", help="Set available languages (ar,de,en,es,fr,ga,hi,it,ja,ko,pt,ru,zh)", ) From b56245ca149f5cd4e4d625cae4e7e91260204659 Mon Sep 17 00:00:00 2001 From: euberdeveloper <33126163+euberdeveloper@users.noreply.github.com> Date: Mon, 12 Jul 2021 23:41:03 +0200 Subject: [PATCH 2/2] fix: fix typo --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index 45d9fde..d9b5b44 100644 --- a/app/main.py +++ b/app/main.py @@ -30,7 +30,7 @@ def main(): ) parser.add_argument( "--daily-req-limit", - default=DEFARGS['DAILY_REQ_LIMI'], + default=DEFARGS['DAILY_REQ_LIMIT'], type=int, metavar="", help="Set the default maximum number of requests per day per client, in addition to req-limit. (%(default)s)",