o
    ViZ                     @   sV   d Z ddlZddlZddlmZmZmZ G dd dZdad
dee	 defdd	Z
dS )z+
Configuration handling for Perth library.
    N)DictAnyOptionalc                	   @   s   e Zd ZdZdddddddd	dd
dZd dee fddZdeddfddZdeddfddZ	d dedede
de
fddZdedede
ddfddZdedeee
f fddZdefddZdS )!Configz
    Configuration management for Perth.
    
    Handles loading, saving, and accessing configuration settings for the library.
    perthT)default_watermarkerverbosecpuimplicitN)devicerun_name
models_diriD  )default_sample_rate	normalize)generalr   audioconfig_pathc              	   C   s   i | _ | j D ]\}}| | j |< qtjtjtjtjt	ddd| j d d< |r=tj
|r?| | dS dS dS )z
        Initialize configuration with default values and optional user config.
        
        Args:
            config_path: Path to a YAML configuration file to load
        r   	perth_net
pretrainedr   N)_config	_defaultsitemscopyospathjoindirnameabspath__file__existsload)selfr   sectionvalues r$   @/home/ubuntu/.local/lib/python3.10/site-packages/perth/config.py__init__&   s   zConfig.__init__returnc              
   C   s   z>t |d}t|}W d   n1 sw   Y  |r9| D ]\}}|| jv r3| j| | q!|| j|< q!W dS W dS  ty[ } ztd| d|  W Y d}~dS d}~ww )z
        Load configuration from a YAML file.
        
        Args:
            config_path: Path to a YAML configuration file
        rNz$Warning: Could not load config from z: )openyaml	safe_loadr   r   update	Exceptionprint)r!   r   fuser_configr"   r#   er$   r$   r%   r    <   s   
"zConfig.loadc                 C   s`   t jt jt j|dd t|d}tj| j|dd W d   dS 1 s)w   Y  dS )z
        Save current configuration to a YAML file.
        
        Args:
            config_path: Path to save the configuration to
        T)exist_okwFdefault_flow_styleN)	r   makedirsr   r   r   r)   r*   dumpr   )r!   r   r/   r$   r$   r%   saveQ   s   "zConfig.saver"   keydefaultc                 C   s*   || j v r|| j | v r| j | | S |S )a  
        Get a configuration value.
        
        Args:
            section: Configuration section
            key: Configuration key
            default: Default value to return if key is not found
            
        Returns:
            Configuration value or default
        r   )r!   r"   r9   r:   r$   r$   r%   get\   s   z
Config.getvaluec                 C   s&   || j vr
i | j |< || j | |< dS )z
        Set a configuration value.
        
        Args:
            section: Configuration section
            key: Configuration key
            value: Value to set
        Nr;   )r!   r"   r9   r=   r$   r$   r%   setl   s   
	
z
Config.setc                 C   s   | j |i  S )z
        Get an entire configuration section.
        
        Args:
            section: Configuration section name
            
        Returns:
            Dictionary of configuration values for the section
        )r   r<   r   )r!   r"   r$   r$   r%   get_sectiony   s   
zConfig.get_sectionc                 C   s   t j| jddS )z4Return a string representation of the configuration.Fr4   )r*   r7   r   )r!   r$   r$   r%   __str__   s   zConfig.__str__N)__name__
__module____qualname____doc__r   r   strr&   r    r8   r   r<   r>   r   r?   r@   r$   r$   r$   r%   r   	   s(    
r   r   r'   c                 C   s&   t du r
t| a t S | rt |  t S )z
    Get the global configuration instance.
    
    Args:
        config_path: Optional path to a configuration file to load
        
    Returns:
        Config instance
    N)_config_instancer   r    )r   r$   r$   r%   
get_config   s   
rH   rA   )rE   r   r*   typingr   r   r   r   rG   rF   rH   r$   r$   r$   r%   <module>   s     