Skip to content

papi.core.settings

get_config(config_file_path=None)

Load and cache the application configuration from a YAML file.

Parameters:

Name Type Description Default
config_file_path Optional[str]

Path to the configuration file. If not provided, uses cached path or defaults to 'config.yaml'.

None

Returns:

Name Type Description
AppConfig AppConfig

The loaded configuration object.

Raises:

Type Description
FileNotFoundError

If the specified config file does not exist.

YAMLError

If the file cannot be parsed as valid YAML.

Exception

If the data cannot be converted into an AppConfig.

Source code in papi/core/settings.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def get_config(config_file_path: Optional[str] = None) -> AppConfig:
    """
    Load and cache the application configuration from a YAML file.

    Args:
        config_file_path (Optional[str]): Path to the configuration file.
            If not provided, uses cached path or defaults to 'config.yaml'.

    Returns:
        AppConfig: The loaded configuration object.

    Raises:
        FileNotFoundError: If the specified config file does not exist.
        yaml.YAMLError: If the file cannot be parsed as valid YAML.
        Exception: If the data cannot be converted into an AppConfig.
    """
    global _config_cache, _config_file_path

    logger.debug("=== get_config() called ===")
    logger.debug(f"Received config_file_path: {config_file_path}")
    logger.debug(f"Current cached _config_file_path: {_config_file_path}")
    logger.debug(f"Config cache status: {'filled' if _config_cache else 'empty'}")

    if _config_cache is not None and config_file_path is None:
        logger.debug("Returning cached configuration object.")
        return _config_cache

    # Determine the effective configuration file path
    if config_file_path:
        requested_path = Path(config_file_path).resolve()
        _config_file_path = str(requested_path)
    elif _config_file_path:
        requested_path = Path(_config_file_path).resolve()
    else:
        requested_path = Path("config.yaml").resolve()
        _config_file_path = str(requested_path)

    logger.info(f"Loading configuration file from: {requested_path}")

    if not requested_path.is_file():
        logger.error(f"Configuration file not found: {requested_path}")
        raise FileNotFoundError(f"Configuration file not found: {requested_path}")

    try:
        with requested_path.open("r", encoding="utf-8") as f:
            data = yaml.safe_load(f)
        _config_cache = AppConfig(**data)
        logger.debug("Configuration file successfully parsed and cached.")
    except yaml.YAMLError:
        logger.exception("Failed to parse YAML configuration file.")
        raise
    except Exception:
        logger.exception("Failed to load configuration into AppConfig.")
        raise

    return _config_cache