Autohelp: sanitize the value during discovery

Most of the default value sanitization happened during the docbook
writting, and not during the 'dump' of the options. This resulted in
invalid values in the xml files produced by diff_branches.py.

This patch moves all the sanitization code to the same function,
ensuring that the sanitization happens during options discovery.

Change-Id: I4d7e5d601baac5294937525ede8655da6b25e6cf
This commit is contained in:
Gauvain Pocentek 2014-10-26 20:34:16 +01:00
parent 3321a489c4
commit 5442f593b5
1 changed files with 14 additions and 11 deletions

View File

@ -223,7 +223,18 @@ def _register_runtime_opts(module, abs_path, verbose):
def _sanitize_default(opt):
"""Adapts unrealistic default values."""
# If the Oslo version is recent enough, we can use the 'sample_default'
# attribute
if (hasattr(opt, 'sample_default') and opt.sample_default is not None):
return str(opt.sample_default)
if ((type(opt).__name__ == "ListOpt") and (type(opt.default) == list)):
return ", ".join(opt.default)
default = str(opt.default)
if default == os.uname()[1]:
return 'localhost'
@ -241,7 +252,7 @@ def _sanitize_default(opt):
continue
if pathelm.endswith('/'):
pathelm = pathelm[:-1]
if default.startswith(pathelm):
if pathelm in default:
default = re.sub(r'%s(/sources)?' % pathelm,
'/usr/lib/python/site-packages', default)
@ -269,8 +280,7 @@ class OptionsCache(object):
if self._verbose >= 2:
print ("Duplicate option name %s" % optname)
else:
if opt.name == 'bindir':
opt.default = _sanitize_default(opt)
opt.default = _sanitize_default(opt)
self._opts_by_name[optname] = (group, opt)
self._opt_names.append(optname)
@ -390,14 +400,7 @@ def write_docbook(package_name, options, target, verbose=0):
if not option.help:
option.help = "No help text available for this option."
if ((type(option).__name__ == "ListOpt") and (
type(option.default) == list)):
option.default = ", ".join(option.default)
if (hasattr(option, 'sample_default') and
option.sample_default is not None):
default = str(option.sample_default)
else:
default = _sanitize_default(option)
default = _sanitize_default(option)
tr = etree.Element('tr')
tbody.append(tr)