Skip to content

Commit

Permalink
Fix environment variable parsing (apache#423)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
Fokko and hussein-awala authored Feb 14, 2024
1 parent 4e2127f commit cc44926
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pyiceberg/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ def set_property(_config: RecursiveDict, path: List[str], config_value: str) ->
env_var_lower = env_var.lower()
if env_var_lower.startswith(PYICEBERG.lower()):
key = env_var_lower[len(PYICEBERG) :]
parts = key.split("__")
parts_normalized = [part.replace("_", "-") for part in parts]
parts = key.split("__", maxsplit=2)
parts_normalized = [part.replace('__', '.').replace("_", "-") for part in parts]
set_property(config, parts_normalized, config_value)

return config
Expand Down
14 changes: 14 additions & 0 deletions tests/utils/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ def test_from_environment_variables_uppercase() -> None:
assert Config().get_catalog_config("PRODUCTION") == {"uri": "https://service.io/api"}


@mock.patch.dict(
os.environ,
{
"PYICEBERG_CATALOG__PRODUCTION__S3__REGION": "eu-north-1",
"PYICEBERG_CATALOG__PRODUCTION__S3__ACCESS_KEY_ID": "username",
},
)
def test_fix_nested_objects_from_environment_variables() -> None:
assert Config().get_catalog_config("PRODUCTION") == {
's3.region': 'eu-north-1',
's3.access-key-id': 'username',
}


def test_from_configuration_files(tmp_path_factory: pytest.TempPathFactory) -> None:
config_path = str(tmp_path_factory.mktemp("config"))
with open(f"{config_path}/.pyiceberg.yaml", "w", encoding=UTF8) as file:
Expand Down

0 comments on commit cc44926

Please sign in to comment.