~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

Separate the hooks for old and new config implementations instead of cheating like crazy.

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['get']:
375
 
            # We're lying here pretending to be a Stack until we get deprecated
376
 
            # -- vila 20110610
 
374
        for hook in ConfigHooks['old_get']:
377
375
            hook(self, option_name, value)        
378
376
        return value
379
377
 
591
589
                      'Invoked when a config option is removed.'
592
590
                      ' The signature is (stack, name).',
593
591
                      (2, 4))
 
592
        # Hooks for the actual implementation
 
593
        self.add_hook('old_load',
 
594
                      'Invoked when a config store is loaded.'
 
595
                      ' The signature is (config).',
 
596
                      (2, 4))
 
597
        self.add_hook('old_save',
 
598
                      'Invoked when a config store is saved.'
 
599
                      ' The signature is (config).',
 
600
                      (2, 4))
 
601
        # The hooks for config options
 
602
        self.add_hook('old_get',
 
603
                      'Invoked when a config option is read.'
 
604
                      ' The signature is (config, name, value).',
 
605
                      (2, 4))
 
606
        self.add_hook('old_set',
 
607
                      'Invoked when a config option is set.'
 
608
                      ' The signature is (config, name, value).',
 
609
                      (2, 4))
 
610
        self.add_hook('old_remove',
 
611
                      'Invoked when a config option is removed.'
 
612
                      ' The signature is (config, name).',
 
613
                      (2, 4))
594
614
ConfigHooks = _ConfigHooks()
595
615
 
596
616
 
661
681
            raise errors.ParseConfigError(e.errors, e.config.filename)
662
682
        # Make sure self.reload() will use the right file name
663
683
        self._parser.filename = self.file_name
664
 
        for hook in ConfigHooks['load']:
665
 
            # We're lying here pretending to be a Store until we get deprecated
666
 
            # -- vila 20110610
 
684
        for hook in ConfigHooks['old_load']:
667
685
            hook(self)
668
686
        return self._parser
669
687
 
673
691
            raise AssertionError('We need a file name to reload the config')
674
692
        if self._parser is not None:
675
693
            self._parser.reload()
676
 
        for hook in ConfigHooks['load']:
677
 
            # We're lying here pretending to be a Store until we get deprecated
678
 
            # -- vila 20110610
 
694
        for hook in ConfigHooks['old_load']:
679
695
            hook(self)
680
696
 
681
697
    def _get_matching_sections(self):
854
870
        except KeyError:
855
871
            raise errors.NoSuchConfigOption(option_name)
856
872
        self._write_config_file()
857
 
        for hook in ConfigHooks['remove']:
858
 
            # We're lying here pretending to be a Stack until we get deprecated
859
 
            # -- vila 20110610
 
873
        for hook in ConfigHooks['old_remove']:
860
874
            hook(self, option_name)
861
875
 
862
876
    def _write_config_file(self):
869
883
        atomic_file.commit()
870
884
        atomic_file.close()
871
885
        osutils.copy_ownership_from_path(self.file_name)
872
 
        for hook in ConfigHooks['save']:
873
 
            # We're lying here pretending to be a Store until we get deprecated
874
 
            # -- vila 20110610
 
886
        for hook in ConfigHooks['old_save']:
875
887
            hook(self)
876
888
 
877
889
 
1006
1018
        self.reload()
1007
1019
        self._get_parser().setdefault(section, {})[option] = value
1008
1020
        self._write_config_file()
1009
 
        for hook in ConfigHooks['set']:
1010
 
            # We're lying here pretending to be a Store until we get deprecated
1011
 
            # -- vila 20110610
 
1021
        for hook in ConfigHooks['old_set']:
1012
1022
            hook(self, option, value)
1013
1023
 
1014
1024
    def _get_sections(self, name=None):
1211
1221
        # the allowed values of store match the config policies
1212
1222
        self._set_option_policy(location, option, store)
1213
1223
        self._write_config_file()
1214
 
        for hook in ConfigHooks['set']:
1215
 
            # We're lying here pretending to be a Store until we get deprecated
1216
 
            # -- vila 20110610
 
1224
        for hook in ConfigHooks['old_set']:
1217
1225
            hook(self, option, value)
1218
1226
 
1219
1227
 
2118
2126
            except KeyError:
2119
2127
                return default
2120
2128
        value = section_obj.get(name, default)
2121
 
        for hook in ConfigHooks['get']:
2122
 
            # We're lying here pretending to be a Stack until we get deprecated
2123
 
            # -- vila 20110613
 
2129
        for hook in ConfigHooks['old_get']:
2124
2130
            hook(self, name, value)        
2125
2131
        return value
2126
2132
 
2136
2142
            configobj[name] = value
2137
2143
        else:
2138
2144
            configobj.setdefault(section, {})[name] = value
2139
 
        for hook in ConfigHooks['set']:
2140
 
            # We're lying here pretending to be a Store until we get deprecated
2141
 
            # -- vila 20110610
 
2145
        for hook in ConfigHooks['old_set']:
2142
2146
            hook(self, name, value)
2143
2147
        self._set_configobj(configobj)
2144
2148
 
2148
2152
            del configobj[option_name]
2149
2153
        else:
2150
2154
            del configobj[section_name][option_name]
2151
 
        for hook in ConfigHooks['remove']:
2152
 
            # We're lying here pretending to be a Store until we get deprecated
2153
 
            # -- vila 20110613
 
2155
        for hook in ConfigHooks['old_remove']:
2154
2156
            hook(self, option_name, value)
2155
2157
        self._set_configobj(configobj)
2156
2158
 
2157
2159
    def _get_config_file(self):
2158
2160
        try:
2159
2161
            f = StringIO(self._transport.get_bytes(self._filename))
2160
 
            for hook in ConfigHooks['load']:
2161
 
                # We're lying here pretending to be a Store until we get
2162
 
                # deprecated -- vila 20110613
 
2162
            for hook in ConfigHooks['old_load']:
2163
2163
                hook(self)
2164
2164
            return f
2165
2165
        except errors.NoSuchFile:
2177
2177
        configobj.write(out_file)
2178
2178
        out_file.seek(0)
2179
2179
        self._transport.put_file(self._filename, out_file)
2180
 
        for hook in ConfigHooks['save']:
2181
 
            # We're lying here pretending to be a Store until we get deprecated
2182
 
            # -- vila 20110613
 
2180
        for hook in ConfigHooks['old_save']:
2183
2181
            hook(self)
2184
2182
 
2185
2183