/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: John Arbash Meinel
  • Date: 2006-12-18 19:21:11 UTC
  • mfrom: (2196 +trunk)
  • mto: This revision was merged to the branch mainline in revision 2209.
  • Revision ID: john@arbash-meinel.com-20061218192111-3fc13nmt21u3f1wm
[merge] bzr.dev 2196

Show diffs side-by-side

added added

removed removed

Lines of Context:
75
75
from bzrlib import (
76
76
    errors,
77
77
    osutils,
 
78
    symbol_versioning,
78
79
    urlutils,
79
80
    )
80
81
import bzrlib.util.configobj.configobj as configobj
93
94
SIGN_NEVER=2
94
95
 
95
96
 
 
97
POLICY_NONE = 0
 
98
POLICY_NORECURSE = 1
 
99
POLICY_APPENDPATH = 2
 
100
 
 
101
_policy_name = {
 
102
    POLICY_NONE: None,
 
103
    POLICY_NORECURSE: 'norecurse',
 
104
    POLICY_APPENDPATH: 'appendpath',
 
105
    }
 
106
_policy_value = {
 
107
    None: POLICY_NONE,
 
108
    'none': POLICY_NONE,
 
109
    'norecurse': POLICY_NORECURSE,
 
110
    'appendpath': POLICY_APPENDPATH,
 
111
    }
 
112
 
 
113
 
 
114
STORE_LOCATION = POLICY_NONE
 
115
STORE_LOCATION_NORECURSE = POLICY_NORECURSE
 
116
STORE_LOCATION_APPENDPATH = POLICY_APPENDPATH
 
117
STORE_BRANCH = 3
 
118
STORE_GLOBAL = 4
 
119
 
 
120
 
96
121
class ConfigObj(configobj.ConfigObj):
97
122
 
98
123
    def get_bool(self, section, key):
278
303
        """Override this to define the section used by the config."""
279
304
        return "DEFAULT"
280
305
 
 
306
    def _get_option_policy(self, section, option_name):
 
307
        """Return the policy for the given (section, option_name) pair."""
 
308
        return POLICY_NONE
 
309
 
281
310
    def _get_signature_checking(self):
282
311
        """See Config._get_signature_checking."""
283
312
        policy = self._get_user_option('check_signatures')
298
327
        """See Config._get_user_option."""
299
328
        for (section, extra_path) in self._get_matching_sections():
300
329
            try:
301
 
                return self._get_parser().get_value(section, option_name)
 
330
                value = self._get_parser().get_value(section, option_name)
302
331
            except KeyError:
303
 
                pass
 
332
                continue
 
333
            policy = self._get_option_policy(section, option_name)
 
334
            if policy == POLICY_NONE:
 
335
                return value
 
336
            elif policy == POLICY_NORECURSE:
 
337
                # norecurse items only apply to the exact path
 
338
                if extra_path:
 
339
                    continue
 
340
                else:
 
341
                    return value
 
342
            elif policy == POLICY_APPENDPATH:
 
343
                if extra_path:
 
344
                    value = urlutils.join(value, extra_path)
 
345
                return value
 
346
            else:
 
347
                raise AssertionError('Unexpected config policy %r' % policy)
304
348
        else:
305
349
            return None
306
350
 
431
475
            # if section is longer, no match.
432
476
            if len(section_names) > len(location_names):
433
477
                continue
434
 
            # if path is longer, and recurse is not true, no match
435
 
            if len(section_names) < len(location_names):
436
 
                try:
437
 
                    if not self._get_parser()[section].as_bool('recurse'):
438
 
                        continue
439
 
                except KeyError:
440
 
                    pass
441
478
            matches.append((len(section_names), section,
442
479
                            '/'.join(location_names[len(section_names):])))
443
480
        matches.sort(reverse=True)
452
489
                pass
453
490
        return sections
454
491
 
455
 
    def set_user_option(self, option, value):
 
492
    def _get_option_policy(self, section, option_name):
 
493
        """Return the policy for the given (section, option_name) pair."""
 
494
        # check for the old 'recurse=False' flag
 
495
        try:
 
496
            recurse = self._get_parser()[section].as_bool('recurse')
 
497
        except KeyError:
 
498
            recurse = True
 
499
        if not recurse:
 
500
            return POLICY_NORECURSE
 
501
 
 
502
        policy_key = option_name + ':policy'
 
503
        try:
 
504
            policy_name = self._get_parser()[section][policy_key]
 
505
        except KeyError:
 
506
            policy_name = None
 
507
 
 
508
        return _policy_value[policy_name]
 
509
 
 
510
    def _set_option_policy(self, section, option_name, option_policy):
 
511
        """Set the policy for the given option name in the given section."""
 
512
        # The old recurse=False option affects all options in the
 
513
        # section.  To handle multiple policies in the section, we
 
514
        # need to convert it to a policy_norecurse key.
 
515
        try:
 
516
            recurse = self._get_parser()[section].as_bool('recurse')
 
517
        except KeyError:
 
518
            pass
 
519
        else:
 
520
            symbol_versioning.warn(
 
521
                'The recurse option is deprecated as of 0.14.  '
 
522
                'The section "%s" has been converted to use policies.'
 
523
                % section,
 
524
                DeprecationWarning)
 
525
            del self._get_parser()[section]['recurse']
 
526
            if not recurse:
 
527
                for key in self._get_parser()[section].keys():
 
528
                    if not key.endswith(':policy'):
 
529
                        self._get_parser()[section][key +
 
530
                                                    ':policy'] = 'norecurse'
 
531
 
 
532
        policy_key = option_name + ':policy'
 
533
        policy_name = _policy_name[option_policy]
 
534
        if policy_name is not None:
 
535
            self._get_parser()[section][policy_key] = policy_name
 
536
        else:
 
537
            if policy_key in self._get_parser()[section]:
 
538
                del self._get_parser()[section][policy_key]
 
539
 
 
540
    def set_user_option(self, option, value, store=STORE_LOCATION):
456
541
        """Save option and its value in the configuration."""
 
542
        assert store in [STORE_LOCATION,
 
543
                         STORE_LOCATION_NORECURSE,
 
544
                         STORE_LOCATION_APPENDPATH], 'bad storage policy'
457
545
        # FIXME: RBC 20051029 This should refresh the parser and also take a
458
546
        # file lock on locations.conf.
459
547
        conf_dir = os.path.dirname(self._get_filename())
467
555
        elif location + '/' in self._get_parser():
468
556
            location = location + '/'
469
557
        self._get_parser()[location][option]=value
 
558
        # the allowed values of store match the config policies
 
559
        self._set_option_policy(location, option, store)
470
560
        self._get_parser().write(file(self._get_filename(), 'wb'))
471
561
 
472
562
 
547
637
                return value
548
638
        return None
549
639
 
550
 
    def set_user_option(self, name, value, local=False):
551
 
        if local is True:
552
 
            self._get_location_config().set_user_option(name, value)
553
 
        else:
 
640
    def set_user_option(self, name, value, store=STORE_BRANCH):
 
641
        if store == STORE_BRANCH:
554
642
            self._get_branch_data_config().set_option(value, name)
555
 
 
 
643
        elif store == STORE_GLOBAL:
 
644
            self._get_global_config().set_user_option(name, value)
 
645
        else:
 
646
            self._get_location_config().set_user_option(name, value, store)
556
647
 
557
648
    def _gpg_signing_command(self):
558
649
        """See Config.gpg_signing_command."""