configuration parser.
installation
$ pip install kaptan
Also available as a package on FreeBSD, Debian, Arch Linux and Slackware.
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
see more at https://github.com/emre/kaptan/graphs/contributors.
Metadata
Release files for kaptan 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| kaptan-0.6.0.tar.gz | 11.9 kB | Without provenance | Details for kaptan-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| kaptan-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for kaptan-0.6.0-py3-none-any.whl |
Total release size: 22.9 kB
Release files / kaptan-0.6.0.tar.gz
| Download URL | kaptan-0.6.0.tar.gz |
|---|---|
| Size | 11.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
101330a44fdede888586f3010bd145c0ec48a4806bc56429fa5487a6774021f8
|
|
BLAKE2b-256 checksum How to use checksums |
cac83569c90535df1b7c6079c75b7fd3f304d6ec8ee1626855d37ca137409ba2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Release files / kaptan-0.6.0-py3-none-any.whl
| Download URL | kaptan-0.6.0-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
05b6a3d9eb4c4e53173519491cf32847a56d923226145938f1ffce547e4eac27
|
|
BLAKE2b-256 checksum How to use checksums |
77264e7fa0e03e8bac6fc489f11f4bddfcf0174f725bc19e72609219ab44ca1f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.11.5
|
Security for kaptan 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.