~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

add support for specifying policy when storing options

Show diffs side-by-side

added added

removed removed

Lines of Context:
93
93
SIGN_NEVER=2
94
94
 
95
95
 
96
 
STORE_LOCATION = 0
97
 
STORE_LOCATION_NORECURSE = 1
98
 
STORE_LOCATION_APPENDPATH = 2
99
 
STORE_BRANCH = 3
100
 
STORE_GLOBAL = 4
101
 
 
102
 
 
103
96
POLICY_NONE = 0
104
97
POLICY_NORECURSE = 1
105
98
POLICY_APPENDPATH = 2
106
99
 
 
100
_policy_key_names = [
 
101
    ('policy_norecurse', POLICY_NORECURSE),
 
102
    ('policy_appendpath', POLICY_APPENDPATH)]
 
103
 
 
104
 
 
105
STORE_LOCATION = POLICY_NONE
 
106
STORE_LOCATION_NORECURSE = POLICY_NORECURSE
 
107
STORE_LOCATION_APPENDPATH = POLICY_APPENDPATH
 
108
STORE_BRANCH = 3
 
109
STORE_GLOBAL = 4
 
110
 
107
111
 
108
112
class ConfigObj(configobj.ConfigObj):
109
113
 
491
495
        if not recurse:
492
496
            return POLICY_NORECURSE
493
497
 
494
 
        for (name, policy) in [('norecurse', POLICY_NORECURSE),
495
 
                               ('appendpath', POLICY_APPENDPATH)]:
 
498
        for (name, policy) in _policy_key_names:
496
499
            try:
497
 
                value = self._get_parser().get_value(section,
498
 
                                                     'policy_%s' % name)
 
500
                value = self._get_parser().get_value(section, name)
 
501
                if not isinstance(value, list):
 
502
                    value = [value]
499
503
                if option_name in value:
500
504
                    return policy
501
505
            except KeyError:
504
508
        # fall back to no special policy
505
509
        return POLICY_NONE
506
510
 
507
 
    def set_user_option(self, option, value):
 
511
    def _set_option_policy(self, section, option_name, option_policy):
 
512
        """Set the policy for the given option name in the given section."""
 
513
        # The old recurse=False option affects all options in the
 
514
        # section.  To handle multiple policies in the section, we
 
515
        # need to convert it to a policy_norecurse key.
 
516
        try:
 
517
            recurse = self._get_parser()[section].as_bool('recurse')
 
518
        except KeyError:
 
519
            pass
 
520
        else:
 
521
            warning('The recurse option in section %s of %s '
 
522
                    'has been converted to a policy_norecurse option'
 
523
                    % (section, self._get_filename()))
 
524
            del self._get_parser()[section]['recurse']
 
525
            all_keys = sorted(
 
526
                key for key in self._get_parser()[section].keys()
 
527
                if not key.startswith('policy_'))
 
528
            if all_keys:
 
529
                self._get_parser()[section]['policy_norecurse'] = all_keys
 
530
 
 
531
        for (name, policy) in _policy_key_names:
 
532
            try:
 
533
                value = self._get_parser().get_value(section, name)
 
534
                if not isinstance(value, list):
 
535
                    value = [value]
 
536
            except KeyError:
 
537
                value = []
 
538
            if policy == option_policy:
 
539
                if option_name not in value:
 
540
                    value = sorted(value + [option_name])
 
541
                    self._get_parser()[section][name] = value
 
542
            else:
 
543
                if option_name in value:
 
544
                    value.remove(option_name)
 
545
                    self._get_parser()[section][name] = value
 
546
 
 
547
    def set_user_option(self, option, value, store=STORE_LOCATION):
508
548
        """Save option and its value in the configuration."""
 
549
        assert store in [STORE_LOCATION,
 
550
                         STORE_LOCATION_NORECURSE,
 
551
                         STORE_LOCATION_APPENDPATH], 'bad storage policy'
509
552
        # FIXME: RBC 20051029 This should refresh the parser and also take a
510
553
        # file lock on locations.conf.
511
554
        conf_dir = os.path.dirname(self._get_filename())
519
562
        elif location + '/' in self._get_parser():
520
563
            location = location + '/'
521
564
        self._get_parser()[location][option]=value
 
565
        # the allowed values of store match the config policies
 
566
        self._set_option_policy(location, option, store)
522
567
        self._get_parser().write(file(self._get_filename(), 'wb'))
523
568
 
524
569
 
599
644
                return value
600
645
        return None
601
646
 
602
 
    def set_user_option(self, name, value, local=False):
603
 
        if local is True:
604
 
            self._get_location_config().set_user_option(name, value)
605
 
        else:
 
647
    def set_user_option(self, name, value, store=STORE_BRANCH):
 
648
        if store == STORE_BRANCH:
606
649
            self._get_branch_data_config().set_option(value, name)
607
 
 
 
650
        elif store == STORE_GLOBAL:
 
651
            self._get_global_config().set_option(name, value)
 
652
        else:
 
653
            self._get_location_config().set_user_option(name, value, store)
608
654
 
609
655
    def _gpg_signing_command(self):
610
656
        """See Config.gpg_signing_command."""