diff --git a/exasol/connections.py b/exasol/connections.py index 329ae6d..cbb7c4a 100644 --- a/exasol/connections.py +++ b/exasol/connections.py @@ -10,22 +10,11 @@ import exasol.bucketfs as bfs # type: ignore from exasol.secret_store import Secrets +from exasol.utils import optional_str_to_bool -def _optional_str_to_bool(value: Optional[str]) -> Optional[bool]: - if value is None: - return None - value_l = value.lower() - if value_l in ["y", "yes", "true"]: - return True - elif value_l in ["n", "no", "false"]: - return False - else: - raise ValueError("Invalid boolean value " + value) - - -def _optional_encryption(conf: Secrets) -> Optional[bool]: - return _optional_str_to_bool(conf.get("ENCRYPTION")) +def _optional_encryption(conf: Secrets, key: str = "ENCRYPTION") -> Optional[bool]: + return optional_str_to_bool(conf.get(key)) def _extract_ssl_options(conf: Secrets) -> dict: @@ -37,7 +26,7 @@ def _extract_ssl_options(conf: Secrets) -> dict: sslopt: dict[str, object] = {} # Is server certificate validation required? - certificate_validation = _optional_str_to_bool(conf.get("CERTIFICATE_VALIDATION")) + certificate_validation = optional_str_to_bool(conf.get("CERTIFICATE_VALIDATION")) if certificate_validation is not None: sslopt["cert_reqs"] = ( ssl.CERT_REQUIRED if certificate_validation else ssl.CERT_NONE diff --git a/exasol/utils.py b/exasol/utils.py index 38e1661..d59814e 100644 --- a/exasol/utils.py +++ b/exasol/utils.py @@ -1,4 +1,5 @@ from pathlib import Path +from typing import Optional def upward_file_search(file_name: str) -> str: @@ -17,3 +18,22 @@ def upward_file_search(file_name: str) -> str: return str(maybe_file) dir = dir.parent raise ValueError(f"Cannot find {file_name}") + + +def optional_str_to_bool(value: Optional[str]) -> Optional[bool]: + """ + Converts an optional string value to an optional boolean. + None, '' => None. + Case-insensitive "y", "yes", "true" => True + Case-insensitive "n", "no", "false" => False + Any other value cause a ValueError exception. + """ + if value is None: + return None + value_l = value.lower() + if value_l in ["y", "yes", "true"]: + return True + elif value_l in ["n", "no", "false"]: + return False + else: + raise ValueError("Invalid boolean value " + value) diff --git a/test/unit/test_utils.py b/test/unit/test_utils.py index 87df249..5bbf270 100644 --- a/test/unit/test_utils.py +++ b/test/unit/test_utils.py @@ -3,7 +3,7 @@ import pytest -from exasol.utils import upward_file_search +from exasol.utils import upward_file_search, optional_str_to_bool def test_upward_file_search(): @@ -32,3 +32,22 @@ def test_upward_file_search_failure(): upward_file_search(file_to_search) finally: os.chdir(current_dir) + + +@pytest.mark.parametrize("v, expected", [ + (None, None), + ('y', True), + ('yes', True), + ('true', True), + ('Y', True), + ('YES', True), + ('TRUE', True), + ('n', False), + ('no', False), + ('false', False), + ('N', False), + ('NO', False), + ('FALSE', False) +]) +def test_optional_str_to_bool(v, expected): + assert optional_str_to_bool(v) == expected