~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

Clearly seaparate both sets of hooks for the old and new config implementations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
371
371
                              % (option_name,))
372
372
            else:
373
373
                value = self._expand_options_in_string(value)
374
 
        for hook in ConfigHooks['old_get']:
 
374
        for hook in OldConfigHooks['get']:
375
375
            hook(self, option_name, value)        
376
376
        return value
377
377
 
589
589
                      'Invoked when a config option is removed.'
590
590
                      ' The signature is (stack, name).',
591
591
                      (2, 4))
592
 
        # Hooks for the actual implementation
593
 
        self.add_hook('old_load',
 
592
ConfigHooks = _ConfigHooks()
 
593
 
 
594
 
 
595
class _OldConfigHooks(hooks.Hooks):
 
596
    """A dict mapping hook names and a list of callables for configs.
 
597
    """
 
598
 
 
599
    def __init__(self):
 
600
        """Create the default hooks.
 
601
 
 
602
        These are all empty initially, because by default nothing should get
 
603
        notified.
 
604
        """
 
605
        super(_OldConfigHooks, self).__init__('bzrlib.config', 'OldConfigHooks')
 
606
        self.add_hook('load',
594
607
                      'Invoked when a config store is loaded.'
595
608
                      ' The signature is (config).',
596
609
                      (2, 4))
597
 
        self.add_hook('old_save',
 
610
        self.add_hook('save',
598
611
                      'Invoked when a config store is saved.'
599
612
                      ' The signature is (config).',
600
613
                      (2, 4))
601
614
        # The hooks for config options
602
 
        self.add_hook('old_get',
 
615
        self.add_hook('get',
603
616
                      'Invoked when a config option is read.'
604
617
                      ' The signature is (config, name, value).',
605
618
                      (2, 4))
606
 
        self.add_hook('old_set',
 
619
        self.add_hook('set',
607
620
                      'Invoked when a config option is set.'
608
621
                      ' The signature is (config, name, value).',
609
622
                      (2, 4))
610
 
        self.add_hook('old_remove',
 
623
        self.add_hook('remove',
611
624
                      'Invoked when a config option is removed.'
612
625
                      ' The signature is (config, name).',
613
626
                      (2, 4))
614
 
ConfigHooks = _ConfigHooks()
 
627
OldConfigHooks = _OldConfigHooks()
615
628
 
616
629
 
617
630
class IniBasedConfig(Config):
681
694
            raise errors.ParseConfigError(e.errors, e.config.filename)
682
695
        # Make sure self.reload() will use the right file name
683
696
        self._parser.filename = self.file_name
684
 
        for hook in ConfigHooks['old_load']:
 
697
        for hook in OldConfigHooks['load']:
685
698
            hook(self)
686
699
        return self._parser
687
700
 
691
704
            raise AssertionError('We need a file name to reload the config')
692
705
        if self._parser is not None:
693
706
            self._parser.reload()
694
 
        for hook in ConfigHooks['old_load']:
 
707
        for hook in ConfigHooks['load']:
695
708
            hook(self)
696
709
 
697
710
    def _get_matching_sections(self):
870
883
        except KeyError:
871
884
            raise errors.NoSuchConfigOption(option_name)
872
885
        self._write_config_file()
873
 
        for hook in ConfigHooks['old_remove']:
 
886
        for hook in OldConfigHooks['remove']:
874
887
            hook(self, option_name)
875
888
 
876
889
    def _write_config_file(self):
883
896
        atomic_file.commit()
884
897
        atomic_file.close()
885
898
        osutils.copy_ownership_from_path(self.file_name)
886
 
        for hook in ConfigHooks['old_save']:
 
899
        for hook in OldConfigHooks['save']:
887
900
            hook(self)
888
901
 
889
902
 
1018
1031
        self.reload()
1019
1032
        self._get_parser().setdefault(section, {})[option] = value
1020
1033
        self._write_config_file()
1021
 
        for hook in ConfigHooks['old_set']:
 
1034
        for hook in OldConfigHooks['set']:
1022
1035
            hook(self, option, value)
1023
1036
 
1024
1037
    def _get_sections(self, name=None):
1221
1234
        # the allowed values of store match the config policies
1222
1235
        self._set_option_policy(location, option, store)
1223
1236
        self._write_config_file()
1224
 
        for hook in ConfigHooks['old_set']:
 
1237
        for hook in OldConfigHooks['set']:
1225
1238
            hook(self, option, value)
1226
1239
 
1227
1240
 
2126
2139
            except KeyError:
2127
2140
                return default
2128
2141
        value = section_obj.get(name, default)
2129
 
        for hook in ConfigHooks['old_get']:
 
2142
        for hook in OldConfigHooks['get']:
2130
2143
            hook(self, name, value)        
2131
2144
        return value
2132
2145
 
2142
2155
            configobj[name] = value
2143
2156
        else:
2144
2157
            configobj.setdefault(section, {})[name] = value
2145
 
        for hook in ConfigHooks['old_set']:
 
2158
        for hook in OldConfigHooks['set']:
2146
2159
            hook(self, name, value)
2147
2160
        self._set_configobj(configobj)
2148
2161
 
2152
2165
            del configobj[option_name]
2153
2166
        else:
2154
2167
            del configobj[section_name][option_name]
2155
 
        for hook in ConfigHooks['old_remove']:
 
2168
        for hook in OldConfigHooks['remove']:
2156
2169
            hook(self, option_name)
2157
2170
        self._set_configobj(configobj)
2158
2171
 
2159
2172
    def _get_config_file(self):
2160
2173
        try:
2161
2174
            f = StringIO(self._transport.get_bytes(self._filename))
2162
 
            for hook in ConfigHooks['old_load']:
 
2175
            for hook in OldConfigHooks['load']:
2163
2176
                hook(self)
2164
2177
            return f
2165
2178
        except errors.NoSuchFile:
2177
2190
        configobj.write(out_file)
2178
2191
        out_file.seek(0)
2179
2192
        self._transport.put_file(self._filename, out_file)
2180
 
        for hook in ConfigHooks['old_save']:
 
2193
        for hook in OldConfigHooks['save']:
2181
2194
            hook(self)
2182
2195
 
2183
2196