From a69701c495947a2bbe618e71e20d6203977a6943 Mon Sep 17 00:00:00 2001 From: Brian Rosmaita Date: Wed, 11 Apr 2018 22:46:33 -0400 Subject: [PATCH] Make List option format bounds-sensitive The List ConfigType has a boolean 'bounds' option that indicates whether the value must be enclosed in brackets. This patch modifies the List formatter so that when bounds=True the output is displayed in enclosing brackets. Change-Id: Ide3748e67d14191b6b33c97c7d8437e4888e61d5 Closes-bug: #1763239 --- oslo_config/tests/test_generator.py | 15 +++++++++++++++ oslo_config/types.py | 5 +++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index e01bbab3..8575d8e2 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -151,6 +151,10 @@ class GeneratorTestCase(base.BaseTestCase): 'list_opt': cfg.ListOpt('list_opt', default=['1', '2', '3'], help='a list'), + 'list_opt_with_bounds': cfg.ListOpt('list_opt_with_bounds', + default=['1', '2', '3'], + help='a list', + bounds=True), 'list_opt_single': cfg.ListOpt('list_opt_single', default='1', help='a list'), @@ -630,6 +634,17 @@ class GeneratorTestCase(base.BaseTestCase): # a list (list value) #list_opt = 1,2,3 +''')), + ('list_opt_with_bounds', + dict(opts=[('test', [(None, [opts['list_opt_with_bounds']])])], + expected='''[DEFAULT] + +# +# From test +# + +# a list (list value) +#list_opt_with_bounds = [1,2,3] ''')), ('list_opt_single', dict(opts=[('test', [(None, [opts['list_opt_single']])])], diff --git a/oslo_config/types.py b/oslo_config/types.py index 752fe1a8..06a81dde 100644 --- a/oslo_config/types.py +++ b/oslo_config/types.py @@ -525,14 +525,15 @@ class List(ConfigType): ) def _formatter(self, value): + fmtstr = '[{}]' if self.bounds else '{}' if isinstance(value, six.string_types): - return value + return fmtstr.format(value) if isinstance(value, list): value = [ six.text_type(v) for v in value ] - return ','.join(value) + return fmtstr.format(','.join(value)) class Range(ConfigType):