Skip to main content

Flexible, simple, extensible settings loader from environment, AWS SSM, AWS Secrets Manager, configparser .ini, and more.

Project description

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=[
        # Lowest priority is setting-defaults.ini.  All configuration values should be defaulted in here.
        # The `loaders.ConfigParserLoader` is optional - if you pass a string ending in .ini, it wraps it in
        # a `loaders.ConfigParserLoader` for you.
        loaders.ConfigParserLoader(os.path.join(SCRIPT_DIR, "setting-defaults.ini")),

        # Next priority is a "user" settings file.
        os.path.expanduser("~/.app-settings.ini"),

        # You can include a dictionary in the sources too...
        {
            "general": {"CLIENT_NAME": "client"},
            "email": {"EMAIL_HOST": "smtp.example.com", "EMAIL_PORT": 25},
        },

        # Secrets Manager works similar to SSM. If the secret is a json object, the object's keys will be the layered setting "key"
        # If the secret is just plain text, the secret's name will be used as the key
        loaders.SecretsManagerLoader(f"/app/stage/", aws_region="us-east-1") if ALLOW_SSM_CONFIGURATION else None,

        # If we are able/willing to reach out to AWS, do so.  A `None` in the initialize_settings sources
        # will simply be ignored.
        loaders.SSMLoader(f"/app/stage/", aws_region="us-east-1") if ALLOW_SSM_CONFIGURATION else None,

        # Top priority -- env var in the format APP__section__key.
        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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

layered-settings-1.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

layered_settings-1.1.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file layered-settings-1.1.0.tar.gz.

File metadata

  • Download URL: layered-settings-1.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for layered-settings-1.1.0.tar.gz
Algorithm Hash digest
SHA256 0904e4f0b38a25a4e0f294a6c7fddc1678dae9928d1bbdb4430aa7ff5cb7416d
MD5 c73d4e37e8abad5b95eb1fa09233594e
BLAKE2b-256 debdba38c9c7d1db0faf8b1916e35e4caa75e732211e0ae9ead04903556ac51d

See more details on using hashes here.

File details

Details for the file layered_settings-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for layered_settings-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 edf6a56c7515020d8e48bf15b7eebc8adefbc054c49c0b288f473c9c4433fa50
MD5 7b77fc3228072f9b71b10ad2e902c7ca
BLAKE2b-256 96d93b7a42df489dbfd3322f63e11accd19dfcfa20f70d37a76667c8ae132aae

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page