summaryrefslogtreecommitdiffstats
path: root/openstack/common
diff options
context:
space:
mode:
authorMark McLoughlin <markmc@redhat.com>2012-05-10 14:25:19 +0100
committerMark McLoughlin <markmc@redhat.com>2012-05-10 14:26:40 +0100
commitf879ed1ca8e80ba8287b04fd5410e14921fa6b50 (patch)
treeab67577347eaed9f57646ad9860605d995ca9a90 /openstack/common
parent5b0b59ed56abcd08b5a3f7fcf1af8639f9ef0a7c (diff)
downloadoslo-f879ed1ca8e80ba8287b04fd5410e14921fa6b50.tar.gz
oslo-f879ed1ca8e80ba8287b04fd5410e14921fa6b50.tar.xz
oslo-f879ed1ca8e80ba8287b04fd5410e14921fa6b50.zip
cfg: automatically create option groups
Implements blueprint cfg-auto-create-groups Remove the restriction that groups must be explicitly created. Often you only need a group to have a name (not e.g. a title or help string) so we can easily just auto-create groups for that case. Change-Id: I150ab3900e3aad0068b93487c8d396d21d26cfea
Diffstat (limited to 'openstack/common')
-rw-r--r--openstack/common/cfg.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/openstack/common/cfg.py b/openstack/common/cfg.py
index 08efeb7..8237107 100644
--- a/openstack/common/cfg.py
+++ b/openstack/common/cfg.py
@@ -149,6 +149,14 @@ Options can be registered as belonging to a group::
conf.register_opt(rabbit_host_opt, group=rabbit_group)
conf.register_opt(rabbit_port_opt, group='rabbit')
+If it no group attributes are required other than the group name, the group
+need not be explicitly registered e.g.
+
+ def register_rabbit_opts(conf):
+ # The group will automatically be created, equivalent calling::
+ # conf.register_group(OptGroup(name='rabbit'))
+ conf.register_opt(rabbit_port_opt, group='rabbit')
+
If no group is specified, options belong to the 'DEFAULT' section of config
files::
@@ -977,7 +985,7 @@ class ConfigOpts(collections.Mapping):
:raises: DuplicateOptError
"""
if group is not None:
- return self._get_group(group)._register_opt(opt)
+ return self._get_group(group, autocreate=True)._register_opt(opt)
if _is_opt_registered(self._opts, opt):
return False
@@ -1012,7 +1020,7 @@ class ConfigOpts(collections.Mapping):
return False
if group is not None:
- group = self._get_group(group)
+ group = self._get_group(group, autocreate=True)
opt._add_to_cli(self._oparser, group)
@@ -1241,7 +1249,7 @@ class ConfigOpts(collections.Mapping):
else:
return value
- def _get_group(self, group_or_name):
+ def _get_group(self, group_or_name, autocreate=False):
"""Looks up a OptGroup object.
Helper function to return an OptGroup given a parameter which can
@@ -1252,15 +1260,17 @@ class ConfigOpts(collections.Mapping):
the API have access to.
:param group_or_name: the group's name or the OptGroup object itself
+ :param autocreate: whether to auto-create the group if it's not found
:raises: NoSuchGroupError
"""
- if isinstance(group_or_name, OptGroup):
- group_name = group_or_name.name
- else:
- group_name = group_or_name
+ group = group_or_name if isinstance(group_or_name, OptGroup) else None
+ group_name = group.name if group else group_or_name
if not group_name in self._groups:
- raise NoSuchGroupError(group_name)
+ if not group is None or not autocreate:
+ raise NoSuchGroupError(group_name)
+
+ self.register_group(OptGroup(name=group_name))
return self._groups[group_name]