GithubHelp home page GithubHelp logo

phipleg / trafaret-config Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tailhook/trafaret-config

0.0 1.0 0.0 75 KB

A small wrapper around trafaret and yaml that does nice error reporting (python)

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

License: Apache License 2.0

Python 100.00%

trafaret-config's Introduction

Trafaret Config

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.

trafaret-config's People

Contributors

asvetlov avatar kataev avatar tailhook avatar vlcinsky avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.