Skip to main content

Simple YAML configuration file parser with easy access for structured data

Project description

EnvYAML | Latest Version Build Status Coverage Status Versions License

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

    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

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 prevent from declaring variables that not exist in environment variables or .env file. This leads to have runtime ValueError exception when variables not define with message Strict mode enabled, variable $VAR not defined!. To disable strict mode specify strict=False to EnvYAML object

License

MIT licensed. See the LICENSE file for more details.

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

envyaml-1.1.201201.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

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

envyaml-1.1.201201-py2.py3-none-any.whl (9.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file envyaml-1.1.201201.tar.gz.

File metadata

  • Download URL: envyaml-1.1.201201.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.0

File hashes

Hashes for envyaml-1.1.201201.tar.gz
Algorithm Hash digest
SHA256 423ff41a9150e0b190d6f1ddd1ab1db299aa6dbf68a903e1e2d297c78952c68c
MD5 f098c329f0a6ba108d567f52982f48a7
BLAKE2b-256 5afb2ae943da2148eafc0407f5aca30c5a59dd3ce2a0cdfee88381a0038cd6a4

See more details on using hashes here.

File details

Details for the file envyaml-1.1.201201-py2.py3-none-any.whl.

File metadata

  • Download URL: envyaml-1.1.201201-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.8.0

File hashes

Hashes for envyaml-1.1.201201-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4160f4efd28eefde3e2b167c40a3753138537d877f2ddc1f26b3a4b84d8b2648
MD5 63681efc43e49fe098a210ef00cc8e43
BLAKE2b-256 08e41e304f26a0d8f7904efe2f49a009b58b5c317a9bd5ee9d35115b835d60ef

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