Simple YAML configuration file parser with easy access for structured data
Project description
EnvYAML | Easy configuration file parser for structured data
Motivation
Modern configuration files become to be more and more complex, flexible, and readable. YAML file format is perfect to store configuration but had no option to pass environment variables. They give flexibility, readability and provide an option to store complex data structure. This project goal is to simplify usage of the YAML file and environment variables as program configuration files with easy config key access.
Install
pip install envyaml
Basic usage
Let's assume we had a project with this config file env.yaml
# env.yaml
project:
name: "${PROJECT_NAME}-${PROJECT_ID}"
database:
host: $DATABASE_HOST
port: 3301
username: username
password: $DATABASE_PASSWORD
database: test
table:
user: table_user
blog: table_blog
query: |-
SELECT * FROM "users" WHERE "user" = $1 AND "login" = $2 AND "pwd" = $3
insert: |-
INSERT INTO "{table}" (user, login) VALUES ($1, $2)
redis:
host: $REDIS_HOST|127.0.0.1
port: 5040
db: $REDIS_DB|3 # with default value
config:
expire: 300
prefix: $REDIS_PREFIX
escaped: $$.extra
empty_env: $NOT_EXIST_ENV_VARIABLE
Environment variables set to
PROJECT_NAME=simple-hello
PROJECT_ID=42
DATABASE_HOST=xxx.xxx.xxx.xxx
DATABASE_PASSWORD=super-secret-password
REDIS_PREFIX=state
Parse file with EnvYAML
from envyaml import EnvYAML
# read file env.yaml and parse config
env = EnvYAML('env.yaml')
# access project name
print(env['project.name'])
# >> simple-hello-42
# access whole database section
print(env['database'])
# {
# 'database': 'test',
# 'host': 'xxx.xxx.xxx.xxx',
# 'password': 'super-secret-password',
# 'port': 3301,
# 'table':
# {
# 'blog': 'table_blog',
# 'user': 'table_user'
# },
# 'username': 'username'
# }
# access database host value as key item
print(env['database.host'])
# >> xxx.xxx.xxx.xxx
# access database user table value as key item
print(env['database.table.user'])
# >> table_user
# get sql query with $1,$2,$3 variables
print(env['database.query'])
# >> SELECT * FROM "users" WHERE "user" = $1 AND "login" = $2 AND "pwd" = $3
# using default values if variable not defined
# one example is redis host and redis port, when $REDIS_HOST not set then default value will be used
print(env['redis.host'])
# >> 127.0.0.1
# one example is redis host and redis port, when $REDIS_DB not set then default value will be used
print(env['redis.db'])
# >> 3
# access list items by number
print(env['list_test'][0])
# >> one
# access list items by number as key
print(env['list_test.1'])
# >> two
# test if you have key
print('redis.port' in env)
# >> True
Access config with get
function and default value
print(env.get('not.exist.value', 'default'))
# >> default
print(env.get('empty_env', 'default'))
# >> default
print(env['empty_env'])
# >> None
Use format
function to update placeholder
print(env.format('database.insert', table="users"))
# >> INSERT INTO "users" (user, login) VALUES ($1, $2)
Strict mode
This mode is enable by default and prevents from declaring variables that do not exist in environment variables
or .env
file. This leads to having runtime ValueError
exception when variables do not define with message Strict mode enabled, variable $VAR not defined!
. To disable strict mode specify strict=False
at EnvYAML object initialization. Another option to disable strict
mode is to define ENVYAML_STRICT_DISABLE
environment variable before initializing EnvYAML object.
Escaped variables
In case of usage $
in env.yaml file as value double $$
should be used. Example:
Use escaped
variable
print(env['escaped'])
# >> $.extra
License
MIT licensed. See the LICENSE file for more details.
Project details
Release history Release notifications | RSS feed
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
File details
Details for the file envyaml-1.10.211231.tar.gz
.
File metadata
- Download URL: envyaml-1.10.211231.tar.gz
- Upload date:
- Size: 7.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88f8a076159e3c317d3450a5f404132b6ac91aecee4934ea72eac65f911f1244 |
|
MD5 | d3251f65d7be437ff8f370dc69c78014 |
|
BLAKE2b-256 | aacebcc062f1d55368713674cf55851a4d9dfa77835c0258753d0f23dff70743 |
File details
Details for the file envyaml-1.10.211231-py2.py3-none-any.whl
.
File metadata
- Download URL: envyaml-1.10.211231-py2.py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8d7a7a6be12587cc5da32a587067506b47b849f4643981099ad148015a72de52 |
|
MD5 | fe556d5be429c339c74df843874995bf |
|
BLAKE2b-256 | 67996612dcf7d494223041c029cc4fa325cb513fe99bf989e6895a1de357f1eb |