diff --git a/config.yaml b/config.yaml index 23c9268..e2fd18b 100644 --- a/config.yaml +++ b/config.yaml @@ -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 diff --git a/image_prediction/exceptions.py b/image_prediction/exceptions.py index 1b88f0d..f03b42a 100644 --- a/image_prediction/exceptions.py +++ b/image_prediction/exceptions.py @@ -32,3 +32,7 @@ class IntentionalTestException(RuntimeError): class InvalidBox(Exception): pass + + +class ParsingError(Exception): + pass diff --git a/image_prediction/transformer/transformers/response.py b/image_prediction/transformer/transformers/response.py index 5a37670..dc647f8 100644 --- a/image_prediction/transformer/transformers/response.py +++ b/image_prediction/transformer/transformers/response.py @@ -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 diff --git a/test/unit_tests/config_test.py b/test/unit_tests/config_test.py index e2a2c5e..878d7e1 100644 --- a/test/unit_tests/config_test.py +++ b/test/unit_tests/config_test.py @@ -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):