Skip to main content

A configuration library for python using trafaret and yaml

Project description

Status:
Stable
Documentation:

http://trafaret-config.readthedocs.io/

This is a wrapper that loads yaml and checks config using trafaret while keeping track of actual lines of file where error has happened. Additionally, it can pretty print the error.

Basic Usage:

import argparse
import trafaret
from trafaret_config import commandline

TRAFARET = trafaret.Dict({'x': trafaret.String()})

def main():
    ap = argparse.ArgumentParser()
    commandline.standard_argparse_options(ap, default_config='config.yaml')
    #
    # define your command-line arguments here
    #
    options = ap.parse_args()
    config = commandline.config_from_options(options, TRAFARET)
    pprint.pprint(config)

Example output when there is an error in config (from a example.py which has better trafaret than example above):

bad.yaml:3: smtp.port: value can't be converted to int
  -> 'unknown'
bad.yaml:4: smtp.ssl_port: value can't be converted to int
  -> 'NaN'
bad.yaml:5: port: value can't be converted to int
  -> '???'

Help looks like this:

usage: example.py [-h] [-c CONFIG] [--print-config] [--print-config-vars] [-C]

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        Configuration file (default: 'config.yaml')
  --print-config        Print config as it is read after parsing and exit
  --print-config-vars   Print variables used in configuration file
  -C, --check-config    Check configuration and exit

Since trafaret-config 2.0 environment variables in the config are replaced by default, this means that config like this:

url: http://${HOST}:$PORT/

Will get HOST and PORT variables insert from the environment, and if variable does not exist, you will get the following error:

config.yaml:2: variable 'PORT' not found
  -> 'http://${HOST}:$PORT/'

Low-level interface, without relying on argparse:

import sys
import trafaret
from trafaret_config import ConfigError, read_and_validate

TRAFARET = trafaret.Dict({'x': trafaret.String()})

try:
    config = read_and_validate('config.yaml', TRAFARET)
except ConfigError as e:
    e.output()
    sys.exit(1)

Installation

pip install trafaret-config==2.0.2

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

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

trafaret-config-2.0.2.tar.gz (6.2 kB view details)

Uploaded Source

File details

Details for the file trafaret-config-2.0.2.tar.gz.

File metadata

  • Download URL: trafaret-config-2.0.2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.3

File hashes

Hashes for trafaret-config-2.0.2.tar.gz
Algorithm Hash digest
SHA256 440b6b49e5e975f9a640a2519abb2feddd96eb2aeb1715f87f947a7a079f20be
MD5 b5991b1753e605d786cfecb03b087908
BLAKE2b-256 086e4072cf1e3e04b703911c36603c08efc030d0b1b19bf2ad9c1e0fff691097

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page