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
345
def _get_global_config(self):
346
if self._global_config is None:
347
self._global_config = GlobalConfig()
348
return self._global_config
350
353
def _get_section(self):
351
354
"""Get the section we should look in for config items.
388
391
matches.sort(reverse=True)
389
392
return matches[0][1]
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:
396
return self._get_global_config()._gpg_signing_command()
398
def _log_format(self):
399
"""See Config.log_format."""
400
command = super(LocationConfig, self)._log_format()
401
if command is not None:
403
return self._get_global_config()._log_format()
405
def _get_user_id(self):
406
user_id = super(LocationConfig, self)._get_user_id()
407
if user_id is not None:
409
return self._get_global_config()._get_user_id()
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:
417
return self._get_global_config()._get_user_option(option_name)
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:
424
return self._get_global_config()._get_signature_checking()
426
def _get_signing_policy(self):
427
"""See Config._get_signing_policy."""
428
sign = super(LocationConfig, self)._get_signing_policy()
431
return self._get_global_config()._get_signing_policy()
433
def _post_commit(self):
434
"""See Config.post_commit."""
435
hook = self._get_user_option('post_commit')
438
return self._get_global_config()._post_commit()
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."""
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
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
425
def _get_global_config(self):
426
if self._global_config is None:
427
self._global_config = GlobalConfig()
428
return self._global_config
430
def _get_best_value(self, option_name):
431
"""This returns a user option from local, tree or global config.
433
They are tried in that order. Use get_safe_value if trusted values
436
for source in self.option_sources:
437
value = getattr(source(), option_name)()
438
if value is not None:
442
def _get_safe_value(self, option_name):
443
"""This variant of get_best_value never returns untrusted values.
445
It does not return values from the branch data, because the branch may
446
not be controlled by the user.
448
We may wish to allow locations.conf to control whether branches are
449
trusted in the future.
451
for source in (self._get_location_config, self._get_global_config):
452
value = getattr(source(), option_name)()
453
if value is not None:
466
457
def _get_user_id(self):
467
458
"""Return the full user id for the branch.
477
468
except errors.NoSuchFile, e:
480
return self._get_location_config()._get_user_id()
471
return self._get_best_value('_get_user_id')
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')
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')
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:
489
def set_user_option(self, name, value, local=False):
491
self._get_location_config().set_user_option(name, value)
493
self._get_branch_data_config().set_option(value, name)
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')
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)
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')
514
def _get_nickname(self):
515
value = self._get_explicit_nickname()
516
if value is not None:
518
return self.branch.base.split('/')[-2]
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
524
def _get_explicit_nickname(self):
525
return self._get_best_value('_get_nickname')
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')
512
532
def ensure_config_dir_exists(path=None):
542
562
base = os.environ.get('HOME', 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')
547
567
# cygwin, linux, and darwin all have a $HOME directory
628
648
return m.group(0)
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
656
def _get_parser(self, file=None):
658
return IniBasedConfig._get_parser(file)
659
return self._get_config()
636
661
def _get_config(self):
638
663
obj = ConfigObj(self.branch.control_files.get('branch.conf'),