add parser for environment variables WIP

This commit is contained in:
Julius Unverfehrt 2022-09-12 11:38:01 +02:00
parent 101b71726c
commit c60e8cd678
4 changed files with 35 additions and 2 deletions

View File

@ -18,6 +18,7 @@ filters:
image_to_page_quotient: # Image size to page size ratio (ratio of geometric means of areas)
min: $MIN_REL_IMAGE_SIZE|0.05 # Minimum permissible
max: $MAX_REL_IMAGE_SIZE|0.75 # Maximum permissible
# Fixme: Temporary solution, delete
customized: # Customized settings per class (RED-5202)
max:
signature: $MAX_REL_SIGNATURE_SIZE|0.4

View File

@ -32,3 +32,7 @@ class IntentionalTestException(RuntimeError):
class InvalidBox(Exception):
pass
class ParsingError(Exception):
pass

View File

@ -1,9 +1,14 @@
import json
import math
import os
from functools import lru_cache
from operator import itemgetter
from image_prediction.config import CONFIG
from image_prediction.exceptions import ParsingError
from image_prediction.transformer.transformer import Transformer
from image_prediction.utils import get_logger
from funcy import filter, juxt, first, rest
logger = get_logger()
@ -83,3 +88,26 @@ def __is_max_image_to_page_quotient_breached(quotient: float, label: str) -> boo
max_quotient = customized_entries.get(label, default_max_quotient)
max_quotient = max_quotient if max_quotient else default_max_quotient
return bool(quotient > max_quotient)
@lru_cache(maxsize=None)
def parse_env_var(prefix, fallback_value):
head, tail = juxt(first, rest)(filter(prefix, os.environ))
if not head or tail:
logger.warning(
f"Found multiple candidates for environment variable with prefix '{prefix}', falling back to default value."
)
return fallback_value
else:
try:
return parse_env_var_value(os.environ[head])
except ParsingError as err:
logger.warning(f"{err}, falling back to default value.")
return fallback_value
def parse_env_var_value(env_var_value):
try:
return json.loads(env_var_value)
except Exception as err:
raise ParsingError(f"Failed to parse {env_var_value}") from err

View File

@ -3,7 +3,7 @@ import tempfile
import pytest
import yaml
from image_prediction.config import Config, DotIndexable
from image_prediction.config import Config
@pytest.fixture
@ -40,7 +40,7 @@ def test_access_key_does_not_exists(config):
def test_get_method_returns_key_if_key_does_exist(config):
dot_indexable = config.D.E
assert dot_indexable.get("F", "default_value") == True
assert dot_indexable.get("F", "default_value") is True
def test_get_method_returns_default_if_key_does_not_exist(config):