2150
2152
It may be set to a location, or None.
2152
This policy affects all branches contained by this bzrdir, except for
2153
those under repositories.
2154
This policy affects all branches contained by this control dir, except
2155
for those under repositories.
2155
2157
if self._config is None:
2156
2158
raise errors.BzrError("Cannot set configuration in %s" % self._bzrdir)
2165
2167
This will either be a location, or None.
2167
This policy affects all branches contained by this bzrdir, except for
2168
those under repositories.
2169
This policy affects all branches contained by this control dir, except
2170
for those under repositories.
2170
2172
if self._config is None:
3620
3625
self.store = store
3621
3626
self.mutable_section_id = mutable_section_id
3623
def get(self, name, expand=True):
3628
def iter_sections(self):
3629
"""Iterate all the defined sections."""
3630
# Ensuring lazy loading is achieved by delaying section matching (which
3631
# implies querying the persistent storage) until it can't be avoided
3632
# anymore by using callables to describe (possibly empty) section
3634
for sections in self.sections_def:
3635
for store, section in sections():
3636
yield store, section
3638
def get(self, name, expand=True, convert=True):
3624
3639
"""Return the *first* option value found in the sections.
3626
3641
This is where we guarantee that sections coming from Store are loaded
3634
3649
:param expand: Whether options references should be expanded.
3651
:param convert: Whether the option value should be converted from
3652
unicode (do nothing for non-registered options).
3636
3654
:returns: The value of the option.
3638
3656
# FIXME: No caching of options nor sections yet -- vila 20110503
3669
3687
value = opt.get_override()
3670
3688
value = expand_and_convert(value)
3671
3689
if value is None:
3672
# Ensuring lazy loading is achieved by delaying section matching
3673
# (which implies querying the persistent storage) until it can't be
3674
# avoided anymore by using callables to describe (possibly empty)
3676
for sections in self.sections_def:
3677
for store, section in sections():
3678
value = section.get(name)
3679
if value is not None:
3690
for store, section in self.iter_sections():
3691
value = section.get(name)
3682
3692
if value is not None:
3684
3695
value = expand_and_convert(value)
3685
3696
if opt is not None and value is None:
3751
3762
# anything else
3752
3763
value = env[name]
3754
value = self.get(name, expand=False)
3765
value = self.get(name, expand=False, convert=False)
3755
3766
value = self._expand_options_in_string(value, env, _refs)
3998
4009
self.store.save_changes()
4001
# Use a an empty dict to initialize an empty configobj avoiding all
4002
# parsing and encoding checks
4003
_quoting_config = configobj.ConfigObj(
4004
{}, encoding='utf-8', interpolation=False, list_values=True)
4006
4012
class cmd_config(commands.Command):
4007
4013
__doc__ = """Display, set or remove a configuration option.
4106
4112
except errors.NotBranchError:
4107
4113
return LocationStack(directory)
4115
def _quote_multiline(self, value):
4117
value = '"""' + value + '"""'
4109
4120
def _show_value(self, name, directory, scope):
4110
4121
conf = self._get_stack(directory, scope)
4111
value = conf.get(name, expand=True)
4122
value = conf.get(name, expand=True, convert=False)
4112
4123
if value is not None:
4113
4124
# Quote the value appropriately
4114
value = _quoting_config._quote(value)
4125
value = self._quote_multiline(value)
4115
4126
self.outf.write('%s\n' % (value,))
4117
4128
raise errors.NoSuchConfigOption(name)
4125
4136
cur_store_id = None
4126
4137
cur_section = None
4127
4138
conf = self._get_stack(directory, scope)
4128
for sections in conf.sections_def:
4129
for store, section in sections():
4130
for oname in section.iter_option_names():
4131
if name.search(oname):
4132
if cur_store_id != store.id:
4133
# Explain where the options are defined
4134
self.outf.write('%s:\n' % (store.id,))
4135
cur_store_id = store.id
4137
if (section.id is not None
4138
and cur_section != section.id):
4139
# Display the section id as it appears in the store
4140
# (None doesn't appear by definition)
4141
self.outf.write(' [%s]\n' % (section.id,))
4142
cur_section = section.id
4143
value = section.get(oname, expand=False)
4144
# Since we don't use the stack, we need to restore a
4147
opt = option_registry.get(oname)
4148
value = opt.convert_from_unicode(store, value)
4150
value = store.unquote(value)
4151
value = _quoting_config._quote(value)
4152
self.outf.write(' %s = %s\n' % (oname, value))
4139
for store, section in conf.iter_sections():
4140
for oname in section.iter_option_names():
4141
if name.search(oname):
4142
if cur_store_id != store.id:
4143
# Explain where the options are defined
4144
self.outf.write('%s:\n' % (store.id,))
4145
cur_store_id = store.id
4147
if (section.id is not None and cur_section != section.id):
4148
# Display the section id as it appears in the store
4149
# (None doesn't appear by definition)
4150
self.outf.write(' [%s]\n' % (section.id,))
4151
cur_section = section.id
4152
value = section.get(oname, expand=False)
4153
# Quote the value appropriately
4154
value = self._quote_multiline(value)
4155
self.outf.write(' %s = %s\n' % (oname, value))
4154
4157
def _set_config_option(self, name, value, directory, scope):
4155
4158
conf = self._get_stack(directory, scope, write_access=True)