Code Monkey home page Code Monkey logo

kaptan's Introduction

kaptan

Python Package Documentation Status Build Status License bitdeli badge

configuration parser.

installation

$ pip install kaptan

FreeBSD port

devel/py-kaptan package.

  • To install the port: cd /usr/ports/devel/py-kaptan/ && make install clean
  • To add the package: pkg install devel/py-kaptan

usage

supported handlers

  • dict
  • json
  • yaml
  • .ini
  • python file

default (dict) handler

config = kaptan.Kaptan()
config.import_config({
    'environment': 'DEV',
    'redis_uri': 'redis://localhost:6379/0',
    'debug': False,
    'pagination': {
        'per_page': 10,
        'limit': 20,
    }
})

print config.get("pagination.limit")

# output: 20

json handler

config = kaptan.Kaptan(handler="json")
config.import_config('{"everything": 42}')

print config.get("everything")
# output: 42

yaml handler

config = kaptan.Kaptan(handler="yaml")
config.import_config("""
product:
  price:
    value: 12.65
    currency_list:
      1. TL
      2. EURO
""")
print config.get("product.price.currency_list.0")
# output: TL

or you can get from directly from the filename:

config.import_config("configuration.yaml")

.ini handler

config.ini

[development]
database_uri = mysql://root:123456@localhost/posts

[production]
database_uri = mysql://poor_user:poor_password@localhost/poor_posts
config = kaptan.Kaptan(handler="ini")
config.import_config('config.ini')

print config.get("production.database_uri")
# output: mysql://poor_user:poor_password@localhost/poor_posts

file handler

config.py

DATABASE = 'mysql://root:123456@localhost/posts'
DEBUG = False
PAGINATION = {
    'per_page': 10,
    'limit': 20,
}
config = kaptan.Kaptan(handler="file")
config.import_config('config')

print config.get("DEBUG")
# output: False

exporting configuration

config = kaptan.Kaptan(handler="file")
config.import_config({
    'environment': 'DEV',
    'redis_uri': 'redis://localhost:6379/0',
    'debug': False,
    'pagination': {
        'per_page': 10,
        'limit': 20,
    }
})

print config.export("yaml")

output:

debug: false
environment: DEV
pagination: {limit: 20, per_page: 10}
redis_uri: redis://localhost:6379/0

print config.export("json")

outputs unindented json. .export accepts kwargs which pass into json.dumps.

print config.export("json", indent=4)

output:

{
    "environment": "DEV",
    "debug": false,
    "pagination": {
        "per_page": 10,
        "limit": 20
    },
    "redis_uri": "redis://localhost:6379/0"
}

config.export('yaml') also supports the kwargs for pyyaml.

New in Version 0.5.7: config.export('yaml', safe=True) will use .safe_dump.

cli

exporting (defaults to json)

$ echo "environment: DEV" > config.yaml
$ kaptan config.yaml --export json > config.json
$ cat config.json
{"environment": "DEV"}

getting a value

$ kaptan config.yaml --key environment
DEV

specifying the handler

$ mv config.yaml config.settings
$ kaptan config.settings:yaml --export json
{"environment": "DEV"}

config from stdin

$ echo '{"source": "stdin"}' | kaptan -
{"source": "stdin"}
$ echo 'source: stdin' | kaptan -:yaml
{"source": "stdin"}

merging configs

$ echo "environment: PROD" > config.settings
$ echo '{"source": "stdin"}' | kaptan - config.json config.settings:yaml
{"environment": "PROD", "source": "stdin"}

setting default handler

$ echo "source: stdin" | kaptan --handler yaml - config.settings
{"environment": "PROD", "source": "stdin"}

writing json with yaml

$ kaptan -:yaml -e json
<type yaml here>
<Ctrl + D>
<get json here>

running tests

With py.test:

$ py.test

contributors

kaptan's People

Contributors

tony avatar emre avatar berkerpeksag avatar alexsavio avatar marksteve avatar cenkalti avatar bitdeli-chef avatar s avatar wessie avatar

Watchers

James Cloos avatar  avatar Borja Ayerdi avatar  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.