3645
3645
self.store = store
3646
3646
self.mutable_section_id = mutable_section_id
3648
def get(self, name, expand=None):
3648
def iter_sections(self):
3649
"""Iterate all the defined sections."""
3650
# Ensuring lazy loading is achieved by delaying section matching (which
3651
# implies querying the persistent storage) until it can't be avoided
3652
# anymore by using callables to describe (possibly empty) section
3654
for sections in self.sections_def:
3655
for store, section in sections():
3656
yield store, section
3658
def get(self, name, expand=None, convert=True):
3649
3659
"""Return the *first* option value found in the sections.
3651
3661
This is where we guarantee that sections coming from Store are loaded
3659
3669
:param expand: Whether options references should be expanded.
3671
:param convert: Whether the option value should be converted from
3672
unicode (do nothing for non-registered options).
3661
3674
:returns: The value of the option.
3663
3676
# FIXME: No caching of options nor sections yet -- vila 20110503
3696
3709
value = opt.get_override()
3697
3710
value = expand_and_convert(value)
3698
3711
if value is None:
3699
# Ensuring lazy loading is achieved by delaying section matching
3700
# (which implies querying the persistent storage) until it can't be
3701
# avoided anymore by using callables to describe (possibly empty)
3703
for sections in self.sections_def:
3704
for store, section in sections():
3705
value = section.get(name)
3706
if value is not None:
3712
for store, section in self.iter_sections():
3713
value = section.get(name)
3709
3714
if value is not None:
3711
3717
value = expand_and_convert(value)
3712
3718
if opt is not None and value is None:
3778
3784
# anything else
3779
3785
value = env[name]
3781
value = self.get(name, expand=False)
3787
value = self.get(name, expand=False, convert=False)
3782
3788
value = self._expand_options_in_string(value, env, _refs)
4025
4031
self.store.save_changes()
4028
# Use a an empty dict to initialize an empty configobj avoiding all
4029
# parsing and encoding checks
4030
_quoting_config = configobj.ConfigObj(
4031
{}, encoding='utf-8', interpolation=False, list_values=True)
4033
4034
class cmd_config(commands.Command):
4034
4035
__doc__ = """Display, set or remove a configuration option.
4133
4134
except errors.NotBranchError:
4134
4135
return LocationStack(directory)
4137
def _quote_multiline(self, value):
4139
value = '"""' + value + '"""'
4136
4142
def _show_value(self, name, directory, scope):
4137
4143
conf = self._get_stack(directory, scope)
4138
value = conf.get(name, expand=True)
4144
value = conf.get(name, expand=True, convert=False)
4139
4145
if value is not None:
4140
4146
# Quote the value appropriately
4141
value = _quoting_config._quote(value)
4147
value = self._quote_multiline(value)
4142
4148
self.outf.write('%s\n' % (value,))
4144
4150
raise errors.NoSuchConfigOption(name)
4152
4158
cur_store_id = None
4153
4159
cur_section = None
4154
4160
conf = self._get_stack(directory, scope)
4155
for sections in conf.sections_def:
4156
for store, section in sections():
4157
for oname in section.iter_option_names():
4158
if name.search(oname):
4159
if cur_store_id != store.id:
4160
# Explain where the options are defined
4161
self.outf.write('%s:\n' % (store.id,))
4162
cur_store_id = store.id
4164
if (section.id is not None
4165
and cur_section != section.id):
4166
# Display the section id as it appears in the store
4167
# (None doesn't appear by definition)
4168
self.outf.write(' [%s]\n' % (section.id,))
4169
cur_section = section.id
4170
value = section.get(oname, expand=False)
4171
# Since we don't use the stack, we need to restore a
4174
opt = option_registry.get(oname)
4175
value = opt.convert_from_unicode(store, value)
4177
value = store.unquote(value)
4178
value = _quoting_config._quote(value)
4179
self.outf.write(' %s = %s\n' % (oname, value))
4161
for store, section in conf.iter_sections():
4162
for oname in section.iter_option_names():
4163
if name.search(oname):
4164
if cur_store_id != store.id:
4165
# Explain where the options are defined
4166
self.outf.write('%s:\n' % (store.id,))
4167
cur_store_id = store.id
4169
if (section.id is not None and cur_section != section.id):
4170
# Display the section id as it appears in the store
4171
# (None doesn't appear by definition)
4172
self.outf.write(' [%s]\n' % (section.id,))
4173
cur_section = section.id
4174
value = section.get(oname, expand=False)
4175
# Quote the value appropriately
4176
value = self._quote_multiline(value)
4177
self.outf.write(' %s = %s\n' % (oname, value))
4181
4179
def _set_config_option(self, name, value, directory, scope):
4182
4180
conf = self._get_stack(directory, scope, write_access=True)