Example #1
0
    def get_settings(self):
        """Read settings from config file."""

        if not self.config_file:
            return
        config = ConfigParserImproved()
        config.read(self.config_file)
        settings = {}

        # The web2py section is required, if not found an exception is raised.
        for (name, value) in config.items_scrubbed('web2py'):
            settings[name] = value

        # The application section is optional, if not found the web2py
        # values are used.
        if self.application:
            try:
                for (name, value) in config.items_scrubbed(self.application):
                    settings[name] = value
            except ConfigParser.NoSectionError:
                pass

        for key in settings.keys():
            # Set the group values
            parts = key.split('.', 1)
            if len(parts) == 1:
                parts.insert(0, 'local')
            (group, setting) = parts[0:2]
            if not group in self.settings:
                self.settings[group] = {}
            self.settings[group][setting] = settings[key]
    def test__items_scrubbed(self):
        text = \
            """
[section]
s01_true = True
s02_false = False
s03_int = 123
s04_float = 123.45
s05_str1 = my setting value
s06_str2 = 'This is my setting value'
s07_str_true = 'True'
s08_str_int = '123'
s09_str_float = '123.45'

[strings]
str1 = my setting value
str2 = 'This is my setting value'

"""
        f_text = '/tmp/TestConfigParserImproved.txt'
        _config_file_from_text(f_text, text)
        config = ConfigParserImproved()
        config.read(f_text)
        # items() and items_scrubbed() should return strings identically
        self.assertEqual(config.items('strings'),
                config.items_scrubbed('strings'))

        self.assertEqual(sorted(config.items_scrubbed('section')),
            [
                ('s01_true', True),
                ('s02_false', False),
                ('s03_int', 123),
                ('s04_float', 123.45),
                ('s05_str1', 'my setting value'),
                ('s06_str2', "'This is my setting value'"),
                ('s07_str_true', 'True'),
                ('s08_str_int', '123'),
                ('s09_str_float', '123.45'),
                ])

        os.unlink(f_text)