layered-settings
layered-settings
is a simple and configurable hierarchical settings library for Python, including Django, Flask, or any other
scripts that need settings from potentially a variety of sources. With it you can load
settings from the environment, Amazon's SSM, Amazon's Secrets Manager, local configparser .ini files, and more.
Installation
The package is available on pip as layered-settings
. Run:
pip install layered-settings
If you want to use the AWS SSM layer or Secrets Manager layer, include the [aws] extra:
pip install layered-settings[aws]
then import via:
from layered_settings import initialize_settings, loaders
Example Usage
import os
from layered_settings import initialize_settings
from layered_settings import loaders
get_setting = initialize_settings(
sources=[
loaders.ConfigParserLoader(os.path.join(SCRIPT_DIR, "setting-defaults.ini")),
os.path.expanduser("~/.app-settings.ini"),
{
"general": {"CLIENT_NAME": "client"},
"email": {"EMAIL_HOST": "smtp.example.com", "EMAIL_PORT": 25},
},
loaders.SecretsManagerLoader(f"/app/stage/", aws_region="us-east-1") if ALLOW_SSM_CONFIGURATION else None,
loaders.SSMLoader(f"/app/stage/", aws_region="us-east-1") if ALLOW_SSM_CONFIGURATION else None,
loaders.EnvLoader("APP__{section}__{key}"),
]
)
DATABASE_HOST = get_setting("database", "DATABASE_HOST")
AWS_SECRET_KEY = get_setting("aws", "AWS_SECRET_KEY")
...
Frameworks Supported
This library is tested using Python 3.x.