~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Robey Pointer
  • Date: 2006-07-01 19:03:33 UTC
  • mfrom: (1829 +trunk)
  • mto: This revision was merged to the branch mainline in revision 1830.
  • Revision ID: robey@lag.net-20060701190333-f58465aec4bd3412
merge from bzr.dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
227
227
    def _get_alias(self, value):
228
228
        pass
229
229
 
 
230
    def get_nickname(self):
 
231
        return self._get_nickname()
 
232
 
 
233
    def _get_nickname(self):
 
234
        return None
 
235
 
230
236
 
231
237
class IniBasedConfig(Config):
232
238
    """A configuration policy that draws from ini files."""
255
261
            return self._string_to_signature_policy(policy)
256
262
 
257
263
    def _get_signing_policy(self):
258
 
        """See Config._get_signature_checking."""
 
264
        """See Config._get_signing_policy"""
259
265
        policy = self._get_user_option('create_signatures')
260
266
        if policy:
261
267
            return self._string_to_signing_policy(policy)
318
324
        except KeyError:
319
325
            pass
320
326
 
 
327
    def _get_nickname(self):
 
328
        return self.get_user_option('nickname')
 
329
 
321
330
 
322
331
class GlobalConfig(IniBasedConfig):
323
332
    """The configuration that should be used for a specific location."""
339
348
            warning('Please rename branches.conf to locations.conf')
340
349
            name_generator = branches_config_filename
341
350
        super(LocationConfig, self).__init__(name_generator)
342
 
        self._global_config = None
343
351
        self.location = location
344
352
 
345
 
    def _get_global_config(self):
346
 
        if self._global_config is None:
347
 
            self._global_config = GlobalConfig()
348
 
        return self._global_config
349
 
 
350
353
    def _get_section(self):
351
354
        """Get the section we should look in for config items.
352
355
 
388
391
        matches.sort(reverse=True)
389
392
        return matches[0][1]
390
393
 
391
 
    def _gpg_signing_command(self):
392
 
        """See Config.gpg_signing_command."""
393
 
        command = super(LocationConfig, self)._gpg_signing_command()
394
 
        if command is not None:
395
 
            return command
396
 
        return self._get_global_config()._gpg_signing_command()
397
 
 
398
 
    def _log_format(self):
399
 
        """See Config.log_format."""
400
 
        command = super(LocationConfig, self)._log_format()
401
 
        if command is not None:
402
 
            return command
403
 
        return self._get_global_config()._log_format()
404
 
 
405
 
    def _get_user_id(self):
406
 
        user_id = super(LocationConfig, self)._get_user_id()
407
 
        if user_id is not None:
408
 
            return user_id
409
 
        return self._get_global_config()._get_user_id()
410
 
 
411
 
    def _get_user_option(self, option_name):
412
 
        """See Config._get_user_option."""
413
 
        option_value = super(LocationConfig, 
414
 
                             self)._get_user_option(option_name)
415
 
        if option_value is not None:
416
 
            return option_value
417
 
        return self._get_global_config()._get_user_option(option_name)
418
 
 
419
 
    def _get_signature_checking(self):
420
 
        """See Config._get_signature_checking."""
421
 
        check = super(LocationConfig, self)._get_signature_checking()
422
 
        if check is not None:
423
 
            return check
424
 
        return self._get_global_config()._get_signature_checking()
425
 
 
426
 
    def _get_signing_policy(self):
427
 
        """See Config._get_signing_policy."""
428
 
        sign = super(LocationConfig, self)._get_signing_policy()
429
 
        if sign is not None:
430
 
            return sign
431
 
        return self._get_global_config()._get_signing_policy()
432
 
 
433
 
    def _post_commit(self):
434
 
        """See Config.post_commit."""
435
 
        hook = self._get_user_option('post_commit')
436
 
        if hook is not None:
437
 
            return hook
438
 
        return self._get_global_config()._post_commit()
439
 
 
440
394
    def set_user_option(self, option, value):
441
395
        """Save option and its value in the configuration."""
442
396
        # FIXME: RBC 20051029 This should refresh the parser and also take a
458
412
class BranchConfig(Config):
459
413
    """A configuration object giving the policy for a branch."""
460
414
 
 
415
    def _get_branch_data_config(self):
 
416
        if self._branch_data_config is None:
 
417
            self._branch_data_config = TreeConfig(self.branch)
 
418
        return self._branch_data_config
 
419
 
461
420
    def _get_location_config(self):
462
421
        if self._location_config is None:
463
422
            self._location_config = LocationConfig(self.branch.base)
464
423
        return self._location_config
465
424
 
 
425
    def _get_global_config(self):
 
426
        if self._global_config is None:
 
427
            self._global_config = GlobalConfig()
 
428
        return self._global_config
 
429
 
 
430
    def _get_best_value(self, option_name):
 
431
        """This returns a user option from local, tree or global config.
 
432
 
 
433
        They are tried in that order.  Use get_safe_value if trusted values
 
434
        are necessary.
 
435
        """
 
436
        for source in self.option_sources:
 
437
            value = getattr(source(), option_name)()
 
438
            if value is not None:
 
439
                return value
 
440
        return None
 
441
 
 
442
    def _get_safe_value(self, option_name):
 
443
        """This variant of get_best_value never returns untrusted values.
 
444
        
 
445
        It does not return values from the branch data, because the branch may
 
446
        not be controlled by the user.
 
447
 
 
448
        We may wish to allow locations.conf to control whether branches are
 
449
        trusted in the future.
 
450
        """
 
451
        for source in (self._get_location_config, self._get_global_config):
 
452
            value = getattr(source(), option_name)()
 
453
            if value is not None:
 
454
                return value
 
455
        return None
 
456
 
466
457
    def _get_user_id(self):
467
458
        """Return the full user id for the branch.
468
459
    
477
468
        except errors.NoSuchFile, e:
478
469
            pass
479
470
        
480
 
        return self._get_location_config()._get_user_id()
 
471
        return self._get_best_value('_get_user_id')
481
472
 
482
473
    def _get_signature_checking(self):
483
474
        """See Config._get_signature_checking."""
484
 
        return self._get_location_config()._get_signature_checking()
 
475
        return self._get_best_value('_get_signature_checking')
485
476
 
486
477
    def _get_signing_policy(self):
487
478
        """See Config._get_signing_policy."""
488
 
        return self._get_location_config()._get_signing_policy()
 
479
        return self._get_best_value('_get_signing_policy')
489
480
 
490
481
    def _get_user_option(self, option_name):
491
482
        """See Config._get_user_option."""
492
 
        return self._get_location_config()._get_user_option(option_name)
 
483
        for source in self.option_sources:
 
484
            value = source()._get_user_option(option_name)
 
485
            if value is not None:
 
486
                return value
 
487
        return None
 
488
 
 
489
    def set_user_option(self, name, value, local=False):
 
490
        if local is True:
 
491
            self._get_location_config().set_user_option(name, value)
 
492
        else:
 
493
            self._get_branch_data_config().set_option(value, name)
 
494
 
493
495
 
494
496
    def _gpg_signing_command(self):
495
497
        """See Config.gpg_signing_command."""
496
 
        return self._get_location_config()._gpg_signing_command()
 
498
        return self._get_safe_value('_gpg_signing_command')
497
499
        
498
500
    def __init__(self, branch):
499
501
        super(BranchConfig, self).__init__()
500
502
        self._location_config = None
 
503
        self._branch_data_config = None
 
504
        self._global_config = None
501
505
        self.branch = branch
 
506
        self.option_sources = (self._get_location_config, 
 
507
                               self._get_branch_data_config,
 
508
                               self._get_global_config)
502
509
 
503
510
    def _post_commit(self):
504
511
        """See Config.post_commit."""
505
 
        return self._get_location_config()._post_commit()
 
512
        return self._get_safe_value('_post_commit')
 
513
 
 
514
    def _get_nickname(self):
 
515
        value = self._get_explicit_nickname()
 
516
        if value is not None:
 
517
            return value
 
518
        return self.branch.base.split('/')[-2]
 
519
 
 
520
    def has_explicit_nickname(self):
 
521
        """Return true if a nickname has been explicitly assigned."""
 
522
        return self._get_explicit_nickname() is not None
 
523
 
 
524
    def _get_explicit_nickname(self):
 
525
        return self._get_best_value('_get_nickname')
506
526
 
507
527
    def _log_format(self):
508
528
        """See Config.log_format."""
509
 
        return self._get_location_config()._log_format()
 
529
        return self._get_best_value('_log_format')
510
530
 
511
531
 
512
532
def ensure_config_dir_exists(path=None):
541
561
        if base is None:
542
562
            base = os.environ.get('HOME', None)
543
563
        if base is None:
544
 
            raise BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
 
564
            raise errors.BzrError('You must have one of BZR_HOME, APPDATA, or HOME set')
545
565
        return pathjoin(base, 'bazaar', '2.0')
546
566
    else:
547
567
        # cygwin, linux, and darwin all have a $HOME directory
628
648
    return m.group(0)
629
649
 
630
650
 
631
 
class TreeConfig(object):
 
651
class TreeConfig(IniBasedConfig):
632
652
    """Branch configuration data associated with its contents, not location"""
633
653
    def __init__(self, branch):
634
654
        self.branch = branch
635
655
 
 
656
    def _get_parser(self, file=None):
 
657
        if file is not None:
 
658
            return IniBasedConfig._get_parser(file)
 
659
        return self._get_config()
 
660
 
636
661
    def _get_config(self):
637
662
        try:
638
663
            obj = ConfigObj(self.branch.control_files.get('branch.conf'),