Simple YAML configuration file parser with easy access for structured data
Project description
EnvYAML |
Simple YAML configuration file parser with easy access for structured data
Why
Modern configuration file become to be more and more complex, flexible and readable. YAML file format are perfect to store configuration, but had no option to pass environment variables. They give flexibility, readability and provide option to store complex data structure. This project aim to simplify usage of the YAML file and environment variables as program configuration file 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
redis:
host: $REDIS_HOST
port: 5040
config:
expire: 300
prefix: $REDIS_PREFIX
empty_env: $NOT_EXIST_ENV_VARIABLE
and 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
# access list items by number
print(env['list_test'][0])
# >> one
# access list items by number as key
print(env['list_test.1'])
# >> two
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
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
Hashes for envyaml-0.1912-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a49c867e1c3da512c84cd5bccb42feed35a0e790a17dc3d430e5e0c640c1eb68 |
|
MD5 | c8d3233ac63e78a15e250d81f6610a16 |
|
BLAKE2b-256 | cdbbaf3f8f93d42b3c82b6a18980f00af026ea384870d8b9e7f1ababaf7529f7 |