阿拉将要自己新建一个项目,来show 一下 oslo config的用法。我用的环境是:配有virtualenv的ubuntu系统。
首先让我们创建一个简单的项目:
$ virtualenv example-app
$ cd example-app
$ source bin/activate
$ pip install oslo.config
$ touch app.py
$ touch app.conf
|
pip 安装 oslo.config 并且创建两个python文件:
app.py ----我们的主代码
app.conf----用来保存我们的一些配置
首先,在app.conf里面放个吧参数:
[simple]
enable = True
|
from
__future__
import
print_function
from
oslo.config
import
cfg
opt_group
=
cfg.OptGroup(name
=
'simple'
,
title
=
'A Simple Example'
)
simple_opts
=
[
cfg.BoolOpt(
'enable'
, default
=
False
,
help
=
(
'True enables, False disables'
))
]
CONF
=
cfg.CONF
CONF.register_group(opt_group)
CONF.register_opts(simple_opts, opt_group)
if
__name__
=
=
"__main__"
:
CONF(default_config_files
=
[
'app.conf'
])
print
(CONF.simple.enable)
|
观察一下代码,你可以注意到,首先呢设置opt_group和simple_opts的变量,之后再设置一下simple_opts列表来保存config 文件的enable变量的值。最后一步是注册对应的组别。需要强调一下,你可以同时使用多个配置文件。例子如下:
CONF(default_config_files
=
[
'app.conf'
,
'mything.conf'
])
|
实际应用中,你还可能更加经常地用到如下的形式:
CONF(default_config_files
=
[
'/etc/example-app/app.conf'
])
|
运行这个程序,程序会打出enable的值为True。:)
继续编辑 app.conf的内容:
enable = False
|
再次运行程序,你将得到False.
要走就走得远!让我们更加深入一些,咱再往app.conf里面添加一些乱七八糟的数据:
[simple]
enable = True
[morestuff]
# StrOpt
message = Hello World
# ListOpt
usernames = ['Joe', 'Jessica', 'Peter']
# DictOpt
usermetadata = {'Joe': 'Manager', 'Jessica': 'CEO', 'Peter': 'Security Guard'}
# IntOpt
payday = 20
# FloatOpt
pi = 3.14
|
这里呢,我们添加了很多各种奇奇怪怪的数据类型: StrOpt, ListOpt, DictOpt, IntOp, FloatOpt。
修改 app.py 的代码以获取更多的值:
from
__future__
import
print_function
from
oslo.config
import
cfg
opt_simple_group
=
cfg.OptGroup(name
=
'simple'
,
title
=
'A Simple Example'
)
opt_morestuff_group
=
cfg.OptGroup(name
=
'morestuff'
,
title
=
'A More Complex Example'
)
simple_opts
=
[
cfg.BoolOpt(
'enable'
, default
=
False
,
help
=
(
'True enables, False disables'
))
]
morestuff_opts
=
[
cfg.StrOpt(
'message'
, default
=
'No data'
,
help
=
(
'A message'
)),
cfg.ListOpt(
'usernames'
, default
=
None
,
help
=
(
'A list of usernames'
)),
cfg.DictOpt(
'jobtitles'
, default
=
None
,
help
=
(
'A dictionary of usernames and job titles'
)),
cfg.IntOpt(
'payday'
, default
=
30
,
help
=
(
'Default payday monthly date'
)),
cfg.FloatOpt(
'pi'
, default
=
0.0
,
help
=
(
'The value of Pi'
))
]
CONF
=
cfg.CONF
CONF.register_group(opt_simple_group)
CONF.register_opts(simple_opts, opt_simple_group)
CONF.register_group(opt_morestuff_group)
CONF.register_opts(morestuff_opts, opt_morestuff_group)
if
__name__
=
=
"__main__"
:
CONF(default_config_files
=
[
'app.conf'
])
print
(
'(simple) enable: {}'
.
format
(CONF.simple.enable))
print
(
'(morestuff) message :{}'
.
format
(CONF.morestuff.message))
print
(
'(morestuff) usernames: {}'
.
format
(CONF.morestuff.usernames))
print
(
'(morestuff) jobtitles: {}'
.
format
(CONF.morestuff.jobtitles))
print
(
'(morestuff) payday: {}'
.
format
(CONF.morestuff.payday))
print
(
'(morestuff) pi: {}'
.
format
(CONF.morestuff.pi))
|
现在呢,当咱run起来这个程序的时候,得到的结果应该是兹样子的:
(simple) enable: True
(morestuff) message :Hello World
(morestuff) usernames: ["['Joe'", "'Jessica'", "'Peter']“]
(morestuff) jobtitles: {“{‘Joe’”: “‘Manager’”, “‘Peter’”: “‘Security Guard’}”, “‘Jessica’”: “‘CEO’”}
(morestuff) payday: 20
(morestuff) pi: 3.14
当然喽,还有一大堆乱七八糟的数据类型你还可以用用: MultiStrOpt, SubCommandOpt 等等喽。反正我是用不到滴。
翻得比较挫,请多多见谅。