aboutsummaryrefslogtreecommitdiffstats
path: root/python/fatcat_web
diff options
context:
space:
mode:
authorBryan Newbold <bnewbold@robocracy.org>2021-11-03 16:25:40 -0700
committerBryan Newbold <bnewbold@robocracy.org>2021-11-03 16:52:44 -0700
commit37a1c20c5832166e03b8f10f22fde26b56fe4a13 (patch)
tree9ee85114a221133c8b44844d1fe420be5e0e3e4e /python/fatcat_web
parent91bd35a1ee0993126c369e39fbf8f81f775840ee (diff)
downloadfatcat-37a1c20c5832166e03b8f10f22fde26b56fe4a13.tar.gz
fatcat-37a1c20c5832166e03b8f10f22fde26b56fe4a13.zip
web config: add helper for coercing env vars into booleans
Diffstat (limited to 'python/fatcat_web')
-rw-r--r--python/fatcat_web/web_config.py35
1 files changed, 32 insertions, 3 deletions
diff --git a/python/fatcat_web/web_config.py b/python/fatcat_web/web_config.py
index 229c2761..82525487 100644
--- a/python/fatcat_web/web_config.py
+++ b/python/fatcat_web/web_config.py
@@ -10,12 +10,41 @@ import scripts.
import os
import subprocess
+from typing import Union
import raven
basedir = os.path.abspath(os.path.dirname(__file__))
+def bool_str(raw: Union[str, bool, None]) -> bool:
+ """
+ Helper for parsing environment variables
+ """
+ if not raw:
+ return False
+ if raw is True:
+ return True
+
+ if isinstance(raw, str):
+ raw_str = raw.strip()
+ if raw_str.lower() in ["0", "f", "false", "no", "n"]:
+ return False
+ if raw_str.lower() in ["1", "t", "true", "yes", "y"]:
+ return True
+ raise ValueError("Unparsable boolean value: {raw}")
+
+
+def test_bool_str() -> None:
+ assert bool_str(True) is True
+ assert bool_str(None) is False
+ assert bool_str(False) is False
+ assert bool_str("") is False
+ assert bool_str("0") is False
+ assert bool_str("True") is True
+ assert bool_str("FALSE") is False
+
+
class Config(object):
GIT_REVISION = (
subprocess.check_output(["git", "describe", "--tags", "--long", "--always"])
@@ -74,7 +103,7 @@ class Config(object):
IA_XAUTH_CLIENT_SECRET = os.environ.get("IA_XAUTH_CLIENT_SECRET", default=None)
# analytics; used in production
- ENABLE_GOATCOUNTER = bool(os.environ.get("ENABLE_GOATCOUNTER", default=False))
+ ENABLE_GOATCOUNTER = bool_str(os.environ.get("ENABLE_GOATCOUNTER", default=False))
GOATCOUNTER_ENDPOINT = os.environ.get(
"GOATCOUNTER_ENDPOINT", default="https://goatcounter.fatcat.wiki/count"
)
@@ -83,8 +112,8 @@ class Config(object):
)
# controls granularity of "shadow_only" preservation category
- FATCAT_MERGE_SHADOW_PRESERVATION = os.environ.get(
- "FATCAT_MERGE_SHADOW_PRESERVATION", default=False
+ FATCAT_MERGE_SHADOW_PRESERVATION = bool_str(
+ os.environ.get("FATCAT_MERGE_SHADOW_PRESERVATION", default=False)
)
# CSRF on by default, but only for WTF forms (not, eg, search, lookups, GET