"""Utility functions for config file management.""" from pathlib import Path from urllib.parse import urlparse def normalize_domain_to_slug(domain: str) -> str: """ 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') """ # Remove protocol if present if domain.startswith(('http://', 'https://')): parsed = urlparse(domain) domain = parsed.netloc else: # Remove path if present domain = domain.split('/')[0] # Remove www. prefix if domain.startswith('www.'): domain = domain[4:] # Replace dots with dashes domain_slug = domain.replace('.', '-') return domain_slug def get_config_path_for_domain(domain: str, config_dir: Path = None) -> Path: """ Get the standard config file path for a domain. Args: domain: Domain name config_dir: Config directory (default: 'configs') Returns: Path to config file """ if config_dir is None: config_dir = Path('configs') domain_slug = normalize_domain_to_slug(domain) return config_dir / f"{domain_slug}.yaml" def find_config_file_for_domain(domain: str, config_dir: Path = None) -> Path: """ 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 """ if config_dir is None: config_dir = Path('configs') # Normalize domain if domain.startswith(('http://', 'https://')): parsed = urlparse(domain) domain = parsed.netloc if domain.startswith('www.'): domain = domain[4:] # Try both formats possible_configs = [ config_dir / f"{domain}.yaml", # Exact domain (with dot) config_dir / f"{domain.replace('.', '-')}.yaml", # Domain slug (with dash) config_dir / f"{domain}.yml", config_dir / f"{domain.replace('.', '-')}.yml", ] for config_path in possible_configs: if config_path.exists(): return config_path return None