Flask Environment Manager
A Flask environment manager for copying parameters from a source into the Flask app config, while also abiding by an override whitelist.
Currently, this packaged supports overriding the config from:
os.environviaOsEnvironmentManagerAWS SSMviaSsmEnvironmentManager
Installation
Install and update using pip:
pip install -U flask-environment-manager
Getting Started
Before using the Environment Manager, you must ensure the following is set up in your Flask app config:
- ENV_OVERRIDE_WHITELIST
Example
{
"ENV_OVERRIDE_WHITELIST": [
"ENV_VAR_1",
"ENV_VAR_2",
"ENV_VAR_3",
"ENV_VAR_4"
],
}
The keys stored in the whitelist will be the only keys updated in the config.
Managers
SSM Environment Manager
This manager can be imported with from flask_environment_manager import SsmEnvironmentManager
This manager requires the following to be definied in the Flask app config, in addition the the whitelist:
- AWS_SSM_ACCESS_KEY
- AWS_SSM_ACCESS_SECRET
- AWS_SSM_REGION
This manager will connect to AWS SSM and get parameters from a given path.
The following snippet will load all parameters nested under the /directory path (recursively).
from flask_environment_manager import SsmEnvironmentManager
manager = SsmEnvironmentManager(app, "/directory")
manager.load_into_config()
It is important to note that parameters are stored as their final name in the path. For example, the parameter stored at /directory/params/param will be stored as param.
Local Development
The SSM Manager also provides a way to work with a local config file, instead of SSM. This is useful when working locally or in a way where you don't have access to SSM.
You can access this by passing config_pyfile into the constructor. This will make use of config.from_pyfile when reading the file name passed in.
Example:
from flask_environment_manager import SsmEnvironmentManager
manager = SsmEnvironmentManager(app, "/directory", config_pyfile='local_config.py')
manager.load_into_config()
Where local_config.py looks something like:
KEY1=1
KEY2="2"
KEY3=True
OS Environment Manager
This manager can be imported with from flask_environment_manager import OsEnvironmentManager
This manager will use the os.environ keys and values to update the app.config
from flask_environment_manager import OsEnvironmentManager
manager = OsEnvironmentManager(app)
manager.load_into_config()
Developing
The build pipeline requires your tests to pass and code to be formatted
Make sure you have Python 3.x installed on your machine (use pyenv).
Install the dependencies with pipenv (making sure to include dev and pre-release packages):
pipenv install --dev --pre
Configure your environment:
pipenv shell && export PYTHONPATH="$PWD"
Run the tests:
pytest
Or with logging:
pytest -s
Or tests with coverage:
pytest --cov=./
Format the code with Black:
black $PWD
Releases
Cleanup the (.gitignored) dist folder (if you have one):
rm -rf dist
Notch up the version number in setup.py and build:
python3 setup.py sdist bdist_wheel
Push to PyPi (using the ScholarPack credentials when prompted)
python3 -m twine upload --repository-url https://upload.pypi.org/legacy/ dist/*
Links
Release files for flask-environment-manager 2.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| flask-environment-manager-2.4.0.tar.gz | 5.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| flask_environment_manager-2.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.7 kB
Release files / flask-environment-manager-2.4.0.tar.gz
| Download URL | flask-environment-manager-2.4.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a23a1dc6ed7f9ca63958ff422644428abd7c4e3f665ea99f97d4431414dff879
|
|
BLAKE2b-256 checksum How to use checksums |
9285d813eea558b9f2cf1d744e5994c6f0afac684326153cf4b422d02dbee325
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
|
Release files / flask_environment_manager-2.4.0-py3-none-any.whl
| Download URL | flask_environment_manager-2.4.0-py3-none-any.whl |
|---|---|
| Size | 7.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c7a344b607de6efb9414bbf7a1a7c7ee280c1ad5cdf611aeb64b8554313b311c
|
|
BLAKE2b-256 checksum How to use checksums |
fee7727f706e191e7384926d981e6b6965c2d85434f16096c03b9b202c467b5a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/51.3.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.1
|