Source code for exasol.nb_connector.utils

from pathlib import Path
from typing import Optional






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 not value:
        return None

    mapping = {
        "y": True,
        "yes": True,
        "true": True,
        "n": False,
        "no": False,
        "false": False,
    }
    key = value.lower()

    try:
        result = mapping[key]
    except KeyError as ex:
        raise ValueError("Invalid boolean value " + value) from ex

    return result