Normalizes non lowercase option group names in conf files

To avoid inconsistency of option group name format used in conf files,
this patch normalizes group names to all lowercase names

Implements bp cfg-lowercase-groups

Change-Id: If8e6c671be776a708a7200e52e4897a8a3b90bdf
This commit is contained in:
Zhongyue Luo
2013-05-10 00:44:14 +08:00
parent f828f2fc16
commit c9524919cc
2 changed files with 23 additions and 1 deletions

View File

@@ -491,6 +491,12 @@ def set_defaults(opts, **kwargs):
opt.default = kwargs[opt.dest]
def _normalize_group_name(group_name):
if group_name == 'DEFAULT':
return group_name
return group_name.lower()
class Opt(object):
"""Base class for all configuration options.
@@ -1040,7 +1046,7 @@ class ConfigParser(iniparser.BaseParser):
return super(ConfigParser, self).parse(f)
def new_section(self, section):
self.section = section
self.section = _normalize_group_name(section)
self.sections.setdefault(self.section, {})
def assignment(self, key, value):
@@ -1080,6 +1086,8 @@ class MultiConfigParser(object):
def get(self, names, multi=False):
rvalue = []
names = [(_normalize_group_name(section), name)
for section, name in names]
for sections in self.parsed:
for section, name in names:
if section not in sections:

View File

@@ -1299,6 +1299,20 @@ class OptGroupsTestCase(BaseTestCase):
self.assertTrue(hasattr(self.conf.blaa, 'foo'))
self.assertEquals(self.conf.blaa.foo, 'bar')
def test_arg_group_in_config_file_with_capital_name(self):
self.conf.register_group(cfg.OptGroup('blaa'))
self.conf.register_opt(cfg.StrOpt('foo'), group='blaa')
paths = self.create_tempfiles([('test',
'[BLAA]\n'
'foo = bar\n')])
self.conf(['--config-file', paths[0]])
self.assertTrue(hasattr(self.conf, 'blaa'))
self.assertTrue(hasattr(self.conf.blaa, 'foo'))
self.assertEquals(self.conf.blaa.foo, 'bar')
class MappingInterfaceTestCase(BaseTestCase):