|
3 | 3 | #
|
4 | 4 |
|
5 | 5 | import logging
|
6 |
| -from distutils.util import strtobool |
7 | 6 | from enum import Flag, auto
|
8 | 7 | from typing import Any, Callable, Dict, Generator, Mapping, Optional, cast
|
9 | 8 |
|
|
22 | 21 |
|
23 | 22 | logger = logging.getLogger("airbyte")
|
24 | 23 |
|
| 24 | +_TRUTHY_STRINGS = ("y", "yes", "t", "true", "on", "1") |
| 25 | +_FALSEY_STRINGS = ("n", "no", "f", "false", "off", "0") |
| 26 | + |
| 27 | + |
| 28 | +def _strtobool(value: str, /) -> int: |
| 29 | + """Mimic the behavior of distutils.util.strtobool. |
| 30 | +
|
| 31 | + From: https://docs.python.org/2/distutils/apiref.html#distutils.util.strtobool |
| 32 | +
|
| 33 | + > Convert a string representation of truth to true (1) or false (0). |
| 34 | + > True values are y, yes, t, true, on and 1; false values are n, no, f, false, off and 0. Raises |
| 35 | + > `ValueError` if val is anything else. |
| 36 | + """ |
| 37 | + normalized_str = value.lower().strip() |
| 38 | + if normalized_str in _TRUTHY_STRINGS: |
| 39 | + return 1 |
| 40 | + |
| 41 | + if normalized_str in _FALSEY_STRINGS: |
| 42 | + return 0 |
| 43 | + |
| 44 | + raise ValueError(f"Invalid boolean value: {normalized_str}") |
| 45 | + |
25 | 46 |
|
26 | 47 | class TransformConfig(Flag):
|
27 | 48 | """
|
@@ -129,7 +150,7 @@ def default_convert(original_item: Any, subschema: Dict[str, Any]) -> Any:
|
129 | 150 | return int(original_item)
|
130 | 151 | elif target_type == "boolean":
|
131 | 152 | if isinstance(original_item, str):
|
132 |
| - return strtobool(original_item) == 1 |
| 153 | + return _strtobool(original_item) == 1 |
133 | 154 | return bool(original_item)
|
134 | 155 | elif target_type == "array":
|
135 | 156 | item_types = set(subschema.get("items", {}).get("type", set()))
|
|
0 commit comments