diff options
| author | Mark McLoughlin <markmc@redhat.com> | 2012-07-31 12:16:28 +0100 |
|---|---|---|
| committer | Mark McLoughlin <markmc@redhat.com> | 2012-07-31 12:16:28 +0100 |
| commit | 038d59778eecf953375da701b4cd55d4e3ca309f (patch) | |
| tree | 5d0046643bc5c3dcd864675fdac7f09e1c99d0a2 /openstack | |
| parent | e0134fc3e2d7e59741b3643e2680f578cc9def41 (diff) | |
| download | oslo-038d59778eecf953375da701b4cd55d4e3ca309f.tar.gz oslo-038d59778eecf953375da701b4cd55d4e3ca309f.tar.xz oslo-038d59778eecf953375da701b4cd55d4e3ca309f.zip | |
Add import_opt() method to ConfigOpts
Related to blueprint cfg-global-object
When using the global config object pattern, you often have modules
which define options that are referenced in other options.
So, for example if module A defined option 'foo' and module be needed
to reference that option, you might do:
import A
print CONF.foo
However, this makes it entirely unclear to the casual reader why
module A was imported.
Nova has a flags.DECLARE() function that helps with this problem
by allowing you to do:
flags.DECLARE('foo', 'A')
The function simply imports module A and checks that the 'foo'
option is now defined in the global config object.
This is fine, but it is also implicit that this function applies
to the global config object. Instead, let's do the following:
CONF.import_opt('foo', 'A')
Change-Id: I7b98f5be71068bbde70cc0eab991eaebb577de52
Diffstat (limited to 'openstack')
| -rw-r--r-- | openstack/common/cfg.py | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py index 728ecdc..b42c30e 100644 --- a/openstack/common/cfg.py +++ b/openstack/common/cfg.py @@ -1156,6 +1156,25 @@ class ConfigOpts(collections.Mapping): for opt in opts: self.unregister_opt(opt, group, clear_cache=False) + def import_opt(self, name, module_str, group=None): + """Import an option definition from a module. + + Import a module and check that a given option is registered. + + This is intended for use with global configuration objects + like cfg.CONF where modules commonly register options with + CONF at module load time. If one module requires an option + defined by another module it can use this method to explicitly + declare the dependency. + + :param name: the name/dest of the opt + :param module_str: the name of a module to import + :param group: an option OptGroup object or group name + :raises: NoSuchOptError, NoSuchGroupError + """ + __import__(module_str) + self._get_opt_info(name, group) + @__clear_cache def set_override(self, name, override, group=None): """Override an opt value. |
