如何在 cement python 应用程序中加载 JSON 配置文件?
How to load JSON config file in cement python app?
我刚刚开始使用 cement 作为 python 框架。该应用程序的默认配置似乎不是 JSON.
Cement 似乎有 JsonConfigHandler()
class 可以将 JSON 配置加载到应用程序中。我在我的应用程序中使用了以下代码:
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'
我应该如何在 cement 应用程序中加载 JSON 配置文件?
默认情况下,我使用 app.config.parse_file('/etc/my_app/app.conf')
加载配置没有问题,配置文件包含:
[connection]
host=172.16.131.12
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')
很抱歉没有在这上面,但这是官方回复(主要开发人员):
您只需包含 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
来查找文件。
我刚刚开始使用 cement 作为 python 框架。该应用程序的默认配置似乎不是 JSON.
Cement 似乎有 JsonConfigHandler()
class 可以将 JSON 配置加载到应用程序中。我在我的应用程序中使用了以下代码:
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'
我应该如何在 cement 应用程序中加载 JSON 配置文件?
默认情况下,我使用 app.config.parse_file('/etc/my_app/app.conf')
加载配置没有问题,配置文件包含:
[connection]
host=172.16.131.12
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')
很抱歉没有在这上面,但这是官方回复(主要开发人员):
您只需包含 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
来查找文件。