handle option defaults that are not strings

The sample config generator and sphinx integration are breaking when
option defaults are not strings. This shows up in cinder, so this bug
is preventing cinder from adopting the sphinx integration for showing
configuration options.

Fix the rendering in the generator, and in the type class for list
options.

Change-Id: Ib8a248b6dc695b6afe4f1e760af836ac664fa137
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-07-18 11:24:17 -04:00
parent 608a15ea61
commit 2d0ba09bd6
3 changed files with 37 additions and 2 deletions

View File

@ -113,7 +113,7 @@ def _format_defaults(opt):
default_str = str(opt.default)
elif isinstance(opt, (cfg.ListOpt, cfg._ConfigFileOpt,
cfg._ConfigDirOpt)):
default_str = ','.join(opt.default)
default_str = ','.join(six.text_type(d) for d in opt.default)
elif isinstance(opt, cfg.DictOpt):
sorted_items = sorted(opt.default.items(),
key=operator.itemgetter(0))
@ -125,7 +125,9 @@ def _format_defaults(opt):
results = []
for default_str in defaults:
if default_str.strip() != default_str:
if not isinstance(default_str, six.text_type):
default_str = six.text_type(default_str)
elif default_str.strip() != default_str:
default_str = '"%s"' % default_str
results.append(default_str)
return results

View File

@ -151,6 +151,12 @@ class GeneratorTestCase(base.BaseTestCase):
'list_opt': cfg.ListOpt('list_opt',
default=['1', '2', '3'],
help='a list'),
'list_opt_single': cfg.ListOpt('list_opt_single',
default='1',
help='a list'),
'list_int_opt': cfg.ListOpt('list_int_opt',
default=[1, 2, 3],
help='a list'),
'dict_opt': cfg.DictOpt('dict_opt',
default={'1': 'yes', '2': 'no'},
help='a dict'),
@ -619,6 +625,28 @@ class GeneratorTestCase(base.BaseTestCase):
# a list (list value)
#list_opt = 1,2,3
''')),
('list_opt_single',
dict(opts=[('test', [(None, [opts['list_opt_single']])])],
expected='''[DEFAULT]
#
# From test
#
# a list (list value)
#list_opt_single = 1
''')),
('list_int_opt',
dict(opts=[('test', [(None, [opts['list_int_opt']])])],
expected='''[DEFAULT]
#
# From test
#
# a list (list value)
#list_int_opt = 1,2,3
''')),
('dict_opt',
dict(opts=[('test', [(None, [opts['dict_opt']])])],

View File

@ -489,6 +489,11 @@ class List(ConfigType):
def _formatter(self, value):
if isinstance(value, six.string_types):
return value
if isinstance(value, list):
value = [
six.text_type(v)
for v in value
]
return ','.join(value)