Fokko commented on code in PR #5488:
URL: https://github.com/apache/iceberg/pull/5488#discussion_r947579279


##########
python/pyiceberg/utils/config.py:
##########
@@ -0,0 +1,130 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+import logging
+import os
+from typing import Any, List, Optional
+
+import yaml
+
+from pyiceberg.typedef import EMPTY_DICT, FrozenDict, RecursiveDict
+
+PYICEBERG = "pyiceberg__"
+CATALOG = "catalog"
+HOME = "HOME"
+PYICEBERG_HOME = "PYICEBERG_HOME"
+PYICEBERG_YML = ".pyiceberg.yml"
+
+
+def _coalesce(lhs: Optional[Any], rhs: Optional[Any]) -> Optional[Any]:
+    return lhs or rhs
+
+
+def _merge_config(lhs: RecursiveDict, rhs: RecursiveDict) -> RecursiveDict:
+    """merges right-hand side into the left-hand side"""
+    new_config = lhs.copy()
+    for rhs_key, rhs_value in rhs.items():
+        if rhs_key in new_config:
+            lhs_value = new_config[rhs_key]
+            if isinstance(lhs_value, dict) and isinstance(rhs_value, dict):
+                # If they are both dicts, then we have to go deeper
+                new_config[rhs_key] = _merge_config(lhs_value, rhs_value)
+            else:
+                # Take the non-null value, with precedence on rhs
+                new_config[rhs_key] = _coalesce(rhs_value, lhs_value)
+        else:
+            # New key
+            new_config[rhs_key] = rhs_value
+
+    return new_config
+
+
+logger = logging.getLogger(__name__)
+
+
+def _lowercase_dictionary_keys(input_dict: RecursiveDict) -> RecursiveDict:
+    """Lowers all the keys of a dictionary in a recursive manner, to make the 
lookup case-insensitive"""
+    return {k.lower(): _lowercase_dictionary_keys(v) if isinstance(v, dict) 
else v for k, v in input_dict.items()}
+
+
+class Config:
+    config: RecursiveDict
+
+    def __init__(self):
+        config = self._from_configuration_files()
+        config = _merge_config(config, 
self._from_environment_variables(config))
+        self.config = FrozenDict(**config)
+
+    @staticmethod
+    def _from_configuration_files() -> RecursiveDict:
+        """Looks up configuration files
+
+        Takes preference over RecursiveDict that's close to the user
+        """
+        config: RecursiveDict = {}
+        for directory in [os.environ.get(HOME), 
os.environ.get(PYICEBERG_HOME)]:

Review Comment:
   I've updated this to raise the exception if there is a yaml error and 
removed the loop:
   ```python
   @staticmethod
   def _from_configuration_files() -> Optional[RecursiveDict]:
       """Loads the first configuration file that its finds
   
       Will first look in the PYICEBERG_HOME env variable,
       and then in the home directory.
       """
       def _load_yaml(directory: Optional[str]) -> Optional[RecursiveDict]:
           if directory:
               path = f"{directory.rstrip('/')}/{PYICEBERG_YML}"
               if os.path.isfile(path):
                   with open(path, encoding="utf-8") as f:
                       file_config = yaml.safe_load(f)
                       file_config_lowercase = 
_lowercase_dictionary_keys(file_config)
                       return file_config_lowercase
       # Give priority to the PYICEBERG_HOME directory
       if pyiceberg_home_config := _load_yaml(os.environ.get(PYICEBERG_HOME)):
           return pyiceberg_home_config
       # Look into the home directory
       if pyiceberg_home_config := _load_yaml(os.environ.get(HOME)):
           return pyiceberg_home_config
       # Didn't find a config
       return None
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to