summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJamie Lennox <jamielennox@gmail.com>2016-03-15 10:45:50 +1100
committerJamie Lennox <jamielennox@gmail.com>2016-05-16 09:12:36 +1000
commitdd983f1853f0e6347e7dab9607e96c5697ec1921 (patch)
tree8ad3c03a1cd1a074acf7a660b418efbb82e064b1
parent6d176243f398bf7e5d6bd8a5382945f2c5e318c3 (diff)
downloadoslo-config-dd983f1853f0e6347e7dab9607e96c5697ec1921.tar.gz
Handle some native python types in config generation
When generating sample config files if the option is not a oslo_config.Opt with a type_name value we simply put unknown value in the type help. In particularly the case of keystoneauth those types are typically standard python types like str. In cases like this where there is a limited number of well-known types oslo_config should do it's best to generate a useful type name rather than have unknowns. Change-Id: I5c66b50e1f7ab7d61bd818f4e56d376e14789a22
-rw-r--r--oslo_config/generator.py26
-rw-r--r--oslo_config/tests/test_generator.py49
2 files changed, 71 insertions, 4 deletions
diff --git a/oslo_config/generator.py b/oslo_config/generator.py
index ed66cf0..7787d47 100644
--- a/oslo_config/generator.py
+++ b/oslo_config/generator.py
@@ -105,6 +105,28 @@ def _format_defaults(opt):
return results
+_TYPE_NAMES = {
+ str: 'string value',
+ int: 'integer value',
+ float: 'floating point value',
+}
+
+
+def _format_type_name(opt_type):
+ """Format the type name to use in describing an option"""
+ try:
+ return opt_type.type_name
+ except AttributeError: # nosec
+ pass
+
+ try:
+ return _TYPE_NAMES[opt_type]
+ except KeyError: # nosec
+ pass
+
+ return 'unknown value'
+
+
class _OptFormatter(object):
"""Format configuration option descriptions to a file."""
@@ -168,9 +190,7 @@ class _OptFormatter(object):
if not opt.help:
LOG.warning('"%s" is missing a help string', opt.dest)
- option_type = getattr(opt, 'type', None)
- opt_type = getattr(option_type, 'type_name', 'unknown value')
-
+ opt_type = _format_type_name(opt.type)
opt_prefix = ''
if (opt.deprecated_for_removal and
not opt.help.startswith('DEPRECATED')):
diff --git a/oslo_config/tests/test_generator.py b/oslo_config/tests/test_generator.py
index ae3b36c..92d7067 100644
--- a/oslo_config/tests/test_generator.py
+++ b/oslo_config/tests/test_generator.py
@@ -29,6 +29,11 @@ from oslo_config import types
load_tests = testscenarios.load_tests_apply_scenarios
+def custom_type(a):
+ """Something that acts like a type, but isn't known"""
+ return a
+
+
class GeneratorTestCase(base.BaseTestCase):
groups = {
@@ -160,9 +165,18 @@ class GeneratorTestCase(base.BaseTestCase):
'string_type_with_bad_default': cfg.Opt('string_type_with_bad_default',
help='string with bad default',
default=4096),
+ 'native_str_type': cfg.Opt('native_str_type',
+ help='native help',
+ type=str),
+ 'native_int_type': cfg.Opt('native_int_type',
+ help='native help',
+ type=int),
+ 'native_float_type': cfg.Opt('native_float_type',
+ help='native help',
+ type=float),
'custom_type': cfg.Opt('custom_type',
help='custom help',
- type=type('string')),
+ type=custom_type),
'custom_type_name': cfg.Opt('custom_opt_type',
type=types.Integer(type_name='port'
' number'),
@@ -665,6 +679,39 @@ class GeneratorTestCase(base.BaseTestCase):
# a string (string value)
#str_opt = fooishbar
''')),
+ ('native_str_type',
+ dict(opts=[('test', [(None, [opts['native_str_type']])])],
+ expected='''[DEFAULT]
+
+#
+# From test
+#
+
+# native help (string value)
+#native_str_type = <None>
+''')),
+ ('native_int_type',
+ dict(opts=[('test', [(None, [opts['native_int_type']])])],
+ expected='''[DEFAULT]
+
+#
+# From test
+#
+
+# native help (integer value)
+#native_int_type = <None>
+''')),
+ ('native_float_type',
+ dict(opts=[('test', [(None, [opts['native_float_type']])])],
+ expected='''[DEFAULT]
+
+#
+# From test
+#
+
+# native help (floating point value)
+#native_float_type = <None>
+''')),
('multi_opt_sample_default',
dict(opts=[('test', [(None, [opts['multi_opt_sample_default']])])],
expected='''[DEFAULT]