o
    uyiy                     @   s   d dl Z d dlZd dlmZm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)ListOptional)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   Y/home/ubuntu/maya3_transcribe/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>9   s   , z._lowercase_dictionary_keys.<locals>.<dictcomp>)r   )r   r   r   r   r   7   s   r   c                   @   s   e Zd ZU eed< dddZedee fddZededefdd	Z	de
fd
dZde
dee fddZdee
 fddZde
dee fddZde
dee 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 t dt t 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.
        	directoryr   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_yamlL   s   
z4Config._from_configuration_files.<locals>._load_yaml~N)
r   strr   r*   environgetr   r+   
expandusergetcwd)r8   search_dirsr(   r#   r   r   r   r$   D   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+   rA   elementr   r   r   set_propertym   s   


z8Config._from_environment_variables.<locals>.set_property__   )maxsplitc                 S   s    g | ]}| d d ddqS )rH   rB   _r   )replace)r   partr   r   r   
<listcomp>   s     z6Config._from_environment_variables.<locals>.<listcomp>)r   r   r:   r*   r;   r   r   
startswith	PYICEBERGrC   split)r#   rG   env_varrA   env_var_lowerkeypartsparts_normalizedr   r   r   r%   b   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   r:   rE   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   rE   )r&   r[   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   rE   listkeys)r&   r^   r   r   r   get_known_catalogs   s   
zConfig.get_known_catalogsrT   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<   intrE   r&   rT   valerrr   r   r   get_int      
zConfig.get_intc              
   C   rd   )Nz3 should be a boolean or left unset. Current value: )r#   r<   r   rE   rf   r   r   r   get_bool   rj   zConfig.get_bool)r   N)__name__
__module____qualname__r   __annotations__r'   staticmethodr   r$   r%   r:   rZ   r`   r   rc   re   ri   boolrk   r   r   r   r   r"   <   s   
 
&r"   )loggingr*   typingr   r   r1   pyiceberg.typedefr   r   r   pyiceberg.typesr   rP   rX   r\   rW   r   r-   	getLoggerrl   loggerr   r   r"   r   r   r   r   <module>   s    
