Edgewall Software

Ticket #8290: t8290-config_section2-r8198-011.diff

File t8290-config_section2-r8198-011.diff, 3.6 KB (added by osimons, 3 years ago)

Further improvements - including multiple inheritance…

  • trac/config.py

    diff --git a/trac/config.py b/trac/config.py
    a b  
    141141    def sections(self): 
    142142        """Return a list of section names.""" 
    143143        sections = set(self.parser.sections()) 
    144         parent = self.parent 
    145         while parent: 
    146             sections |= set(parent.parser.sections()) 
    147             parent = parent.parent 
     144        if self.parent: 
     145            sections.update(self.parent.sections()) 
     146        else: 
     147            sections.update(self.defaults().keys()) 
    148148        return sorted(sections) 
    149149 
    150150    def has_option(self, section, option): 
     
    153153         
    154154        (since Trac 0.11) 
    155155        """ 
    156         # Check project trac.ini 
    157         for file_option, val in self.options(section): 
    158             if file_option == option: 
     156        if self.parser.has_section(section): 
     157            if option in self.parser.options(section): 
    159158                return True 
    160         # Check parent trac.ini 
    161159        if self.parent: 
    162             for parent_option, val in self.parent.options(section): 
    163                 if parent_option == option: 
    164                     return True 
    165         # Check the registry 
    166         if (section, option) in Option.registry: 
    167             return True 
    168         # Not found 
    169         return False 
     160            return self.parent.has_option(section, option) 
     161        else: 
     162            return (section, option) in Option.registry 
    170163 
    171164    def save(self): 
    172165        """Write the configuration options to the primary file.""" 
     
    254247            return True 
    255248        if self.config.parent: 
    256249            return name in self.config.parent[self.name] 
    257         return False 
     250        return Option.registry.has_key((self.name, name)) 
    258251 
    259252    def __iter__(self): 
    260253        options = set() 
     
    266259            for option in self.config.parent[self.name]: 
    267260                if option.lower() not in options: 
    268261                    yield option 
     262        else: 
     263            for section, option in Option.registry.keys(): 
     264                if section == self.name and option.lower() not in options: 
     265                    yield option 
    269266 
    270267    def __repr__(self): 
    271268        return '<Section [%s]>' % (self.name) 
  • trac/tests/config.py

    diff --git a/trac/tests/config.py b/trac/tests/config.py
    a b  
    192192        self._write(['[a]', 'option = x', '[b]', 'option = y']) 
    193193        config = self._read() 
    194194        self.assertEquals(['a', 'b'], config.sections()) 
     195         
     196        class Foo(object): 
     197            option_c = Option('c', 'option', 'value') 
     198         
     199        self.assertEquals(['a', 'b', 'c'], config.sections()) 
    195200 
    196201    def test_options(self): 
    197202        self._write(['[a]', 'option = x', '[b]', 'option = y']) 
     
    199204        self.assertEquals(('option', 'x'), iter(config.options('a')).next()) 
    200205        self.assertEquals(('option', 'y'), iter(config.options('b')).next()) 
    201206        self.assertRaises(StopIteration, iter(config.options('c')).next) 
     207         
     208        class Foo(object): 
     209            option_a = Option('a', 'b', 'c') 
     210         
     211        self.assertEquals([('option', 'x'), ('b', 'c')], 
     212                                                list(config.options('a'))) 
    202213 
    203214    def test_has_option(self): 
    204215        config = self._read() 
     
    207218        config = self._read() 
    208219        self.assertEquals(True, config.has_option('a', 'option')) 
    209220 
     221        class Foo(object): 
     222            option_a = Option('a', 'option2', 'x2') 
     223         
     224        self.assertEquals(True, config.has_option('a', 'option2')) 
     225 
    210226    def test_reparse(self): 
    211227        self._write(['[a]', 'option = x']) 
    212228        config = self._read()