YAML substitution
A wrapper around the dotenv and pyaml-env packages.
This project just serves as a wrapper to load environment variables from a .env file, and then update a YAML configuration file with environment variables as needed / required.
It's important to view the documentation for the pyaml-env and dotenv projects.
- python, version 3.11+
- python's build package:
pip install build
To install using pip:
pip install yamlsub
To include the package from VCS in a project, you can follow the pip documentation to import a GitHub project.
For example:
pip install yamlsub@git+https://github.com/willmorejg/yamlsub.git
Review the documentation for the pyaml-env and dotenv projects for additioanl information on how best to incorporate this project into other projects.
Using an example .env file containing the following ...
DB_HOST=localhost
DB_USER=root
DB_PASS=123456
... and a sample config.yaml file ...
database:
host: !ENV ${DB_HOST}
user: !ENV ${DB_USER}
pass: !ENV ${DB_PASS}
... and the following code ...
from yamlsub.config import Config
cfg = Config(env_path='.env', yaml_path='config.yaml')
, you can access configuration values as follows:
database_config = cfg.get_config_key('database')
dbhost = database_config['host']
dbuser = database_config['user']
dbpass = database_config['pass']
print(f'Host: {dbhost}, User: {dbuser}, Pass: {dbpass}')