diff options
author | Doug Hellmann <doug@doughellmann.com> | 2017-07-18 11:24:17 -0400 |
---|---|---|
committer | Doug Hellmann <doug@doughellmann.com> | 2017-07-18 13:49:25 -0400 |
commit | 2d0ba09bd685c5dc7c33ddead3ce4fc5a2ae5325 (patch) | |
tree | f11d081371ab7f2955aa3e6a6f4ab15737db34e5 | |
parent | 608a15ea61ae846c07624e869c16d37781c2a8ce (diff) | |
download | oslo-config-2d0ba09bd685c5dc7c33ddead3ce4fc5a2ae5325.tar.gz |
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>
-rw-r--r-- | oslo_config/generator.py | 6 | ||||
-rw-r--r-- | oslo_config/tests/test_generator.py | 28 | ||||
-rw-r--r-- | oslo_config/types.py | 5 |
3 files changed, 37 insertions, 2 deletions
diff --git a/oslo_config/generator.py b/oslo_config/generator.py index ba3d3cc..4b59ee9 100644 --- a/oslo_config/generator.py +++ b/oslo_config/generator.py @@ -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 diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index 8c7651e..015521e 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -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'), @@ -620,6 +626,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']])])], expected='''[DEFAULT] diff --git a/oslo_config/types.py b/oslo_config/types.py index c531e0c..976f497 100644 --- a/oslo_config/types.py +++ b/oslo_config/types.py @@ -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) |