From 513bd3a917207099125cc044705aee438fee7143 Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Fri, 10 Aug 2012 14:28:59 -0700 Subject: Allow set_default and set_override to use None The current implementation interprets set_default('foo', None) and set_override('foo', None) as 'clear the existing default or override', which makes it impossible to override a value with None. This patch adds support for overriding with a None value by adding a special internal class. set_override('foo', None) will now override the existing value with None. This is a slight change to the existing behavior, so this patch adds two calls for the old functionality of clearing defaults and overrides. Example syntax for the new calls are shown below: conf.clear_default('foo') conf.clear_override('foo') The patch also updates the tests to reflect the change in functionality and adds new tests to verify the new functionality. Fixes bug 1035478 Change-Id: Iee5e20e44da9bef6b86e0483ab0b48b625fe503c --- openstack/common/cfg.py | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) (limited to 'openstack') diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py index b42c30e..a56f957 100644 --- a/openstack/common/cfg.py +++ b/openstack/common/cfg.py @@ -1175,6 +1175,9 @@ class ConfigOpts(collections.Mapping): __import__(module_str) self._get_opt_info(name, group) + class _NoneValue(object): + pass + @__clear_cache def set_override(self, name, override, group=None): """Override an opt value. @@ -1187,6 +1190,8 @@ class ConfigOpts(collections.Mapping): :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ + if override is None: + override = self._NoneValue() opt_info = self._get_opt_info(name, group) opt_info['override'] = override @@ -1202,9 +1207,38 @@ class ConfigOpts(collections.Mapping): :param group: an option OptGroup object or group name :raises: NoSuchOptError, NoSuchGroupError """ + if default is None: + default = self._NoneValue() opt_info = self._get_opt_info(name, group) opt_info['default'] = default + @__clear_cache + def clear_override(self, name, group=None): + """Clear an override an opt value. + + Clear a previously set override of the command line, config file + and default values of a given option. + + :param name: the name/dest of the opt + :param group: an option OptGroup object or group name + :raises: NoSuchOptError, NoSuchGroupError + """ + opt_info = self._get_opt_info(name, group) + opt_info['override'] = None + + @__clear_cache + def clear_default(self, name, group=None): + """Clear an override an opt's default value. + + Clear a previously set override of the default value of given option. + + :param name: the name/dest of the opt + :param group: an option OptGroup object or group name + :raises: NoSuchOptError, NoSuchGroupError + """ + opt_info = self._get_opt_info(name, group) + opt_info['default'] = None + def _all_opt_infos(self): """A generator function for iteration opt infos.""" for info in self._opts.values(): @@ -1347,8 +1381,11 @@ class ConfigOpts(collections.Mapping): info = self._get_opt_info(name, group) default, opt, override = [info[k] for k in sorted(info.keys())] + def _convert_none(value): + return None if isinstance(value, self._NoneValue) else value + if override is not None: - return override + return _convert_none(override) values = [] if self._cparser is not None: @@ -1377,7 +1414,7 @@ class ConfigOpts(collections.Mapping): return values if default is not None: - return default + return _convert_none(default) return opt.default -- cgit