make env var parser discrete

This commit is contained in:
Julius Unverfehrt 2022-09-12 15:14:19 +02:00
parent c1b9227035
commit 23c61ef49e

View File

@ -4,7 +4,7 @@ import os
from functools import lru_cache
from operator import itemgetter
from funcy import filter, juxt, first, rest, compose
from funcy import juxt, first, rest, compose
from image_prediction.config import CONFIG
from image_prediction.exceptions import ParsingError
@ -131,11 +131,9 @@ def build_image_info(data: dict) -> dict:
@lru_cache(maxsize=None)
def parse_env_var(prefix, table=None):
table = table or os.environ
head, tail = juxt(first, compose(list, rest))(filter(prefix, table))
head = first(filter(lambda s: s == prefix, table))
if not head:
logger.warning(f"Found no environment variable with prefix '{prefix}'.")
elif tail:
logger.warning(f"Found multiple candidates for environment variable with prefix '{prefix}'.")
else:
try:
return parse_env_var_value(table[head])