配置管理器
项目描述
配置解析器。
安装
$ pip install kaptan
也适用于FreeBSD、Debian、Arch Linux和Slackware上的软件包。
用法
支持的处理程序
dict
json
yaml
.ini
python文件
默认(dict)处理程序
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处理程序
config = kaptan.Kaptan(handler="json")
config.import_config('{"everything": 42}')
print config.get("everything")
# output: 42
yaml处理程序
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
或者您可以直接从文件名获取
config.import_config("configuration.yaml")
.ini处理程序
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
文件处理程序
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
导出配置
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")
输出:
debug: false
environment: DEV
pagination: {limit: 20, per_page: 10}
redis_uri: redis://localhost:6379/0
print config.export("json")
输出无缩进的json。 .export接受kwargs,并将其传递到json.dumps。
print config.export("json", indent=4)
输出:
{
"environment": "DEV",
"debug": false,
"pagination": {
"per_page": 10,
"limit": 20
},
"redis_uri": "redis://localhost:6379/0"
}
config.export('yaml')也支持pyyaml的kwargs。
0.5.7版本新增: config.export('yaml', safe=True) 将使用 .safe_dump。
命令行界面
导出(默认为json)
$ echo "environment: DEV" > config.yaml
$ kaptan config.yaml --export json > config.json
$ cat config.json
{"environment": "DEV"}
获取值
$ kaptan config.yaml --key environment
DEV
指定处理程序
$ mv config.yaml config.settings
$ kaptan config.settings:yaml --export json
{"environment": "DEV"}
从标准输入配置
$ echo '{"source": "stdin"}' | kaptan -
{"source": "stdin"}
$ echo 'source: stdin' | kaptan -:yaml
{"source": "stdin"}
合并配置
$ echo "environment: PROD" > config.settings
$ echo '{"source": "stdin"}' | kaptan - config.json config.settings:yaml
{"environment": "PROD", "source": "stdin"}
设置默认处理程序
$ echo "source: stdin" | kaptan --handler yaml - config.settings
{"environment": "PROD", "source": "stdin"}
使用YAML写入JSON
$ kaptan -:yaml -e json
<type yaml here>
<Ctrl + D>
<get json here>
运行测试
使用 py.test
$ py.test
贡献者
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分布
kaptan-0.6.0.tar.gz (11.9 kB 查看散列)
构建分布
kaptan-0.6.0-py3-none-any.whl (11.0 kB 查看散列)