Skip to main content

Latest PyPI version Build Status

Partly inspired by Django’s from django.conf import settings settings object.

The goal is to allow shared libraries to be configured by a settings file in the project which imports them (like how Django libraries can expect the Django settings object to exist).

Usage

pip install flexisettings

We want the shared lib to be able to load config values from the app which is importing it.

A suggested layout would be as found in test_project/test_lib in this repo. For example, create a test_lib/conf/__init__.py like:

from flexisettings import Settings

settings = Settings(initial_namespace='TEST_LIB', defaults='test_lib.conf.defaults')

We have a concept of customisable ‘namespace’ (prefix) for config values. This is as defined by the ConfigLoader lib we are making use of.

initial_namespace is the default namespace for config values of your shared lib. Projects who want to use your lib will be able to customise the namespace, but as they are used for bootstrapping there are two config values which will always use the default name (APP_CONFIG and CONFIG_NAMESPACE).

So for example, say myapp wants to use test_lib. myapp can customise the namespace by defining TEST_LIB_CONFIG_NAMESPACE = 'CUSTOM'.

defaults is the import path to a python module or object in your shared lib which contains default values for your config. These keys should not be namespaced.

For example if you want the config namespace for your shared lib to be configurable via env var you could create test_lib/conf/defaults.py like:

import os

# namespace for config keys loaded from e.g. Django conf or env vars
CONFIG_NAMESPACE = os.getenv('TEST_LIB_CONFIG_NAMESPACE', 'TEST_LIB')

APP_CONFIG = os.getenv('TEST_LIB_APP_CONFIG', None)

Then myapp would be able to export TEST_LIB_CONFIG_NAMESPACE=CUSTOM.

That explains namespace customisation a bit, what about the APP_CONFIG?

Say for example that myapp is a Django website and test_lib has the defaults file shown above. In your myapp project you could:

export TEST_LIB_CONFIG_NAMESPACE=CUSTOM
export TEST_LIB_APP_CONFIG=django.conf.settings

Then in myapp/settings.py you could have:

CUSTOM_VAR1 = 'whatever'

Now, recall the test_lib/conf/__init__.py that we created at the start. In your test_lib code you could have:

from test_lib.conf import settings

assert settings.VAR1 == 'whatever'

As you can see the VAR1 was set in the importing project’s Django settings with the CUSTOM_ prefix but is available in your shared lib’s settings object under its non-prefixed name.

Compatibility

This project is tested against:

Python 2.7

Python 3.6

Python 3.7

Python 3.8

Running the tests

CircleCI

The easiest way to test the full version matrix is to install the CircleCI command line app:
(requires Docker)

The cli does not support ‘workflows’ at the moment so you have to run the two Python version jobs separately:

circleci build --job python-2.7
circleci build --job python-3.8

py.test (single python version)

It’s also possible to run the tests locally, allowing for debugging of errors that occur.

Now decide which Python version you want to test and create a virtualenv:

pyenv virtualenv 3.8.5 flexisettings
pip install -r requirements-test.txt

The code in test_project demonstrates collaborative config between a shared library test_lib and the app that wants to use it app. Set the path to the test project

make test

Metadata

Release files for flexisettings 1.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flexisettings 1.0.5
File Size Uploaded
flexisettings-1.0.5.tar.gz 5.7 kB Details

Release files / flexisettings-1.0.5.tar.gz

Download URL flexisettings-1.0.5.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
611e9ca901476120fc8d9cdf388e5bf2166d4ccf2550091b5fa8f09997305b19
BLAKE2b-256 checksum
How to use checksums
c8441b6b3e2780e14044dae90a0f61fb4dc4c0dda0bb8a148e379997df019c20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

1.0.5 This release

1 release file

1.0.4

1 release file

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page