Skip to content

Commit 93931e4

Browse files
authored
feat: support listing known catalogs (#2088)
<!-- Thanks for opening a pull request! --> <!-- In the case this PR will resolve an issue, please replace ${GITHUB_ISSUE_ID} below with the actual Github issue id. --> <!-- Closes #${GITHUB_ISSUE_ID} --> Adds a new function `pyiceberg.catalog.list_catalogs() -> List[str]` to list all known catalogs. # Rationale for this change In creating a pyiceberg-backed API, one must either duplicate the config-parsing logic or access private data in order to know what catalogs are available in the env. I believe that this logic should be part of the library. # Are these changes tested? Yes # Are there any user-facing changes? Yes, knew public function in `pyiceberg.catalog` <!-- In the case of user-facing changes, please add the changelog label. -->
1 parent 49bf3b5 commit 93931e4

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

pyiceberg/catalog/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ def load_catalog(name: Optional[str] = None, **properties: Optional[str]) -> Cat
262262
raise ValueError(f"Could not initialize catalog with the following properties: {properties}")
263263

264264

265+
def list_catalogs() -> List[str]:
266+
return _ENV_CONFIG.get_known_catalogs()
267+
268+
265269
def delete_files(io: FileIO, files_to_delete: Set[str], file_type: str) -> None:
266270
"""Delete files.
267271

pyiceberg/utils/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ def get_catalog_config(self, catalog_name: str) -> Optional[RecursiveDict]:
159159
return catalog_conf
160160
return None
161161

162+
def get_known_catalogs(self) -> List[str]:
163+
catalogs = self.config.get(CATALOG, {})
164+
if not isinstance(catalogs, dict):
165+
raise ValueError("Catalog configurations needs to be an object")
166+
return list(catalogs.keys())
167+
162168
def get_int(self, key: str) -> Optional[int]:
163169
if (val := self.config.get(key)) is not None:
164170
try:

tests/utils/test_config.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ def test_fix_nested_objects_from_environment_variables() -> None:
5656
}
5757

5858

59+
@mock.patch.dict(os.environ, EXAMPLE_ENV)
60+
@mock.patch.dict(os.environ, {"PYICEBERG_CATALOG__DEVELOPMENT__URI": "https://dev.service.io/api"})
61+
def test_list_all_known_catalogs() -> None:
62+
assert Config().get_known_catalogs() == [
63+
"production",
64+
"development",
65+
]
66+
67+
5968
def test_from_configuration_files(tmp_path_factory: pytest.TempPathFactory) -> None:
6069
config_path = str(tmp_path_factory.mktemp("config"))
6170
with open(f"{config_path}/.pyiceberg.yaml", "w", encoding=UTF8) as file:

0 commit comments

Comments
 (0)