U
    84i	                     @   sX   d Z ddlmZ ddlmZ eedddZdeeedd	d
ZdeeedddZdS )z-Utility functions for config file management.    )Path)urlparse)domainreturnc                 C   sN   |  drt| }|j} n| dd } |  dr>| dd } | dd}|S )	au  
    Normalize domain name to config file slug.
    
    Converts domain like 'themarblecoffee.com' to 'themarblecoffee-com'
    This ensures consistent config file naming across the system.
    
    Args:
        domain: Domain name (e.g., 'themarblecoffee.com' or 'https://themarblecoffee.com')
    
    Returns:
        Normalized slug (e.g., 'themarblecoffee-com')
    zhttp://zhttps:///r   www.   N.-)
startswithr   netlocsplitreplace)r   parseddomain_slug r   $scrapy_project/utils/config_utils.pynormalize_domain_to_slug   s    

r   N)r   
config_dirr   c                 C   s&   |dkrt d}t| }|| d S )z
    Get the standard config file path for a domain.
    
    Args:
        domain: Domain name
        config_dir: Config directory (default: 'configs')
    
    Returns:
        Path to config file
    Nconfigs.yaml)r   r   )r   r   r   r   r   r   get_config_path_for_domain&   s    r   c                 C   s   |dkrt d}| dr(t| }|j} | dr>| dd } ||  d || dd d ||  d	 || dd d	 g}|D ]}| r|  S qdS )
a  
    Find config file for a domain, checking both formats (with dot and with dash).
    
    Args:
        domain: Domain name
        config_dir: Config directory (default: 'configs')
    
    Returns:
        Path to config file if found, None otherwise
    Nr   r   r   r	   r   r
   r   z.yml)r   r   r   r   r   exists)r   r   r   Zpossible_configsZconfig_pathr   r   r   find_config_file_for_domain8   s     


r   )N)N)	__doc__pathlibr   Zurllib.parser   strr   r   r   r   r   r   r   <module>   s
   