Skip to content

kamihi.base.config ⚓︎

Configuration module.

This module contains the configuration settings for the Kamihi framework. The configuration settings are loaded from environment variables and/or a .env file. They must begin with the prefix KAMIHI_.

License

MIT

Classes:

Name Description
DatabaseSettings

Defines the database settings schema.

KamihiSettings

Defines the configuration schema for the Kamihi framework.

LogLevel

Enum for log levels.

LogSettings

Defines the logging configuration schema.

ResponseSettings

Defines the response settings schema.

WebSettings

Defines the web settings schema.

DatabaseSettings ⚓︎

Bases: BaseModel

Defines the database settings schema.

Attributes:

Name Type Description
host str

The URL of the database.

name str

The name of the database.

KamihiSettings ⚓︎

Bases: BaseSettings

Defines the configuration schema for the Kamihi framework.

Attributes:

Name Type Description
timezone str

The timezone for the application.

autoreload_templates bool

Whether to enable template auto-reloading.

log LogSettings

The logging settings.

db DatabaseSettings

The database settings.

token str | None

The Telegram bot token.

responses ResponseSettings

The response settings.

web WebSettings

The web settings.

Methods:

Name Description
from_yaml

Load settings from a custom YAML file.

settings_customise_sources

Customize the order of settings sources.

timezone_obj property ⚓︎

timezone_obj: DstTzInfo

Get the timezone object.

Returns:

Name Type Description
DstTzInfo DstTzInfo

The timezone object.

from_yaml classmethod ⚓︎

from_yaml(path: Path) -> KamihiSettings

Load settings from a custom YAML file.

Parameters:

Name Type Description Default

path ⚓︎

Path

The path to the YAML file.

required

Returns:

Name Type Description
KamihiSettings KamihiSettings

An instance of KamihiSettings with the loaded settings.

Source code in src/kamihi/base/config.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
@classmethod
def from_yaml(cls, path: Path) -> "KamihiSettings":
    """
    Load settings from a custom YAML file.

    Args:
        path (Path): The path to the YAML file.

    Returns:
        KamihiSettings: An instance of KamihiSettings with the loaded settings.

    """
    if path.exists() and path.is_file():
        with path.open("r", encoding="utf-8") as f:
            data = yaml.safe_load(f)
        if data and isinstance(data, dict):
            return cls(**data)
    return cls()

settings_customise_sources classmethod ⚓︎

settings_customise_sources(
    settings_cls: type[BaseSettings],
    init_settings: PydanticBaseSettingsSource,
    env_settings: PydanticBaseSettingsSource,
    dotenv_settings: PydanticBaseSettingsSource,
    file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]

Customize the order of settings sources.

This method allows you to customize the order in which settings sources are loaded. The order of sources is important because it determines which settings take precedence when there are conflicts. The order of sources is as follows: 1. Environment variables 2. .env file 3. YAML file 4. Initial settings

Parameters:

Name Type Description Default

settings_cls ⚓︎

type[BaseSettings]

the settings class to customize sources for

required

init_settings ⚓︎

PydanticBaseSettingsSource

settings from class initialization

required

env_settings ⚓︎

PydanticBaseSettingsSource

settings from environment variables

required

dotenv_settings ⚓︎

PydanticBaseSettingsSource

settings from .env file

required

file_secret_settings ⚓︎

PydanticBaseSettingsSource

settings from file secrets

required

Returns:

Name Type Description
tuple tuple[PydanticBaseSettingsSource, ...]

A tuple containing the customized settings sources in the desired order.

Source code in src/kamihi/base/config.py
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
@classmethod
def settings_customise_sources(
    cls,
    settings_cls: type[BaseSettings],
    init_settings: PydanticBaseSettingsSource,
    env_settings: PydanticBaseSettingsSource,
    dotenv_settings: PydanticBaseSettingsSource,
    file_secret_settings: PydanticBaseSettingsSource,
) -> tuple[PydanticBaseSettingsSource, ...]:
    """
    Customize the order of settings sources.

    This method allows you to customize the order in which settings sources are
    loaded. The order of sources is important because it determines which settings
    take precedence when there are conflicts.
    The order of sources is as follows:
        1. Environment variables
        2. .env file
        3. YAML file
        4. Initial settings

    Args:
        settings_cls: the settings class to customize sources for
        init_settings: settings from class initialization
        env_settings: settings from environment variables
        dotenv_settings: settings from .env file
        file_secret_settings: settings from file secrets

    Returns:
        tuple: A tuple containing the customized settings sources in the desired order.

    """
    return (
        init_settings,
        env_settings,
        dotenv_settings,
        YamlConfigSettingsSource(
            settings_cls, yaml_file=[os.getenv("KAMIHI_CONFIG_FILE", "kamihi.yaml"), "kamihi.yaml", "kamihi.yml"]
        ),
        file_secret_settings,
    )

LogLevel ⚓︎

Bases: StrEnum

Enum for log levels.

This enum defines the log levels used in the logging configuration.

Attributes:

Name Type Description
TRACE

Trace level logging.

DEBUG

Debug level logging.

INFO

Info level logging.

SUCCESS

Success level logging.

WARNING

Warning level logging.

ERROR

Error level logging.

CRITICAL

Critical level logging.

LogSettings ⚓︎

Bases: BaseModel

Defines the logging configuration schema.

Attributes:

Name Type Description
stdout_enable bool

Enable or disable stdout logging.

stdout_level str

Log level for stdout logging.

stdout_serialize bool

Enable or disable serialization for stdout logging.

stderr_enable bool

Enable or disable stderr logging.

stderr_level str

Log level for stderr logging.

stderr_serialize bool

Enable or disable serialization for stderr logging.

file_enable bool

Enable or disable file logging.

file_level str

Log level for file logging.

file_path str

Path to the log file.

file_serialize bool

Enable or disable serialization for file logging.

file_rotation str

Rotation policy for the log file.

file_retention str

Retention policy for the log file.

notification_enable bool

Enable or disable notification logging.

notification_level str

Log level for notification logging.

notification_urls list[str]

List of URLs for notification services.

ResponseSettings ⚓︎

Bases: BaseModel

Defines the response settings schema.

Attributes:

Name Type Description
default_enabled(bool)

Whether to enable the default message

default_message(str)

The message to return when no handler has been triggered

error_message(str)

The message to send to the user when an error happens

WebSettings ⚓︎

Bases: BaseModel

Defines the web settings schema.

Attributes:

Name Type Description
host str

The host of the web interface.

port int

The port of the web interface.