o
    <iE                     @   s   d dl Z d dlZd dlZd dlmZmZmZ d dlmZ dZ	dZ
dZe
 de ZdZd	Ze eZd
ededefddZdedefddZG dd dZdS )    N)UTF8
FrozenDictRecursiveDict)	strtobool
pyiceberg_defaultcatalog-PYICEBERG_HOMEz.pyiceberg.yamllhsrhsreturnc                 C   sh   |   }| D ])\}}||v r-|| }t|tr&t|tr&t||||< q|p)|||< q|||< q|S )z.Merge right-hand side into the left-hand side.)copyitems
isinstancedictmerge_config)r   r   
new_configrhs_key	rhs_value	lhs_value r   S/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/pyiceberg/utils/config.pyr   #   s   
r   
input_dictc                 C   s   dd |   D S )z_Lowers all the keys of a dictionary in a recursive manner, to make the lookup case-insensitive.c                 S   s,   i | ]\}}|  t|trt|n|qS r   )lowerr   r   _lowercase_dictionary_keys).0kvr   r   r   
<dictcomp>8   s   , z._lowercase_dictionary_keys.<locals>.<dictcomp>)r   )r   r   r   r   r   6   s   r   c                   @   s   e Zd ZU eed< dddZededB fddZededefdd	Zde	fd
dZ
de	dedB fddZdee	 fddZde	dedB fddZde	dedB fddZdS )Configconfigr   Nc                 C   s0   |   pi }t|| |}tdi || _d S )Nr   )_from_configuration_filesr   _from_environment_variablesr   r!   )selfr!   r   r   r   __init__>   s   zConfig.__init__c                  C   sZ   dt dB dtdB fdd} tjttjdt g}|D ]}| | }r*|  S qdS )zLoad the first configuration file that its finds.

        Will first look in the PYICEBERG_HOME env variable,
        and then in the home directory.
        	directoryNr   c                 S   sn   | r5t j| t}t j|r5t|td}| }W d    n1 s$w   Y  t	|j
}t|}|S d S )N)encoding)ospathjoinPYICEBERG_YMLisfileopenr   read
strictyamlloaddatar   )r&   r)   fyml_strfile_configfile_config_lowercaser   r   r   
_load_yamlK   s   
z4Config._from_configuration_files.<locals>._load_yaml~)	strr   r(   environgetr
   r)   
expandusergetcwd)r6   search_dirsr&   r!   r   r   r   r"   C   s   z Config._from_configuration_filesc                 C   s   dt dtt dtddfdd}tj D ]+\}}| }|t r?|t	td }|j
dd	d
}dd |D }|| || q| S )a  Read the environment variables, to check if there are any prepended by PYICEBERG_.

        Args:
            config: Existing configuration that's being amended with configuration from environment variables.

        Returns:
            Amended configuration.
        _configr)   config_valuer   Nc                 S   s   t |dkr?|d}t |dkr|| |< n!|| vri | |< t| | tr*| | } ntdd| d| t |dksd S d S )Nr   z8Incompatible configurations, merging dict with a value: .z	, value: )lenpopr   r   
ValueErrorr*   )r>   r)   r?   elementr   r   r   set_propertyl   s   


z8Config._from_environment_variables.<locals>.set_property__   )maxsplitc                 S   s    g | ]}| d d ddqS )rF   r@   _r	   )replace)r   partr   r   r   
<listcomp>   s     z6Config._from_environment_variables.<locals>.<listcomp>)r   listr8   r(   r9   r   r   
startswith	PYICEBERGrA   split)r!   rE   env_varr?   env_var_lowerkeypartsparts_normalizedr   r   r   r#   a   s   z"Config._from_environment_variablesc                 C   s0   | j t }rt|tstd| |S tS )zReturn the default catalog name.

        Returns: The name of the default catalog in `default-catalog`.
                 Returns `default` when the key cannot be found in the config file.
        z&Default catalog name should be a str: )r!   r:   DEFAULT_CATALOGr   r8   rC   DEFAULT)r$   default_catalog_namer   r   r   get_default_catalog_name   s
   
zConfig.get_default_catalog_namecatalog_namec                 C   sf   t | jv r1| }| jt  }t|tstd| ||v r1|| }t|ts/td| d|S d S )Nz.Catalog configurations needs to be an object: zConfiguration path catalogs.z needs to be an object)CATALOGr!   r   r   r   rC   )r$   rZ   catalog_name_lowercatalogscatalog_confr   r   r   get_catalog_config   s   



zConfig.get_catalog_configc                 C   s,   | j ti }t|tstdt| S )Nz,Catalog configurations needs to be an object)r!   r:   r[   r   r   rC   rM   keys)r$   r]   r   r   r   get_known_catalogs   s   
zConfig.get_known_catalogsrS   c              
   C   P   | j | }d ur&zt|W S  ty% } z
t| d| |d }~ww d S )Nz4 should be an integer or left unset. Current value: )r!   r:   intrC   r$   rS   valerrr   r   r   get_int      
zConfig.get_intc              
   C   rb   )Nz3 should be a boolean or left unset. Current value: )r!   r:   r   rC   rd   r   r   r   get_bool   rh   zConfig.get_bool)r   N)__name__
__module____qualname__r   __annotations__r%   staticmethodr"   r#   r8   rY   r_   rM   ra   rc   rg   boolri   r   r   r   r   r    ;   s   
 
&r    )loggingr(   r/   pyiceberg.typedefr   r   r   pyiceberg.typesr   rO   rW   r[   rV   r
   r+   	getLoggerrj   loggerr   r   r    r   r   r   r   <module>   s   
