什么是oslo.config
oslo.config是openstack解析命令行(CLI)或者配置文件(.conf)中配置信息的库.官网地址点击打开链接
安装: $ sudo pip install oslo.config
cfg模块
每一个配置项都是一个Opt类或其子类例如:
from oslo_config import cfg
from oslo_config import types
PortType = types.Integer(1, 65535)
common_opts = [
cfg.StrOpt('bind_host',
default='0.0.0.0',
help='IP address to listen on.'),
cfg.Opt('bind_port',
type=PortType,
default=9292,
help='Port number to listen on.')
]
选项类型
选项可以有任意的类型,为了方便,在oslo.config.cfg中预先定义了选项类型的子类如下表
Type |
Option |
oslo_config.types.String |
- oslo_config.cfg.StrOpt
- oslo_config.cfg.SubComt
|
oslo_config.types.Boolean |
oslo_config.cfg.BoolOpt |
oslo_config.types.Integer |
- oslo_config.cfg.IntOpt
- oslo_config.cfg.PortOpt
|
oslo_config.types.Float |
oslo_config.cfg.FloatOpt |
oslo_config.types.List |
oslo_config.cfg.ListOpt |
oslo_config.types.Dict |
oslo_config.cfg.DictOpt |
oslo_config.types.IPAddress |
oslo_config.cfg.IPOpt |
oslo_config.types.Hostname |
oslo_config.cfg.HostnameOp |
cfg模块也支持具有多个值的选项,使用oslo_config.cfg.MultiOpt类来定义.功能感觉和 oslo_config.types.List类似.
注册选项
选项在使用前应该先注册,完善一下第一个例子:
from oslo_config import cfg
from oslo_config import types
PortType = types.Integer(1, 65535)
# 定义一组选项
common_opts = [
cfg.StrOpt('bind_host',
default='0.0.0.0',
help='IP address to listen on.'),
cfg.Opt('bind_port',
type=PortType,
default=9292,
help='Port number to listen on.')
]
def add_common_opts(conf): # 注册选项
conf.register_opts(common_opts)
def get_bind_host(conf): # 使用选项
return conf.bind_host
def get_bind_port(conf):
return conf.bind_port
# 创建配置类
cf = cfg.CONF
# 开始注册
add_common_opts(cf)
print(get_bind_host(cf))
通过命令行配置
cli_opts = [
cfg.BoolOpt('verbose',
short='v',
default=False,
help='Print more verbose output'),
cfg.BoolOpt('debug',
short='d',
default=False,
help='Print debugging output'),
]
def add_common_opts(conf):
conf.register_cli_opts(cli_opts)
cf = cfg.CONF
add_common_opts(cf)
运行时加-h选项可看到定义的东西已经加上
usage: config.py [-h] [--config-dir DIR] [--config-file PATH] [--debug]
[--nodebug] [--noverbose] [--verbose] [--version]
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.
--debug, -d Print debugging output.
--nodebug The inverse of --debug
--noverbose The inverse of --verbose
--verbose, -v Print more verbose output.
--version show program's version number and exit
注册选项组
#定义选项组
rabbit_group = cfg.OptGroup(name='rabbit',
title='RabbitMQ options')
#定义两个选项
rabbit_host_opt = cfg.StrOpt('host',
default='localhost',
help='IP/hostname to listen on'),
rabbit_port_opt = cfg.IntOpt('port',
default=5672,
help='Port number to listen on')
#注册
def register_rabbit_opts(conf):
conf.register_group(rabbit_group)
# options can be registered under a group in either of these ways:
#使用选项组的变量名注册
conf.register_opt(rabbit_host_opt, group=rabbit_group)
#使用选项组名注册
conf.register_opt(rabbit_port_opt, group='rabbit')
通过配置文件注册
配置文件config.conf
[DEFAULT]
bind_host = 127.0.0.1
bind_port = 8000
config.py
from oslo_config import cfg
from oslo_config import types
PortType = types.Integer(1, 65535)
# 定义一组选项
common_opts = [
cfg.StrOpt('bind_host',
default='0.0.0.0',
help='IP address to listen on.'),
cfg.Opt('bind_port',
type=PortType,
default=9292,
help='Port number to listen on.')
]
def add_common_opts(conf): # 注册选项
conf.register_opts(common_opts)
def get_bind_host(conf): # 使用选项
return conf.bind_host
def get_bind_port(conf):
return conf.bind_port
# 创建配置类
cf = cfg.CONF
# 开始注册
add_common_opts(cf)
cf(default_config_files=['config.conf'])
print(get_bind_host(cf))
print(get_bind_port(cf))
运行结果:
127.0.0.1
8000
成功的读取了配置文件的信息,若没有指定配置文件,则读取默认的配置信息.