diff options
author | sylvain thenault <sylvain.thenault@logilab.fr> | 2009-01-14 16:03:57 +0100 |
---|---|---|
committer | sylvain thenault <sylvain.thenault@logilab.fr> | 2009-01-14 16:03:57 +0100 |
commit | 187e66460d8e779ae487faace7b76ae2f85e2a68 (patch) | |
tree | ded83556faa2a0fa3d7aa2c8b14bae63d0336430 | |
parent | b49a547bb2658ce1ab565184c8e8458aca1e23da (diff) | |
download | logilab-common-187e66460d8e779ae487faace7b76ae2f85e2a68.tar.gz |
section's case insensitivity test & fix
-rw-r--r-- | configuration.py | 12 | ||||
-rw-r--r-- | test/unittest_configuration.py | 43 |
2 files changed, 41 insertions, 14 deletions
diff --git a/configuration.py b/configuration.py index db6452d..cdaac73 100644 --- a/configuration.py +++ b/configuration.py @@ -114,7 +114,7 @@ class UnsupportedAction(Exception): def _get_encoding(encoding, stream): - encoding = encoding or stream.encoding + encoding = encoding or getattr(stream, 'encoding', None) if not encoding: import locale encoding = locale.getpreferredencoding() @@ -390,8 +390,9 @@ class OptionsManagerMixIn(object): self._all_options[opt_name] = provider for gname, gdoc in groups: + gname = gname.upper() goptions = [option for option in provider.options - if option[1].get('group') == gname] + if option[1].get('group', '').upper() == gname] self.add_option_group(gname, gdoc, goptions, provider) def add_option_group(self, group_name, doc, options, provider): @@ -518,8 +519,9 @@ class OptionsManagerMixIn(object): parser = self._config_parser parser.read([config_file]) # normalize sections'title - for sect in parser._sections.keys(): - parser._sections[sect.upper()] = parser._sections[sect] + for sect, values in parser._sections.items(): + if not sect.isupper() and values: + parser._sections[sect.upper()] = values elif not self.quiet: msg = 'No config file found, using default configuration' print >> sys.stderr, msg @@ -795,7 +797,7 @@ class ConfigurationMixIn(OptionsManagerMixIn, OptionsProviderMixIn): self.option_groups = [] for option, optdict in self.options: try: - gdef = (optdict['group'], '') + gdef = (optdict['group'].upper(), '') except KeyError: continue if not gdef in self.option_groups: diff --git a/test/unittest_configuration.py b/test/unittest_configuration.py index a60d6e7..0cc2494 100644 --- a/test/unittest_configuration.py +++ b/test/unittest_configuration.py @@ -20,6 +20,10 @@ options = [('dothis', {'type':'yn', 'action': 'store', 'default': True, 'metavar 'metavar':'<yo|ye>'}), ('named', {'type':'named', 'default':Method('get_named'), 'metavar': '<key=val>'}), + + ('diffgroup', {'type':'string', 'default':'pouet', 'metavar': '<key=val>', + 'group': 'agroup'}), + ] class MyConfiguration(Configuration): @@ -92,30 +96,33 @@ class ConfigurationTC(TestCase): stream.write("""# test configuration [Test] -dothis=yes +dothis=no #value= # you can also document the option -multiple=yop,yep +multiple=yop,yepii # boom -number=2 +number=3 choice=yo multiple-choice=yo,ye named=key:val + + +[agroup] + +diffgroup=zou """) + stream.close() self.cfg.load_file_configuration(file) - self.assertEquals(self.cfg['dothis'], True) + self.assertEquals(self.cfg['dothis'], False) self.assertEquals(self.cfg['value'], None) - self.assertEquals(self.cfg['multiple'], ('yop','yep')) - self.assertEquals(self.cfg['number'], 2) - self.assertEquals(self.cfg['choice'], 'yo') - self.assertEquals(self.cfg['multiple-choice'], ('yo', 'ye')) - self.assertEquals(self.cfg['named'], {'key': 'val'}) + self.assertEquals(self.cfg['multiple'], ['yop','yepii']) + self.assertEquals(self.cfg['diffgroup'], 'zou') finally: os.remove(file) @@ -140,6 +147,11 @@ choice=yo multiple-choice=yo,ye named=key:val + + +[AGROUP] + +diffgroup=pouet """) def test_generate_config_with_space_string(self): @@ -164,6 +176,11 @@ choice=yo multiple-choice=yo,ye named=key:val + + +[AGROUP] + +diffgroup=pouet """) @@ -207,6 +224,9 @@ Options: --multiple-choice=<yo|ye> --named=<key=val> + Agroup: + --diffgroup=<key=val> + Bonus: a nice additional help """.strip()) @@ -275,6 +295,11 @@ choice=yo multiple-choice=yo,ye named=key:val + + +[AGROUP] + +diffgroup=pouet """) class Linter(OptionsManagerMixIn, OptionsProviderMixIn): |