Implementation:173TECH Sayn read settings
| Knowledge Sources | |
|---|---|
| Domains | Configuration_Management, Validation |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for reading, validating, and resolving SAYN settings from YAML files and environment variables provided by the SAYN framework.
Description
The read_settings function reads settings.yaml and SAYN_* environment variables, returning a result containing both the parsed SettingsYaml model and the Environment model. The get_settings function merges these into a single configuration dict by resolving the specified profile. The get_connections function creates Database instances from the resolved credentials.
Usage
Use these functions during SAYN application startup to parse settings, resolve the active profile, and create database connections. Called by App.start_app() → set_settings().
Code Reference
Source Location
- Repository: SAYN
- File:
sayn/core/settings.py - Lines: L29-51 (
TableGlob), L54-102 (Environment), L105-232 (SettingsYaml,Profile,get_profile_info), L235-283 (read_settings), L286-320 (get_settings), L323-340 (get_connections)
Signature
def read_settings() -> Result[{"yaml": SettingsYaml, "env": Environment}]
def get_settings(
yaml: Optional[SettingsYaml],
environment: Optional[Environment],
profile_name: Optional[str] = None,
) -> Result[Dict]
def get_connections(
credentials: Dict[str, Mapping],
) -> Result[Dict[str, Union[Database, dict]]]
class SettingsYaml(BaseModel):
credentials: Mapping[str, Mapping]
profiles: Mapping[str, Profile]
default_profile: Optional[str]
def get_profile_info(self, profile_name: Optional[str] = None) -> dict
class Environment(BaseModel):
parameters: Optional[Mapping[str, Any]]
credentials: Optional[Mapping[str, Mapping[str, Any]]]
stringify: Optional[Stringify]
from_prod: Optional[Sequence[TableGlob]]
default_run: Optional[str]
Import
from sayn.core.settings import read_settings, get_settings, get_connections, SettingsYaml
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| settings.yaml | file | Yes | YAML file with credentials, profiles, default_profile |
| SAYN_* env vars | environment | No Environment variables for parameters, credentials, stringify, from_prod, default_run | |
| profile_name | str | No Profile to resolve (defaults to default_profile)
|
Outputs
| Name | Type | Description |
|---|---|---|
| read_settings result | Result[Dict] | {"yaml": SettingsYaml, "env": Environment}
|
| get_settings result | Result[Dict] | Dict with keys: parameters, credentials, stringify, from_prod, default_run |
| get_connections result | Result[Dict] | Dict of connection name → Database instance (or dict for API type)
|
Usage Examples
Reading and Resolving Settings
from sayn.core.settings import read_settings, get_settings, get_connections
# Step 1: Read settings.yaml and environment variables
result = read_settings()
if result.is_err:
print(f"Error: {result.error}")
else:
settings = result.value
yaml_config = settings["yaml"] # SettingsYaml or None
env_config = settings["env"] # Environment or None
# Step 2: Resolve the active profile
settings_dict = get_settings(yaml_config, env_config, profile_name="dev")
if settings_dict.is_ok:
config = settings_dict.value
print(config["credentials"]) # Resolved credentials
print(config["stringify"]) # Dev prefix/suffix/override
print(config["from_prod"]) # from_prod glob patterns
# Step 3: Create database connections
connections_result = get_connections(config["credentials"])
if connections_result.is_ok:
connections = connections_result.value
db = connections["warehouse"] # Database instance
Profile Resolution Without YAML (Env Vars Only)
# When settings.yaml doesn't exist, only env vars are used
result = read_settings()
# result.value["yaml"] will be None
# result.value["env"] will have SAYN_* variables
# get_settings with no profile uses environment directly
settings = get_settings(None, result.value["env"], profile_name=None)