Skip to main content

Flexible configuration handler for Python projects.

Project description

PyFlexCfg

Flexible configuration handler for Python projects.

Description

PyFlexCfg allows you to store your project's configuration in YAML files and seamlessly load them as a unified object when imported into your Python code.

Features:

  • YAML-based config: Organize your project's settings using easy-to-read YAML files within nested directories to logically group your configurations.
  • Unified Access: Load all configuration files as a single object for easy access.
  • Values Override: Dynamically override configuration values using environment variables.
  • Secrets Management: Encrypt and decrypt sensitive data directly within your configuration files.

Installation

pip install pyflexcfg

Configuration

There are several environment variables that could be set in order to adjust PyFlexCfg behaviour:

  1. By default, PyFlexCfg looks for configuration files in a directory config within the current working directory. To specify a different path, define an environment variable with the absolute path to the desired configuration root directory:

    PYFLEX_CFG_ROOT_PATH=\path\to\config
    
  2. In order to use encrypted configuration values, you must set an environment variable with encryption key, which will be used for secrets decryption:

    PYFLEX_CFG_KEY=super_secret_key
    

Basic Usage

Assuming the following configuration file structure:

    \project  <-- working directory
        ├─ config
            ├─ general.yaml
            ├─ env
                ├─ dev.yaml
                ├─ prd.yaml

And each YAML file contains a configuration option like this:

data: test

You can load and use the configurations as follows:

from pyflexcfg import Cfg

print(Cfg.general.data)  # Access data from general.yaml
print(Cfg.env.dev.data)  # Access data from env/dev.yaml
print(Cfg.env.prd.data)  # Access data from env/prd.yaml

Note: Directory and file names in your configuration structure must be compatible with Python attribute naming conventions.

Overriding values with Environment Variables

You can override values in YAML files using environment variables. To do this, create environment variables that reflect the configuration values you want to override. This feature is particularly useful in environments like Docker Compose, where you can pass environment-specific settings to containers dynamically, enabling seamless configuration management across different deployment setups.

For example, if you want to overwrite the value of

Cfg.env.dev.data

Define an environment variable:

CFG__ENV__DEV__DATA=some_value

Having that set, call the method

from pyflexcfg import Cfg

Cfg.update_from_env()

The value from the environment variable will replace the corresponding value from the YAML file.

Handling secrets

Any sensitive data present in configuration files should be encrypted!

Encrypting a Secret

Use the AESCipher class to encrypt your secrets:

from pyflexcfg import AESCipher

aes = AESCipher('secret-key')
aes.encrypt('some-secret-to-encrypt')

This will produce an output like:

b'A1u6BIE2xGtYTSoFRE83H0VHsAW3nrv4WB+T/FEAj1fsh8HIId9r/Rskl0bnDHTI'

Store the encrypted secret in a YAML file with the !encr prefix:

my_secret: !encr b'A1u6BIE2xGtYTSoFRE83H0VHsAW3nrv4WB+T/FEAj1fsh8HIId9r/Rskl0bnDHTI'

Decrypting Secrets

When PyFlexCfg loads the configuration and the environment variable PYFLEX_CFG_KEY is set with your encryption key, it will automatically decrypt values marked with !encr prefix and store them in Cfg as a Secret strings, masking them in logs and console outputs with ******.

Use the AESCipher class to manually decrypt your secrets if needed:

from pyflexcfg import AESCipher

aes = AESCipher('secret-key')
aes.decrypt(b'A1u6BIE2xGtYTSoFRE83H0VHsAW3nrv4WB+T/FEAj1fsh8HIId9r/Rskl0bnDHTI')

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

pyflexcfg-0.3.0.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

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

pyflexcfg-0.3.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file pyflexcfg-0.3.0.tar.gz.

File metadata

  • Download URL: pyflexcfg-0.3.0.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.0 Windows/10

File hashes

Hashes for pyflexcfg-0.3.0.tar.gz
Algorithm Hash digest
SHA256 31088838157315f054486e271ac71bf678ef3c81bd974738a56f52ebe7daf420
MD5 1291732ad0ad8cb01c98a7eb596abce4
BLAKE2b-256 5f0364bc2c5f905afaf42f3db0e5e42b026f361151bd69e3c61f5823a69dd2e7

See more details on using hashes here.

File details

Details for the file pyflexcfg-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: pyflexcfg-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.10.0 Windows/10

File hashes

Hashes for pyflexcfg-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 385d70e24a6d4d7d1bf1560c4348963d0dd44acf96021ad2b664a77e8f4cdac0
MD5 b5ec814872bb7e1a003e31af85a39e3b
BLAKE2b-256 6a8d6a5e0c3b33c0a665efe3f75c2b78b7bc8f36ad1ca52d1000c269ae00866a

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