You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by bl...@apache.org on 2023/02/16 22:00:43 UTC

[iceberg] branch master updated: Python: Add `default-catalog` option to the config (#6864)

This is an automated email from the ASF dual-hosted git repository.

blue pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new aa3cc32c98 Python: Add `default-catalog` option to the config (#6864)
aa3cc32c98 is described below

commit aa3cc32c98d83ae0acf65997c74e3b6eb7ac5019
Author: Fokko Driesprong <fo...@apache.org>
AuthorDate: Thu Feb 16 23:00:37 2023 +0100

    Python: Add `default-catalog` option to the config (#6864)
    
    If you set `default-catalog` it will default to that catalog.
---
 python/pyiceberg/catalog/__init__.py |  6 +++++-
 python/pyiceberg/cli/console.py      |  4 ++--
 python/pyiceberg/utils/config.py     | 16 ++++++++++++++++
 3 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/python/pyiceberg/catalog/__init__.py b/python/pyiceberg/catalog/__init__.py
index c908a3b462..041018f127 100644
--- a/python/pyiceberg/catalog/__init__.py
+++ b/python/pyiceberg/catalog/__init__.py
@@ -146,7 +146,7 @@ def infer_catalog_type(name: str, catalog_properties: RecursiveDict) -> Optional
     )
 
 
-def load_catalog(name: str, **properties: Optional[str]) -> Catalog:
+def load_catalog(name: Optional[str], **properties: Optional[str]) -> Catalog:
     """Load the catalog based on the properties
 
     Will look up the properties from the config, based on the name
@@ -162,6 +162,10 @@ def load_catalog(name: str, **properties: Optional[str]) -> Catalog:
         ValueError: Raises a ValueError in case properties are missing or malformed,
             or if it could not determine the catalog based on the properties
     """
+
+    if name is None:
+        name = _ENV_CONFIG.get_default_catalog_name()
+
     env = _ENV_CONFIG.get_catalog_config(name)
     conf: RecursiveDict = merge_config(env or {}, cast(RecursiveDict, properties))
 
diff --git a/python/pyiceberg/cli/console.py b/python/pyiceberg/cli/console.py
index e2e9dbe1fc..7ece1b1bbe 100644
--- a/python/pyiceberg/cli/console.py
+++ b/python/pyiceberg/cli/console.py
@@ -50,13 +50,13 @@ def catch_exception() -> Callable:  # type: ignore
 
 
 @click.group()
-@click.option("--catalog", default="default")
+@click.option("--catalog")
 @click.option("--verbose", type=click.BOOL)
 @click.option("--output", type=click.Choice(["text", "json"]), default="text")
 @click.option("--uri")
 @click.option("--credential")
 @click.pass_context
-def run(ctx: Context, catalog: str, verbose: bool, output: str, uri: Optional[str], credential: Optional[str]) -> None:
+def run(ctx: Context, catalog: Optional[str], verbose: bool, output: str, uri: Optional[str], credential: Optional[str]) -> None:
     properties = {}
     if uri:
         properties["uri"] = uri
diff --git a/python/pyiceberg/utils/config.py b/python/pyiceberg/utils/config.py
index 6055f75df9..1af3e13aa0 100644
--- a/python/pyiceberg/utils/config.py
+++ b/python/pyiceberg/utils/config.py
@@ -23,7 +23,9 @@ import yaml
 from pyiceberg.typedef import FrozenDict, RecursiveDict
 
 PYICEBERG = "pyiceberg_"
+DEFAULT = "default"
 CATALOG = "catalog"
+DEFAULT_CATALOG = f"{DEFAULT}-{CATALOG}"
 HOME = "HOME"
 PYICEBERG_HOME = "PYICEBERG_HOME"
 PYICEBERG_YML = ".pyiceberg.yaml"
@@ -129,6 +131,20 @@ class Config:
 
         return config
 
+    def get_default_catalog_name(self) -> str:
+        """
+        Looks into the configuration file for `default-catalog`
+        and returns the name as the default catalog
+
+        Returns: The name of the default catalog in `default-catalog`
+                 Returns `default` when the key cannot be found.
+        """
+        if default_catalog_name := self.config.get(DEFAULT_CATALOG):
+            if not isinstance(default_catalog_name, str):
+                raise ValueError(f"Default catalog name should be a str: {default_catalog_name}")
+            return default_catalog_name
+        return DEFAULT
+
     def get_catalog_config(self, catalog_name: str) -> Optional[RecursiveDict]:
         if CATALOG in self.config:
             catalog_name_lower = catalog_name.lower()