diff options
author | Eric Brown <browne@vmware.com> | 2015-06-29 22:53:48 -0700 |
---|---|---|
committer | Eric Brown <browne@vmware.com> | 2015-07-07 00:12:11 -0700 |
commit | 366ca45ef2f46b4a3eb50b283b9b745242aa4bf9 (patch) | |
tree | fb33b72b993e118cceb5208af8da55427e0e2580 | |
parent | ac7c55dee3d5b27d45ab31456d5182902c95164b (diff) | |
download | oslo-config-366ca45ef2f46b4a3eb50b283b9b745242aa4bf9.tar.gz |
Expose min and max to IntOpt
The IntOpt class utilizes the types.Integer class which has
parameters for setting the minimum and maximum value. These min
and max values are not exposed through IntOpt and should be ideally.
In the docstring help for cfg.py, it gives an example of creating a
PortType instance of types.Integer to create a new type with a range.
Rather than projects having to define this new instance, seems better
to just expose the min and max to IntOpt.
Another advantage of adding min and max to IntOpt is the ability to
generate a useful sample config file that displays the range of
valid integer values.
Change-Id: Icce7b6799061711ea512d60facc57bf7d6f6c9cc
-rw-r--r-- | oslo_config/cfg.py | 5 | ||||
-rw-r--r-- | oslo_config/generator.py | 6 | ||||
-rw-r--r-- | oslo_config/tests/test_cfg.py | 13 | ||||
-rw-r--r-- | oslo_config/tests/test_generator.py | 4 | ||||
-rw-r--r-- | tests/test_cfg.py | 13 | ||||
-rw-r--r-- | tests/test_generator.py | 4 |
6 files changed, 43 insertions, 2 deletions
diff --git a/oslo_config/cfg.py b/oslo_config/cfg.py index 44e6942..5227124 100644 --- a/oslo_config/cfg.py +++ b/oslo_config/cfg.py @@ -1029,8 +1029,9 @@ class IntOpt(Opt): `Kept for backward-compatibility with options not using Opt directly`. """ - def __init__(self, name, **kwargs): - super(IntOpt, self).__init__(name, type=types.Integer(), **kwargs) + def __init__(self, name, min=None, max=None, **kwargs): + super(IntOpt, self).__init__(name, type=types.Integer(min, max), + **kwargs) class FloatOpt(Opt): diff --git a/oslo_config/generator.py b/oslo_config/generator.py index 3e2a2b0..e39bc88 100644 --- a/oslo_config/generator.py +++ b/oslo_config/generator.py @@ -120,6 +120,12 @@ class _OptFormatter(object): help_text = u'(%s)' % opt_type lines = self._format_help(help_text) + if getattr(opt.type, 'min', None): + lines.append('# Minimum value: %d\n' % opt.type.min) + + if getattr(opt.type, 'max', None): + lines.append('# Maximum value: %d\n' % opt.type.max) + if getattr(opt.type, 'choices', None): choices_text = ', '.join([self._get_choice_text(choice) for choice in opt.type.choices]) diff --git a/oslo_config/tests/test_cfg.py b/oslo_config/tests/test_cfg.py index 2bd3265..28226e7 100644 --- a/oslo_config/tests/test_cfg.py +++ b/oslo_config/tests/test_cfg.py @@ -977,6 +977,19 @@ class ConfigFileOptsTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.conf.foo, 666) + def test_conf_file_int_min_max(self): + self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5)) + + paths = self.create_tempfiles([('test', + '[DEFAULT]\n' + 'foo = 10\n')]) + + self.conf(['--config-file', paths[0]]) + self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') + + def test_conf_file_int_min_greater_max(self): + self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1) + def test_conf_file_int_use_dname(self): self._do_dname_test_use(cfg.IntOpt, '66', 66) diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py index 42962c0..262a054 100644 --- a/oslo_config/tests/test_generator.py +++ b/oslo_config/tests/test_generator.py @@ -79,6 +79,8 @@ class GeneratorTestCase(base.BaseTestCase): help='a boolean'), 'int_opt': cfg.IntOpt('int_opt', default=10, + min=1, + max=20, help='an integer'), 'float_opt': cfg.FloatOpt('float_opt', default=0.1, @@ -400,6 +402,8 @@ class GeneratorTestCase(base.BaseTestCase): # # an integer (integer value) +# Minimum value: 1 +# Maximum value: 20 #int_opt = 10 ''')), ('float_opt', diff --git a/tests/test_cfg.py b/tests/test_cfg.py index 5d85e1c..6b65788 100644 --- a/tests/test_cfg.py +++ b/tests/test_cfg.py @@ -982,6 +982,19 @@ class ConfigFileOptsTestCase(BaseTestCase): self.assertTrue(hasattr(self.conf, 'foo')) self.assertEqual(self.conf.foo, 666) + def test_conf_file_int_min_max(self): + self.conf.register_opt(cfg.IntOpt('foo', min=1, max=5)) + + paths = self.create_tempfiles([('test', + '[DEFAULT]\n' + 'foo = 10\n')]) + + self.conf(['--config-file', paths[0]]) + self.assertRaises(cfg.ConfigFileValueError, self.conf._get, 'foo') + + def test_conf_file_int_min_greater_max(self): + self.assertRaises(ValueError, cfg.IntOpt, 'foo', min=5, max=1) + def test_conf_file_int_use_dname(self): self._do_dname_test_use(cfg.IntOpt, '66', 66) diff --git a/tests/test_generator.py b/tests/test_generator.py index 35b22d8..60b71a6 100644 --- a/tests/test_generator.py +++ b/tests/test_generator.py @@ -81,6 +81,8 @@ class GeneratorTestCase(base.BaseTestCase): help='a boolean'), 'int_opt': cfg.IntOpt('int_opt', default=10, + min=1, + max=20, help='an integer'), 'float_opt': cfg.FloatOpt('float_opt', default=0.1, @@ -413,6 +415,8 @@ class GeneratorTestCase(base.BaseTestCase): # # an integer (integer value) +# Minimum value: 1 +# Maximum value: 20 #int_opt = 10 ''')), ('float_opt', |