Flexible configuration handler for Python projects.
Project description
PyFlexCfg
Flexible configuration handler for Python projects.
Description
The handler (a.k.a. Cfg) allows to store project's configuration in YAML files and load them at once when imported.
Installation
pip install pyflexcfg
Configuration
In order to use encryption\decryption for configuration secrets, make sure to set an environment variable PYFLEX_CFG_KEY with some encryption key, which will be used for secrets encryption\decryption.
By default, Cfg searches for configuration files in the config directory within current working dir. In order to specify a different path, crate a nenv variable PYFLEX_CFG_ROOT_PATH with the absolute path to the configuration root.
Basic Usage
Assuming you have such configuration files structure:
\project
├─ config
├─ general.yaml
├─ env
├─ dev.yaml
├─ prd.yaml
And each of the yaml files contains a configuration option data: test
Just import the Cfg and you can use your configuration at once:
from pyflexcfg import Cfg
print(Cfg.general.data)
print(Cfg.env.dev.data)
print(Cfg.env.prd.data)
Make sure the names of directories within your configuration are Python object's attribute name compatible.
Override by environment variables
There is an option to override values in YAML files with values from environment variables. In order to do so, you have to create env variables which reflect the name of config value to be overwritten. For example, if you want to overwrite value for
Cfg.env.dev.data
you have to create env variable like this
CFG__ENV__DEV__DATA
Having that, you can call
Cfg.update_from_env()
and the value from the YAML file will be overwritten by the value from env variable.
Handling secrets
Any sensitive data to be stored within your configuration files should be encrypted!
Encrypt your secret:
from pyflexcfg import AESCipher
aes = AESCipher('secret-key')
aes.encrypt('some-secret-to-encrypt')
You will get output like
b'A1u6BIE2xGtYTSoFRE83H0VHsAW3nrv4WB+T/FEAj1fsh8HIId9r/Rskl0bnDHTI'
and it can be stored in yaml file with !encr prefix:
my_secret: !encr b'A1u6BIE2xGtYTSoFRE83H0VHsAW3nrv4WB+T/FEAj1fsh8HIId9r/Rskl0bnDHTI'
The next time Cfg will be loading this configuration file it will automatically decrypt this value, but do not forget to set up env variable PYFLEX_CFG_KEY with your 'secret-key', otherwise Cfg won't be able to decrypt the data.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyflexcfg-0.2.0.tar.gz.
File metadata
- Download URL: pyflexcfg-0.2.0.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
985796c2af0f4f76b814d276f7d7c05ffc53c258d0d1d11226120949782e7e43
|
|
| MD5 |
2b531d568f2ac0efa10d363eb33a92e5
|
|
| BLAKE2b-256 |
a9f508cf69d2897e31bf5caea4a8ad7b388773d7e8b507f93ab6c0ee98da5265
|
File details
Details for the file pyflexcfg-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pyflexcfg-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.10.0 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43547dcc824059c3b7eb34aaef74106182112eeae8e787f8752b84c21b6a8b22
|
|
| MD5 |
16aa9f8c280453aa127af7aad059831d
|
|
| BLAKE2b-256 |
f29ce9143f588ef93964b59b36af088a8ec278cf2a5bee087d5230ae05543795
|