~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Jelmer Vernooij
  • Date: 2012-02-20 14:15:25 UTC
  • mto: (6471.1.4 iter-child-entries)
  • mto: This revision was merged to the branch mainline in revision 6472.
  • Revision ID: jelmer@samba.org-20120220141525-9azkfei62st8yc7w
Use inventories directly in fewer places.

Show diffs side-by-side

added added

removed removed

Lines of Context:
3645
3645
        self.store = store
3646
3646
        self.mutable_section_id = mutable_section_id
3647
3647
 
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
3653
 
        # lists.
3654
 
        for sections in self.sections_def:
3655
 
            for store, section in sections():
3656
 
                yield store, section
3657
 
 
3658
 
    def get(self, name, expand=None, convert=True):
 
3648
    def get(self, name, expand=None):
3659
3649
        """Return the *first* option value found in the sections.
3660
3650
 
3661
3651
        This is where we guarantee that sections coming from Store are loaded
3668
3658
 
3669
3659
        :param expand: Whether options references should be expanded.
3670
3660
 
3671
 
        :param convert: Whether the option value should be converted from
3672
 
            unicode (do nothing for non-registered options).
3673
 
 
3674
3661
        :returns: The value of the option.
3675
3662
        """
3676
3663
        # FIXME: No caching of options nor sections yet -- vila 20110503
3699
3686
                                      % (name, type(val)))
3700
3687
                if opt is None:
3701
3688
                    val = found_store.unquote(val)
3702
 
                elif convert:
 
3689
                else:
3703
3690
                    val = opt.convert_from_unicode(found_store, val)
3704
3691
            return val
3705
3692
 
3709
3696
            value = opt.get_override()
3710
3697
            value = expand_and_convert(value)
3711
3698
        if value is None:
3712
 
            for store, section in self.iter_sections():
3713
 
                value = section.get(name)
 
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)
 
3702
            # section lists.
 
3703
            for sections in self.sections_def:
 
3704
                for store, section in sections():
 
3705
                    value = section.get(name)
 
3706
                    if value is not None:
 
3707
                        found_store = store
 
3708
                        break
3714
3709
                if value is not None:
3715
 
                    found_store = store
3716
3710
                    break
3717
3711
            value = expand_and_convert(value)
3718
3712
            if opt is not None and value is None:
3784
3778
            # anything else
3785
3779
            value = env[name]
3786
3780
        else:
3787
 
            value = self.get(name, expand=False, convert=False)
 
3781
            value = self.get(name, expand=False)
3788
3782
            value = self._expand_options_in_string(value, env, _refs)
3789
3783
        return value
3790
3784
 
4031
4025
        self.store.save_changes()
4032
4026
 
4033
4027
 
 
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)
 
4032
 
4034
4033
class cmd_config(commands.Command):
4035
4034
    __doc__ = """Display, set or remove a configuration option.
4036
4035
 
4134
4133
            except errors.NotBranchError:
4135
4134
                return LocationStack(directory)
4136
4135
 
4137
 
    def _quote_multiline(self, value):
4138
 
        if '\n' in value:
4139
 
            value = '"""' + value + '"""'
4140
 
        return value
4141
 
 
4142
4136
    def _show_value(self, name, directory, scope):
4143
4137
        conf = self._get_stack(directory, scope)
4144
 
        value = conf.get(name, expand=True, convert=False)
 
4138
        value = conf.get(name, expand=True)
4145
4139
        if value is not None:
4146
4140
            # Quote the value appropriately
4147
 
            value = self._quote_multiline(value)
 
4141
            value = _quoting_config._quote(value)
4148
4142
            self.outf.write('%s\n' % (value,))
4149
4143
        else:
4150
4144
            raise errors.NoSuchConfigOption(name)
4158
4152
        cur_store_id = None
4159
4153
        cur_section = None
4160
4154
        conf = self._get_stack(directory, scope)
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
4168
 
                        cur_section = None
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))
 
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
 
4163
                            cur_section = None
 
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
 
4172
                        # proper quoting.
 
4173
                        try:
 
4174
                            opt = option_registry.get(oname)
 
4175
                            value = opt.convert_from_unicode(store, value)
 
4176
                        except KeyError:
 
4177
                            value = store.unquote(value)
 
4178
                        value = _quoting_config._quote(value)
 
4179
                        self.outf.write('  %s = %s\n' % (oname, value))
4178
4180
 
4179
4181
    def _set_config_option(self, name, value, directory, scope):
4180
4182
        conf = self._get_stack(directory, scope, write_access=True)