Repository navigation
Expand file tree
/
Copy pathconfig_test.py
More file actions
135 lines (112 loc) · 3.37 KB
/
Copy pathconfig_test.py
File metadata and controls
135 lines (112 loc) · 3.37 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 17-8-31 上午10:40
# @Author : Tom.Lee
# @File : config.py
# @Product : PyCharm
# @Docs :
# @Source : https://github.com/openstack/oslo.config/blob/master/oslo_config/cfg.py
"""
配置文件中的选项(group, opts),必须在代码中显示的注册,否则无法解析
"""
from oslo_config import cfg
from oslo_config import types
# 端口规范
PortType = types.Integer(1, 65535)
# 多个配置项组成一个模式
default_opts = [
cfg.StrOpt('bind_host',
default='0.0.0.0',
help='IP address to listen on.'),
cfg.Opt('bind_port', # 只有Opt类型才能指定PortType
type=PortType,
default=9292,
help='Port number to listen on.')
]
# 单个配置项模式
default_opt = cfg.ListOpt('enabled_api',
default=['ec2', 'api_compute'],
help='List of APIs to enable by default.')
# 命令行选项
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'),
]
# 配置 rabbit_group 组
rabbit_group = cfg.OptGroup(
name='RABBIT',
title='RABBIT options'
)
# 配置组中的模式,通常以配置组的名称为前缀(非必须)
rabbit_opt = cfg.BoolOpt('ssl',
default=False,
help='use ssl for connection')
# 配置组中的多配置项模式
rabbit_opts = [
cfg.StrOpt('host',
default='localhost',
help='IP/hostname to listen on.'),
cfg.IntOpt('port',
default=5672,
help='Port number to listen on.')
]
def register_default_opts(conf):
"""
注册默认组的配置项
"""
conf.register_opt(default_opt)
conf.register_opts(default_opts)
def register_rabbit_group(conf):
"""
注册 rabbit 信息
"""
# 配置组必须在其组件被注册前注册!
conf.register_group(rabbit_group)
# 注册配置组中含有多个配置项的模式,必须指明配置组
conf.register_opts(rabbit_opts, rabbit_group)
# 注册配置组中的单配置项模式,指明配置组
conf.register_opt(rabbit_opt, rabbit_group)
def register_cli_opts(conf):
"""
注册 cli 选项
:param conf:
:return:
"""
conf.register_cli_opts(cli_opts)
def get_bind_host(conf):
"""
使用选项 bind_host
"""
return getattr(conf, 'bind_host', None)
def get_bind_port(conf):
"""
使用选项 bind_port
"""
return conf.bind_port
def get_rabbit_username(conf):
"""
配置文件中存在,代码没有注册,不能解析
"""
return conf.RABBIT.username
if __name__ == '__main__':
# 创建配置类
config = cfg.CONF
# 开始注册default
register_default_opts(config)
register_rabbit_group(config)
register_cli_opts(config)
# 加载配置文件
config(default_config_files=['config.conf'])
print 'host:', get_bind_host(config)
# list_all_sections
for section in config.list_all_sections():
print section
print config.RABBIT
print config.RABBIT.host
print get_rabbit_username(config)