Skip to content

Settings

settings

Application Settings - Configuration Management.

Provides centralized configuration management using singleton pattern to ensure consistent settings across the application.

Classes:

Name Description
Settings

Application settings manager with YAML configuration support

Classes

Settings

Settings(config_file: Optional[Path] = None)

Application settings manager with singleton pattern.

Loads configuration from YAML file and provides access to settings throughout the application. Only one instance exists per application.

Attributes:

Name Type Description
config_file Path

Path to configuration YAML file

_settings Dict[str, Any]

Loaded settings dictionary

Methods:

Name Description
get

Get setting value by dot-notation key

set

Set setting value by dot-notation key

get_section

Get entire settings section

reload

Reload settings from file

save

Save current settings to YAML file

get_all

Get all settings

Initialize settings.

Parameters:

Name Type Description Default
config_file Optional[Path]

Path to YAML configuration file. Defaults to 'config/settings.yaml'.

None
Source code in src/infrastructure/config/settings.py
def __init__(self, config_file: Optional[Path] = None):
    """
    Initialize settings.

    Parameters
    ----------
    config_file : Optional[Path], default=None
        Path to YAML configuration file.
        Defaults to 'config/settings.yaml'.
    """
    if self._initialized:
        return

    if config_file is None:
        config_file = Path("config/settings.yaml")

    self.config_file = config_file
    self._settings: Dict[str, Any] = {}
    self._load_settings()
    self._initialized = True
Functions
__new__
__new__(config_file: Optional[Path] = None)

Singleton implementation.

Parameters:

Name Type Description Default
config_file Optional[Path]

Path to config file. Only used on first instantiation.

None
Source code in src/infrastructure/config/settings.py
def __new__(cls, config_file: Optional[Path] = None):
    """
    Singleton implementation.

    Parameters
    ----------
    config_file : Optional[Path], default=None
        Path to config file. Only used on first instantiation.
    """
    if cls._instance is None:
        cls._instance = super(Settings, cls).__new__(cls)
        cls._instance._initialized = False
    return cls._instance
get
get(key: str, default: Any = None) -> Any

Get setting value by dot-notation key.

Parameters:

Name Type Description Default
key str

Setting key in dot notation (e.g., 'cache.max_size')

required
default Any

Default value if key not found

None

Returns:

Type Description
Any

Setting value or default

Source code in src/infrastructure/config/settings.py
def get(self, key: str, default: Any = None) -> Any:
    """
    Get setting value by dot-notation key.

    Parameters
    ----------
    key : str
        Setting key in dot notation (e.g., 'cache.max_size')
    default : Any, default=None
        Default value if key not found

    Returns
    -------
    Any
        Setting value or default
    """
    keys = key.split(".")
    value = self._settings

    for k in keys:
        if isinstance(value, dict) and k in value:
            value = value[k]
        else:
            return default

    return value
set
set(key: str, value: Any) -> None

Set setting value by dot-notation key.

Parameters:

Name Type Description Default
key str

Setting key in dot notation

required
value Any

Value to set

required
Source code in src/infrastructure/config/settings.py
def set(self, key: str, value: Any) -> None:
    """
    Set setting value by dot-notation key.

    Parameters
    ----------
    key : str
        Setting key in dot notation
    value : Any
        Value to set
    """
    keys = key.split(".")
    settings = self._settings

    # Navigate to nested dict
    for k in keys[:-1]:
        if k not in settings:
            settings[k] = {}
        settings = settings[k]

    # Set value
    settings[keys[-1]] = value

    logger.debug(f"Setting updated: {key} = {value}")
get_section
get_section(section: str) -> Dict[str, Any]

Get entire settings section.

Parameters:

Name Type Description Default
section str

Section name (e.g., 'cache')

required

Returns:

Type Description
Dict[str, Any]

Section dictionary or empty dict if not found

Source code in src/infrastructure/config/settings.py
def get_section(self, section: str) -> Dict[str, Any]:
    """
    Get entire settings section.

    Parameters
    ----------
    section : str
        Section name (e.g., 'cache')

    Returns
    -------
    Dict[str, Any]
        Section dictionary or empty dict if not found
    """
    return self._settings.get(section, {})
reload
reload() -> None

Reload settings from file.

Source code in src/infrastructure/config/settings.py
def reload(self) -> None:
    """Reload settings from file."""
    logger.info("Reloading settings")
    self._load_settings()
save
save(output_file: Optional[Path] = None) -> None

Save current settings to YAML file.

Parameters:

Name Type Description Default
output_file Optional[Path]

Output file path. If None, uses original config_file.

None
Source code in src/infrastructure/config/settings.py
def save(self, output_file: Optional[Path] = None) -> None:
    """
    Save current settings to YAML file.

    Parameters
    ----------
    output_file : Optional[Path], default=None
        Output file path. If None, uses original config_file.
    """
    output_file = output_file or self.config_file

    try:
        output_file.parent.mkdir(parents=True, exist_ok=True)

        with open(output_file, "w", encoding="utf-8") as f:
            yaml.dump(
                self._settings, f, default_flow_style=False, allow_unicode=True
            )

        logger.info(f"Settings saved to {output_file}")

    except Exception as e:
        logger.error(f"Failed to save settings: {e}")
        raise
get_all
get_all() -> Dict[str, Any]

Get all settings.

Returns:

Type Description
Dict[str, Any]

Complete settings dictionary.

Source code in src/infrastructure/config/settings.py
def get_all(self) -> Dict[str, Any]:
    """
    Get all settings.

    Returns
    -------
    Dict[str, Any]
        Complete settings dictionary.
    """
    return self._settings.copy()

Functions