157
158
return self[section][name]
160
# FIXME: Until we can guarantee that each config file is loaded once and and
161
# FIXME: Until we can guarantee that each config file is loaded once and
161
162
# only once for a given bzrlib session, we don't want to re-read the file every
162
163
# time we query for an option so we cache the value (bad ! watch out for tests
163
164
# needing to restore the proper value).This shouldn't be part of 2.4.0 final,
368
369
% (option_name,))
370
371
value = self._expand_options_in_string(value)
372
for hook in OldConfigHooks['get']:
373
hook(self, option_name, value)
373
376
def get_user_option_as_bool(self, option_name, expand=None, default=None):
554
557
return command_line
560
class _ConfigHooks(hooks.Hooks):
561
"""A dict mapping hook names and a list of callables for configs.
565
"""Create the default hooks.
567
These are all empty initially, because by default nothing should get
570
super(_ConfigHooks, self).__init__('bzrlib.config', 'ConfigHooks')
571
self.add_hook('load',
572
'Invoked when a config store is loaded.'
573
' The signature is (store).',
575
self.add_hook('save',
576
'Invoked when a config store is saved.'
577
' The signature is (store).',
579
# The hooks for config options
581
'Invoked when a config option is read.'
582
' The signature is (stack, name, value).',
585
'Invoked when a config option is set.'
586
' The signature is (stack, name, value).',
588
self.add_hook('remove',
589
'Invoked when a config option is removed.'
590
' The signature is (stack, name).',
592
ConfigHooks = _ConfigHooks()
595
class _OldConfigHooks(hooks.Hooks):
596
"""A dict mapping hook names and a list of callables for configs.
600
"""Create the default hooks.
602
These are all empty initially, because by default nothing should get
605
super(_OldConfigHooks, self).__init__('bzrlib.config', 'OldConfigHooks')
606
self.add_hook('load',
607
'Invoked when a config store is loaded.'
608
' The signature is (config).',
610
self.add_hook('save',
611
'Invoked when a config store is saved.'
612
' The signature is (config).',
614
# The hooks for config options
616
'Invoked when a config option is read.'
617
' The signature is (config, name, value).',
620
'Invoked when a config option is set.'
621
' The signature is (config, name, value).',
623
self.add_hook('remove',
624
'Invoked when a config option is removed.'
625
' The signature is (config, name).',
627
OldConfigHooks = _OldConfigHooks()
557
630
class IniBasedConfig(Config):
558
631
"""A configuration policy that draws from ini files."""
951
1032
self._get_parser().setdefault(section, {})[option] = value
952
1033
self._write_config_file()
1034
for hook in OldConfigHooks['set']:
1035
hook(self, option, value)
955
1037
def _get_sections(self, name=None):
956
1038
"""See IniBasedConfig._get_sections()."""
1152
1234
# the allowed values of store match the config policies
1153
1235
self._set_option_policy(location, option, store)
1154
1236
self._write_config_file()
1237
for hook in OldConfigHooks['set']:
1238
hook(self, option, value)
1157
1241
class BranchConfig(Config):
1635
1719
section[option_name] = value
1638
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1722
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1640
1724
"""Returns the matching credentials from authentication.conf file.
2054
2138
section_obj = configobj[section]
2055
2139
except KeyError:
2057
return section_obj.get(name, default)
2141
value = section_obj.get(name, default)
2142
for hook in OldConfigHooks['get']:
2143
hook(self, name, value)
2059
2146
def set_option(self, value, name, section=None):
2060
2147
"""Set the value associated with a named option.
2076
2165
del configobj[option_name]
2078
2167
del configobj[section_name][option_name]
2168
for hook in OldConfigHooks['remove']:
2169
hook(self, option_name)
2079
2170
self._set_configobj(configobj)
2081
2172
def _get_config_file(self):
2083
return StringIO(self._transport.get_bytes(self._filename))
2174
f = StringIO(self._transport.get_bytes(self._filename))
2175
for hook in OldConfigHooks['load']:
2084
2178
except errors.NoSuchFile:
2085
2179
return StringIO()
2604
2706
"""Set a new value for the option."""
2605
2707
section = self._get_mutable_section()
2606
2708
section.set(name, value)
2709
for hook in ConfigHooks['set']:
2710
hook(self, name, value)
2608
2712
def remove(self, name):
2609
2713
"""Remove an existing option."""
2610
2714
section = self._get_mutable_section()
2611
2715
section.remove(name)
2716
for hook in ConfigHooks['remove']:
2613
2719
def __repr__(self):
2614
2720
# Mostly for debugging use