diff options
author | Tong Damon Da <tongda@outlook.com> | 2015-04-09 13:19:28 +0800 |
---|---|---|
committer | Tong Damon Da <tongda@outlook.com> | 2015-05-23 12:39:12 +0800 |
commit | f1f972aeb7e7e6f672b55fb8eefc37de71f5d551 (patch) | |
tree | d599c17f7674d4ad126c41c07e274821a47e9d70 /oslo_config/cfgfilter.py | |
parent | a8db68bd43a36bd5cfb2ff96f2160ab151779953 (diff) | |
download | oslo-config-f1f972aeb7e7e6f672b55fb8eefc37de71f5d551.tar.gz |
make registering a cli opt on a filter object work.
Change the behaviour of ConfigFilter.register_cli_opt to:
1. If the opt is already registered before parsing, then registering
just import it.
2. if the opt is not registered before, then raise an exception named
CliOptRegisteredError.
By adding this modification, the behaviour of
ConfigFilter.register_cli_opt looks more consistent to
ConfigOpts.register_cli_opt. The solution to the question mentioned in
Bug#1366946 should be like this:
from oslo_config import cfg
from oslo_config import cfgfilter
import sys
c = cfg.CONF
c.register_cli_opt(
cfg.BoolOpt('myflag',
default=False,
help='turn on myflag',
)
)
c(sys.argv[1:])
f = cfgfilter.ConfigFilter(c)
f.register_cli_opt(
cfg.BoolOpt('myflag',
default=False,
help='turn on myflag',
)
)
print f.myflag
Closes-Bug: #1366946
Change-Id: I94df9409f72807461370b4aaf8eb2543c52a89bb
Diffstat (limited to 'oslo_config/cfgfilter.py')
-rw-r--r-- | oslo_config/cfgfilter.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/oslo_config/cfgfilter.py b/oslo_config/cfgfilter.py index 6b1781b..4b00166 100644 --- a/oslo_config/cfgfilter.py +++ b/oslo_config/cfgfilter.py @@ -131,8 +131,19 @@ import itertools from oslo_config import cfg -class ConfigFilter(collections.Mapping): +class CliOptRegisteredError(cfg.Error): + """Raised when registering cli opt not in original ConfigOpts.""" + + def __str__(self): + ret = "Cannot register a cli option that was not present in the" \ + " original ConfigOpts." + + if self.msg: + ret += ": " + self.msg + return ret + +class ConfigFilter(collections.Mapping): """A helper class which wraps a ConfigOpts object. ConfigFilter enforces the explicit declaration of dependencies on external @@ -235,11 +246,13 @@ class ConfigFilter(collections.Mapping): """ if self._already_registered(self._conf, opt, group): # Raises DuplicateError if there is another opt with the same name - ret = self._conf.register_cli_opt(opt, group) + ret = self._conf.register_opt( + opt, group, cli=True, clear_cache=False) self._import_opt(opt.dest, group) return ret else: - return self._fconf.register_cli_opt(opt, group) + raise CliOptRegisteredError( + "Opt '{0}' cannot be registered.".format(opt.dest)) def register_cli_opts(self, opts, group=None): """Register multiple CLI option schemas at once.""" |