From 187e66460d8e779ae487faace7b76ae2f85e2a68 Mon Sep 17 00:00:00 2001 From: sylvain thenault Date: Wed, 14 Jan 2009 16:03:57 +0100 Subject: section's case insensitivity test & fix --- configuration.py | 12 +++++++----- 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':''}), ('named', {'type':'named', 'default':Method('get_named'), 'metavar': ''}), + + ('diffgroup', {'type':'string', 'default':'pouet', 'metavar': '', + '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= --named= + Agroup: + --diffgroup= + 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): -- cgit v1.2.1