Skip to content

app_config

Configuration management for Music Genre Updater.

Config

Config(config_path=None)

Configuration manager for the application.

Handles config file discovery (env var, defaults) and delegates YAML loading + Pydantic validation to core.core_config.load_config.

Initialize configuration.

Parameters:

Name Type Description Default
config_path str | None

Path to the configuration file (use default if None)

None
Source code in src/app/app_config.py
def __init__(self, config_path: str | None = None) -> None:
    """Initialize configuration.

    Args:
        config_path: Path to the configuration file (use default if None)

    """
    if config_path is None:
        # Load .env file if not already loaded
        if load_dotenv is not None:
            load_dotenv()

        # Try environment variable first, then try each default config file
        config_path = os.getenv("CONFIG_PATH")
    if config_path is None:
        for default_file in DEFAULT_CONFIG_FILES:
            if Path(default_file).exists():
                config_path = default_file
                break
        else:
            # Fail fast - no config file found
            msg = (
                f"No configuration file found. Checked CONFIG_PATH env var and files: {DEFAULT_CONFIG_FILES}. "
                "Please create a config.yaml file or set CONFIG_PATH environment variable."
            )
            raise FileNotFoundError(msg)

    # config_path is guaranteed to be not None at this point
    self.config_path = config_path
    self._resolved_path: str | None = None
    self._app_config: AppConfig | None = None

resolved_path property

resolved_path

Get the resolved absolute path to the configuration file.

load

load()

Load configuration from the file.

Returns:

Type Description
AppConfig

Validated AppConfig Pydantic model.

Source code in src/app/app_config.py
def load(self) -> AppConfig:
    """Load configuration from the file.

    Returns:
        Validated AppConfig Pydantic model.

    """
    if self._app_config is None:
        load_path = Path(os.path.expandvars(self.config_path)).expanduser()
        try:
            app_config = load_yaml_config(str(load_path))
        except (OSError, ValueError, yaml.YAMLError, RuntimeError) as e:
            msg = f"Failed to load configuration from '{load_path}': {e}"
            raise RuntimeError(msg) from e
        self._app_config = app_config
        self._resolved_path = self._resolve_config_path()

    return self._app_config