Skip to main content

Support hierarchical loading of applications settings from a number of sources.

Project description

Configular

Support hierarchical loading of application settings from a number of sources.

This is useful for libraries where you want to allow an including application to provide configuration to your code from sources such as django settings, constance, and environment values. Also supports additional custom sources.

Supports optional retrieveal of secrets with values like %%example_key%%. If no secrets managers are configured, and a value matching the format is presented, a warning will be logged.

Common hierarchy of lookups, first defined will be used:

  1. constance
  2. settings.py
  3. os.environ
  4. Settings['key']

Any value found that is surrounded by %% will be processed by the configured secrets_managers.

Settings values are assumed to be dynamic, Settings objects use callables to retrieve the values at run time. This means specifically that changes in the Constance admin will apply to any look ups made after the values are saved.

Secrets (at least in the CredstashManager) are assumed to be immutable. A functools.lru_cache is used around the lookup for cost and performance. If you have rotated the credentials (or need to reset the cache in tests) you can call flush_secret_cache on your CredstashManager instance to reset the lru_cache. This means a value of %%secret%% will look up the secured value one time and cache it in memory until flush_secret_cache is called.

Usage

from configular import Settings
from configular.constance_loader import ConstanceLoader
from configular.credstash_manager import CredstashManager
from configular.django_loader import DjangoLoader
from configular.environ_loader import EnvironLoader

CM = CredstashManager()

loader_settings = Settings(
    {
        'A_SETTING': 'DEFAULT'
    },
    'TEST_PREFIX',
    loaders=[ConstanceLoader, DjangoLoader, EnvironLoader],
    secrets_managers=[CM],
)

To update the loaders and secrets_managers in a settings instance, call reconfigure.

from configular.credstash_manager import CredstashManager
from configular.django_loader import DjangoLoader
from configular.environ_loader import EnvironLoader

from myapp.conf import myapp_settings

myapp_settings.reconfigure(
    loaders=[DjangoLoader, EnvironLoader],
    secrets_managers=[CM],
)

Configuration

Django settings must be defined in a dict named with the defined prefix

TEST_PREFIX = {'A_SETTING': 'NEW_VALUE'}

Constance settings must be named with the prefix leading followed by an underscore, e.g.

CONSTANCE_CONFIG = {
    'TEST_PREFIX_A_SETTING': (True, 'A fine setting'),
}

A sublcass of django.core.exceptions.ImproperlyConfigured is provided that can be used to enforce configuration during app startup.

from configular.exceptions import ImproperlyConfigured

if loader_settings.MY_APP_VERSION not in ('2.25', '2.27'):
    raise ImproperlyConfigured(f'MY_APP_VERSION={MY_APP_VERSION} not supported')

or with a decorator for checking validation

from configular.decorators import validate_setting

@validate_setting(
    settings=loader_settings,
    key='MY_APP_VERSION',
    validate_func=lambda my_app_version: my_app_version in ('2.25', '2.27'),
)
def func()
    ...

Constance settings take precedence over defined django settings. Trying to access a setting that is not defined in your Settings object will raise AttributeError.

Running tests locally

Ensure tox is installed. e.g. pip install -g tox

docker-compose up -d --build  # Tests require a running react server
tox

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

configular-1.1.0.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

configular-1.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file configular-1.1.0.tar.gz.

File metadata

  • Download URL: configular-1.1.0.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for configular-1.1.0.tar.gz
Algorithm Hash digest
SHA256 17286e6ec2fc354eb4966aeee5678d33eb3fd98ef7540208fe8cf906ff972b60
MD5 3a6989c81cf00bcd0d811b343490b585
BLAKE2b-256 91cf3e50eab3dba5db226f3222e7200a31b1c12cb879e339d53b4fecf659fee3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: configular-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for configular-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fefa294f57baa2c972abfeed1cbfa82e2b8b990eedeac5a3ccda0b974ff0fe2
MD5 74a39df65fe1dbbf35ef4047f4d7709a
BLAKE2b-256 c85167e32dd77421bc67eb722138b34a82f95ce0d42442285f3f7c5ff5f8ef5d

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