~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Vincent Ladeuil
  • Date: 2012-03-08 18:30:33 UTC
  • mfrom: (6487 +trunk)
  • mto: This revision was merged to the branch mainline in revision 6488.
  • Revision ID: v.ladeuil+lp@free.fr-20120308183033-bccqnbr1tpozs711
Merge bzr.dev resolving conflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
1448
1448
        value = self._get_explicit_nickname()
1449
1449
        if value is not None:
1450
1450
            return value
 
1451
        if self.branch.name:
 
1452
            return self.branch.name
1451
1453
        return urlutils.unescape(self.branch.base.split('/')[-2])
1452
1454
 
1453
1455
    def has_explicit_nickname(self):
2149
2151
 
2150
2152
        It may be set to a location, or None.
2151
2153
 
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.
2154
2156
        """
2155
2157
        if self._config is None:
2156
2158
            raise errors.BzrError("Cannot set configuration in %s" % self._bzrdir)
2164
2166
 
2165
2167
        This will either be a location, or None.
2166
2168
 
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.
2169
2171
        """
2170
2172
        if self._config is None:
2171
2173
            return None
2410
2412
                value = self.default
2411
2413
        return value
2412
2414
 
 
2415
    def get_help_topic(self):
 
2416
        return self.name
 
2417
 
2413
2418
    def get_help_text(self, additional_see_also=None, plain=True):
2414
2419
        result = self.help
2415
2420
        from bzrlib import help_topics
3620
3625
        self.store = store
3621
3626
        self.mutable_section_id = mutable_section_id
3622
3627
 
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
 
3633
        # lists.
 
3634
        for sections in self.sections_def:
 
3635
            for store, section in sections():
 
3636
                yield store, section
 
3637
 
 
3638
    def get(self, name, expand=True, convert=True):
3624
3639
        """Return the *first* option value found in the sections.
3625
3640
 
3626
3641
        This is where we guarantee that sections coming from Store are loaded
3633
3648
 
3634
3649
        :param expand: Whether options references should be expanded.
3635
3650
 
 
3651
        :param convert: Whether the option value should be converted from
 
3652
            unicode (do nothing for non-registered options).
 
3653
 
3636
3654
        :returns: The value of the option.
3637
3655
        """
3638
3656
        # FIXME: No caching of options nor sections yet -- vila 20110503
3659
3677
                                      % (name, type(val)))
3660
3678
                if opt is None:
3661
3679
                    val = found_store.unquote(val)
3662
 
                else:
 
3680
                elif convert:
3663
3681
                    val = opt.convert_from_unicode(found_store, val)
3664
3682
            return val
3665
3683
 
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)
3675
 
            # section lists.
3676
 
            for sections in self.sections_def:
3677
 
                for store, section in sections():
3678
 
                    value = section.get(name)
3679
 
                    if value is not None:
3680
 
                        found_store = store
3681
 
                        break
 
3690
            for store, section in self.iter_sections():
 
3691
                value = section.get(name)
3682
3692
                if value is not None:
 
3693
                    found_store = store
3683
3694
                    break
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]
3753
3764
        else:
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)
3756
3767
        return value
3757
3768
 
3998
4009
        self.store.save_changes()
3999
4010
 
4000
4011
 
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)
4005
 
 
4006
4012
class cmd_config(commands.Command):
4007
4013
    __doc__ = """Display, set or remove a configuration option.
4008
4014
 
4106
4112
            except errors.NotBranchError:
4107
4113
                return LocationStack(directory)
4108
4114
 
 
4115
    def _quote_multiline(self, value):
 
4116
        if '\n' in value:
 
4117
            value = '"""' + value + '"""'
 
4118
        return value
 
4119
 
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,))
4116
4127
        else:
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
4136
 
                            cur_section = None
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
4145
 
                        # proper quoting.
4146
 
                        try:
4147
 
                            opt = option_registry.get(oname)
4148
 
                            value = opt.convert_from_unicode(store, value)
4149
 
                        except KeyError:
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
 
4146
                        cur_section = None
 
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))
4153
4156
 
4154
4157
    def _set_config_option(self, name, value, directory, scope):
4155
4158
        conf = self._get_stack(directory, scope, write_access=True)