From b2d23de7b676fb81096ce20c52e327f2a6a2ced4 Mon Sep 17 00:00:00 2001 From: Ilona Date: Wed, 8 Jan 2025 21:35:44 +0100 Subject: [PATCH] Fix config for bool values. --- config.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index f8457c0..a97fe11 100644 --- a/config.py +++ b/config.py @@ -6,12 +6,42 @@ BASEDIR = os.path.abspath(os.path.dirname(__file__)) load_dotenv(os.path.join(BASEDIR, ".env")) +BOOL_STR = { + 0: False, + 1: True, + "": False, + "0": False, + "1": True, + "f": False, + "false": False, + "o": True, + "on": True, + "n": False, + "t": True, + "true": True, + True: True, + "v": True, + "vrai": True, + "y": True, +} + +def to_bool(x) -> bool: + """Cast value to boolean. + The value may be encoded as a string + False are: empty, "0", "False", "f", "n". + True: all other values, such as "1", "True", "foo", "bar"... + Case insentive, ignore leading and trailing spaces. + """ + if isinstance(x, str): + return BOOL_STR.get(x.lower().strip(), True) + return bool(x) + class Config: """General configuration. Mostly loaded from environment via .env""" FLASK_ENV = None # # set in subclass SECRET_KEY = os.environ.get("SECRET_KEY") or "90e01e751977d4276a4c70d98564b425e" - LOG_TO_STDOUT = os.environ.get("LOG_TO_STDOUT") + # LOG_TO_STDOUT = os.environ.get("LOG_TO_STDOUT") LOG_LEVEL = getattr(logging, os.environ.get("LOG_LEVEL", "INFO"), "INFO") MAIL_SERVER = os.environ.get("MAIL_SERVER", "localhost") LANGUAGES = ["fr", "en"] # unused for now @@ -37,7 +67,7 @@ class Config: SCODOC_URL = os.environ.get("SCODOC_URL", "http://localhost:5000") SCODOC_LOGIN = os.environ.get("SCODOC_LOGIN", "autosco") SCODOC_PASSWORD = os.environ.get("SCODOC_PASSWORD") - SCODOC_CHECK_CERTIFICATE = os.environ.get("SCODOC_CHECK_CERTIFICATE", True) + SCODOC_CHECK_CERTIFICATE = to_bool(os.environ.get("SCODOC_CHECK_CERTIFICATE", True)) API_TIMEOUT = 120 # 2 minutes API_URL = SCODOC_URL + "/ScoDoc/api" SCODOC_DEPT_ACRONYM = os.environ.get("SCODOC_DEPT_ACRONYM", "ESPL")