我刚刚开始使用水泥作为python框架。似乎应用程序的默认配置不是JSON。
似乎Cement有JsonConfigHandler()
类可以将JSON配置加载到app中。我在我的应用中使用了以下代码:
ins = JsonConfigHandler()
ins.parse_file('/etc/luna/luna.conf')
但它给出了以下错误:
return self._parse_file(file_path)
File "/Library/Python/2.7/site-packages/cement/ext/ext_json.py", line 243, in _parse_file
self.merge(self._json.load(open(file_path)))
AttributeError: 'NoneType' object has no attribute 'load'
我应该在水泥应用程序中加载JSON配置文件吗?
默认情况下,我使用app.config.parse_file('/etc/my_app/app.conf')
加载配置没有问题,配置文件包含:
[connection]
host=172.16.131.12
答案 0 :(得分:1)
JSON文件/etc/luna/luna.conf
:
{
"connection": {
"host": "172.16.131.12"
}
}
的Python:
from cement.core.foundation import CementApp
from cement.ext.ext_json import JsonConfigHandler
app = CementApp('test', config_handler=JsonConfigHandler,
config_files=['/etc/luna/luna.conf'])
app.setup()
print(app.config.get('connection', 'host'))
输出172.16.131.12
旧答案,请忽略
不知道库,但查看代码看起来你必须先调用_setup()
方法:
ins = JsonConfigHandler()
ins._setup(app)
ins.parse_file('/etc/luna/luna.conf')
答案 1 :(得分:1)
道歉并不是最重要的,但这是官方回应(主要开发人员):
您只需要添加json
扩展名,然后将CementApp.Meta.config_handler
设置为json
:
from cement.core.foundation import CementApp
class MyApp(CementApp):
class Meta:
label = 'myapp'
extensions = ['json']
config_handler = 'json'
config_extension = '.json'
这将保持默认config_files
列表不变,但对其查找的文件使用.json
而不是.conf
。