Skip to content

Commit cc44926

Browse files
Fokkohussein-awala
andauthored
Fix environment variable parsing (#423)
* Fix environment variable parsing ```bash export PYICEBERG_CATALOG__SOMETHING__S3__REGION=eu-north-1 ``` Before: ```python >>> from pyiceberg.catalog import load_catalog >>> load_catalog('something').properties {'s3': {'region': 'eu-north-1'}, ...} ``` After: ```python >>> from pyiceberg.catalog import load_catalog >>> load_catalog('something').properties {'s3.region': 'eu-north-1', ...} ``` Which correspondents with the key `s3.region` that we use. * Add second test Co-authored-by: Hussein Awala <[email protected]> * lint --------- Co-authored-by: Hussein Awala <[email protected]>
1 parent 4e2127f commit cc44926

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

pyiceberg/utils/config.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ def set_property(_config: RecursiveDict, path: List[str], config_value: str) ->
125125
env_var_lower = env_var.lower()
126126
if env_var_lower.startswith(PYICEBERG.lower()):
127127
key = env_var_lower[len(PYICEBERG) :]
128-
parts = key.split("__")
129-
parts_normalized = [part.replace("_", "-") for part in parts]
128+
parts = key.split("__", maxsplit=2)
129+
parts_normalized = [part.replace('__', '.').replace("_", "-") for part in parts]
130130
set_property(config, parts_normalized, config_value)
131131

132132
return config

tests/utils/test_config.py

+14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ def test_from_environment_variables_uppercase() -> None:
4141
assert Config().get_catalog_config("PRODUCTION") == {"uri": "https://service.io/api"}
4242

4343

44+
@mock.patch.dict(
45+
os.environ,
46+
{
47+
"PYICEBERG_CATALOG__PRODUCTION__S3__REGION": "eu-north-1",
48+
"PYICEBERG_CATALOG__PRODUCTION__S3__ACCESS_KEY_ID": "username",
49+
},
50+
)
51+
def test_fix_nested_objects_from_environment_variables() -> None:
52+
assert Config().get_catalog_config("PRODUCTION") == {
53+
's3.region': 'eu-north-1',
54+
's3.access-key-id': 'username',
55+
}
56+
57+
4458
def test_from_configuration_files(tmp_path_factory: pytest.TempPathFactory) -> None:
4559
config_path = str(tmp_path_factory.mktemp("config"))
4660
with open(f"{config_path}/.pyiceberg.yaml", "w", encoding=UTF8) as file:

0 commit comments

Comments
 (0)