Overridable config for shared libraries.
Project description
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 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
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
File details
Details for the file flexisettings-1.0.5.tar.gz
.
File metadata
- Download URL: flexisettings-1.0.5.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 611e9ca901476120fc8d9cdf388e5bf2166d4ccf2550091b5fa8f09997305b19 |
|
MD5 | da3112fc1bbd19f88e2a5cff68feb22b |
|
BLAKE2b-256 | c8441b6b3e2780e14044dae90a0f61fb4dc4c0dda0bb8a148e379997df019c20 |