Skip to content

Commit

Permalink
Update ModSettings and yaml_settings_actory
Browse files Browse the repository at this point in the history
Remove settings param from yaml_settings_factory

Convert Config class in ModSettings to model_config + method
  • Loading branch information
TheByronHimes committed Oct 10, 2023
1 parent 1a79c54 commit fef36ce
Showing 1 changed file with 18 additions and 26 deletions.
44 changes: 18 additions & 26 deletions src/hexkit/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing import Any, Callable, Final, Optional

import yaml
from pydantic import BaseSettings
from pydantic_settings import BaseSettings, PydanticBaseSettingsSource

# Default config prefix:
DEFAULT_CONFIG_PREFIX: Final = "ghga_services"
Expand Down Expand Up @@ -77,7 +77,7 @@ def get_default_config_yaml(prefix: str) -> Optional[Path]:

def yaml_settings_factory(
config_yaml: Optional[Path] = None,
) -> Callable[[BaseSettings], dict[str, Any]]:
) -> Callable[[], dict[str, Any]]:
"""
A factory of source methods for pydantic's BaseSettings Config that load
settings from a yaml file.
Expand All @@ -87,9 +87,7 @@ def yaml_settings_factory(
Path to the yaml file to read from.
"""

def yaml_settings( # pylint: disable=unused-argument
settings: BaseSettings,
) -> dict[str, Any]:
def yaml_settings() -> dict[str, Any]:
"""Source method for loading pydantic BaseSettings from a yaml file"""
if config_yaml is None:
return {}
Expand Down Expand Up @@ -161,30 +159,24 @@ def constructor_wrapper(
class ModSettings(settings):
"""Modifies the orginal Settings class provided by the user"""

# pylint: disable=too-few-public-methods
class Config:
"""pydantic Config subclass"""

frozen = True

# add this prefix to all variable names to
# define them as environment variables:
env_prefix = f"{prefix}_"

@classmethod
def customise_sources(
cls,
model_config = {"frozen": True, "env_prefix": f"{prefix}_"}

@classmethod
def settings_customise_sources( # noqa: PLR0913
cls,
settings_cls: type[BaseSettings],
init_settings: PydanticBaseSettingsSource,
env_settings: PydanticBaseSettingsSource,
dotenv_settings: PydanticBaseSettingsSource,
file_secret_settings: PydanticBaseSettingsSource,
):
"""Add custom yaml source"""
return (
init_settings,
env_settings,
file_secret_settings,
):
"""Add custom yaml source"""
return (
init_settings,
env_settings,
file_secret_settings,
yaml_settings_factory(config_yaml),
)
yaml_settings_factory(config_yaml),
)

# construct settings class:
return ModSettings(**kwargs)
Expand Down

0 comments on commit fef36ce

Please sign in to comment.