Skip to content

Commit

Permalink
Moved optional_str_to_bool to utils.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsimb committed Jan 8, 2024
1 parent 423e6b6 commit fe612f7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
19 changes: 4 additions & 15 deletions exasol/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions exasol/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from pathlib import Path
from typing import Optional


def upward_file_search(file_name: str) -> str:
Expand All @@ -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)
21 changes: 20 additions & 1 deletion test/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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

0 comments on commit fe612f7

Please sign in to comment.