oslo_config配置解析模块

简介

oslo_config模块是openstack中使用的配置文件和命令行参数解析模块,可以很方便的解析命令行(CLI)或者配置文件。


我们经常能看到如下的配置文件格式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# this is demo app.conf
# this is comments
[network]
enable = True
ip = '127.0.0.1'
port = 8080
host = tyr.gift
[user]
userlist = ['user1', 'user2', 'user3']
paradict = {'name': 'jason', 'sex': 'male', 'age':23}
description = tyr.gift
pi = 3.14
num = 3

这样的配置文件格式就可以使用oslo_config模块来解析。

app.conf

我们以如下的app.conf为例,来解析这个配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# this is demo app.conf
# this is comments
# group
[network]
# BoolOpt
enable = True
# IPOpt
ip = '127.0.0.1'
# PortOpt
port = 8080
# HostnameOp
host = www.tyr.gift
[user]
# ListOpt
userlist = ['user1', 'user2', 'user3']
# DictOpt
paradict = {'name': 'jason', 'sex': 'male', 'age':23}
# StrOpt
description = hello,world
# FloatOpt
pi = 3.14
# IntOpt
num = 3

app.py

demo程序如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from oslo_config import cfg
network_optgroup = cfg.OptGroup(name='network', title='network settings')
user_optgroup = cfg.OptGroup(name='user', title='user settings')
network_opts = [
cfg.BoolOpt('enable', default=False, help='enable or not'),
cfg.IPOpt('ip', default='127.0.0.1', help='listen ip address'),
cfg.PortOpt('port', default=8080, help='listen port'),
cfg.HostnameOpt('host', default='tyr.gift', help='listen hostname')
]
user_opts = [
cfg.ListOpt('userlist', default=[], help='user list'),
cfg.DictOpt('paradict', default={}, help='para dict'),
cfg.StrOpt('description', default='', help='user description'),
cfg.FloatOpt('pi', default=0.0, help='pi'),
cfg.IntOpt('num', default=0, help='num')
]
CONF = cfg.CONF
CONF.register_group(network_optgroup)
CONF.register_opts(network_opts, network_optgroup)
# 注册到cli参数中
CONF.register_cli_opts(network_opts, network_optgroup)
CONF.register_group(user_optgroup)
CONF.register_opts(user_opts, user_optgroup)
CONF.register_cli_opts(user_opts, user_optgroup)
if __name__ == '__main__':
CONF(default_config_files=['app.conf'])
print CONF.network.enable
print CONF.network.ip
print CONF.network.port
print CONF.network.host
print CONF.user.userlist
print CONF.user.paradict
print CONF.user.description
print CONF.user.pi
print CONF.user.num

注册到cli参数后,也可以在命令行中接受参数,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(all) DoDoMacBook:config tyr$ python app.py --help
usage: app [-h] [--config-dir DIR] [--config-file PATH] [--network-enable]
[--network-host NETWORK_HOST] [--network-ip NETWORK_IP]
[--network-noenable] [--network-port NETWORK_PORT]
[--user-description USER_DESCRIPTION] [--user-num USER_NUM]
[--user-paradict USER_PARADICT] [--user-pi USER_PI]
[--user-userlist USER_USERLIST]
optional arguments:
-h, --help show this help message and exit
--config-dir DIR Path to a config directory to pull *.conf files from.
This file set is sorted, so as to provide a
predictable parse order if individual options are
over-ridden. The set is parsed after the file(s)
specified via previous --config-file, arguments hence
over-ridden options in the directory take precedence.
--config-file PATH Path to a config file to use. Multiple config files
can be specified, with values in later files taking
precedence. Defaults to None.
user settings:
--user-description USER_DESCRIPTION
user description
--user-num USER_NUM num
--user-paradict USER_PARADICT
para dict
--user-pi USER_PI pi
--user-userlist USER_USERLIST
user list
network settings:
--network-enable enable or not
--network-host NETWORK_HOST
listen hostname
--network-ip NETWORK_IP
listen ip address
--network-noenable The inverse of --enable
--network-port NETWORK_PORT
listen port

在oslo_config.cfg中定义了很多参数类型,用于描述和约束参数的数据结构,具体如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
==================================== ======
Type Option
==================================== ======
:class:`oslo_config.types.String` - :class:`oslo_config.cfg.StrOpt`
- :class:`oslo_config.cfg.SubCommandOpt`
:class:`oslo_config.types.Boolean` :class:`oslo_config.cfg.BoolOpt`
:class:`oslo_config.types.Integer` :class:`oslo_config.cfg.IntOpt`
:class:`oslo_config.types.Float` :class:`oslo_config.cfg.FloatOpt`
:class:`oslo_config.types.Port` :class:`oslo_config.cfg.PortOpt`
:class:`oslo_config.types.List` :class:`oslo_config.cfg.ListOpt`
:class:`oslo_config.types.Dict` :class:`oslo_config.cfg.DictOpt`
:class:`oslo_config.types.IPAddress` :class:`oslo_config.cfg.IPOpt`
:class:`oslo_config.types.Hostname` :class:`oslo_config.cfg.HostnameOpt`
:class:`oslo_config.types.HostAddress`:class:`oslo_config.cfg.HostAddressOpt`
:class:`oslo_config.types.URI` :class:`oslo_config.cfg.URIOpt`
==================================== ======

如果需要对参数的取值范围做限制,我们可以定义自己的类型:

1
2
3
4
5
6
7
from oslo_config import cfg
from oslo_config import types
PortType = types.Integer(1, 1024)
network_opts = [
cfg.Opt('port', type=PortType, default=80, help='listen port')
]

当实际参数不满足限制条件时,程序会提示参数错误并退出。

oslo_config模块实际是使用了Python的标准库argparse来进行命令行参数处理。
更详细的信息请参考 Python中的命令行解析工具介绍

如果您觉得这篇文章对您有帮助,不妨支持我一下!