Skip to main content

Environment specific management of application configuration

Project description

Environment Settings

The environment_settings package allows applications to manage multiple runtime environment configurations in a central location using settings stored as normal python modules. Inspired by the [Django settings framework] (https://docs.djangoproject.com/en/dev/ref/settings/), the settings are loaded dynamically using a context manager at the application entry point. Additionally, the environment_settings module provides a decorator that can be used to load the correct settings for automated tests.

Usage

The following guide shows a typical application structure with support for multiple environment configurations. The conf package contains configuration files for each environment.

my-app
|--my_app
|  |--conf
|  |  |--__init__.py
|  |  |--dev.py
|  |  |--uat.py
|  |  |--unittest.py
|  |  `--prod.py
|  |--app.py
|  `--lib.py
|--tests
|  `--unittests
|     `--test_lib.py
|--pyproject.toml
|--README.md
`--setup.py

Settings package configuration

Add the following code to the packge __init__.py to mark it as "settings enabled"

# my_app/conf/__init__.py
from environment_settings import Settings

settings = Settings(package=__name__)

Settings file layout

Each settings file are implemented as normal python modules. Settings should be UPPERCASE as per the example below. Any other python code will not be visible in the settings context.

# my_app/conf/dev.py
GOOD_SETTING_EXAMPLE = 'I am visible'
bad_setting_example = 'I am not visible'

Initialising the settings context at the application entry point

Before any setting can be used, the settings context needs to be activated. Failure to active the context will result in an ImproperlyConfigured exception

# my_app/app.py
from my_app.conf import settings

with settings.environment('dev'):
    print(settings.GOOD_SETTINGS_EXAMPLE)

# Out[0]: 
# I am visible

print(settings.GOOD_SETTINGS_EXAMPLE)

# Out[1]:
# Traceback (most recent call last):
# ...
# ImproperlyConfigured: Requested GOOD_SETTINGS_EXAMPLE, but settings are not configured. You must define the settings.environment(name)

Using settings throughout the application

The centralised settings object can be imported anywhere throughout the application:

# my_app/lib.py
from my_app.conf import settings

def my_func():
    return settings.GOOD_SETTINGS_EXAMPLE

Testing

environment_settings also provides a decorator that can be used to provide test specific settings in test cases:

# my_app/conf/unittest.py
GOOD_SETTINGS_EXAMPLE = 'unittest example'

# tests/unittests/test_lib.py
from my_app.conf import settings
from my_app import lib

@settings.environment('unittest')
class TestLib:
    def test_lib(self):
        assert lib.my_func() == 'unittest example'

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

environment_settings-0.0.3.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

environment_settings-0.0.3-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file environment_settings-0.0.3.tar.gz.

File metadata

  • Download URL: environment_settings-0.0.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for environment_settings-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d0c8ab0ce6b6f08a089d877a2f52acf4efaf4821cf6890161637af2768b294d8
MD5 bd230e4a501ad72709591d7b033fb904
BLAKE2b-256 78d7925aa87f05899cd0149ee56699d0efdc4fc3abb637a3241374fac6ce37a6

See more details on using hashes here.

File details

Details for the file environment_settings-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for environment_settings-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 649a4af6bc95580158159dee8b37650398ee4a11dd8d479bd749585302b6f4b1
MD5 c58d432d89a31088d9cdb3834b7f6f87
BLAKE2b-256 914ec59150fc9d0ac2a179094ddee7d605417f774990a998c70668af60876258

See more details on using hashes here.

Supported by

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