/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

Merge with stacked-fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
151
151
            mail_client_class = {
152
152
                None: mail_client.DefaultMail,
153
153
                # Specific clients
 
154
                'emacsclient': mail_client.EmacsMail,
154
155
                'evolution': mail_client.Evolution,
155
156
                'kmail': mail_client.KMail,
156
157
                'mutt': mail_client.Mutt,
439
440
 
440
441
    def set_user_option(self, option, value):
441
442
        """Save option and its value in the configuration."""
 
443
        self._set_option(option, value, 'DEFAULT')
 
444
 
 
445
    def get_aliases(self):
 
446
        """Return the aliases section."""
 
447
        if 'ALIASES' in self._get_parser():
 
448
            return self._get_parser()['ALIASES']
 
449
        else:
 
450
            return {}
 
451
 
 
452
    def set_alias(self, alias_name, alias_command):
 
453
        """Save the alias in the configuration."""
 
454
        self._set_option(alias_name, alias_command, 'ALIASES')
 
455
 
 
456
    def unset_alias(self, alias_name):
 
457
        """Unset an existing alias."""
 
458
        aliases = self._get_parser().get('ALIASES')
 
459
        if not aliases or alias_name not in aliases:
 
460
            raise errors.NoSuchAlias(alias_name)
 
461
        del aliases[alias_name]
 
462
        self._write_config_file()
 
463
 
 
464
    def _set_option(self, option, value, section):
442
465
        # FIXME: RBC 20051029 This should refresh the parser and also take a
443
466
        # file lock on bazaar.conf.
444
467
        conf_dir = os.path.dirname(self._get_filename())
445
468
        ensure_config_dir_exists(conf_dir)
446
 
        if 'DEFAULT' not in self._get_parser():
447
 
            self._get_parser()['DEFAULT'] = {}
448
 
        self._get_parser()['DEFAULT'][option] = value
 
469
        self._get_parser().setdefault(section, {})[option] = value
 
470
        self._write_config_file()
 
471
 
 
472
    def _write_config_file(self):
449
473
        f = open(self._get_filename(), 'wb')
450
474
        self._get_parser().write(f)
451
475
        f.close()
569
593
 
570
594
    def set_user_option(self, option, value, store=STORE_LOCATION):
571
595
        """Save option and its value in the configuration."""
572
 
        assert store in [STORE_LOCATION,
 
596
        if store not in [STORE_LOCATION,
573
597
                         STORE_LOCATION_NORECURSE,
574
 
                         STORE_LOCATION_APPENDPATH], 'bad storage policy'
 
598
                         STORE_LOCATION_APPENDPATH]:
 
599
            raise ValueError('bad storage policy %r for %r' %
 
600
                (store, option))
575
601
        # FIXME: RBC 20051029 This should refresh the parser and also take a
576
602
        # file lock on locations.conf.
577
603
        conf_dir = os.path.dirname(self._get_filename())
638
664
    def _get_user_id(self):
639
665
        """Return the full user id for the branch.
640
666
    
641
 
        e.g. "John Hacker <jhacker@foo.org>"
 
667
        e.g. "John Hacker <jhacker@example.com>"
642
668
        This is looked up in the email controlfile for the branch.
643
669
        """
644
670
        try:
645
 
            return (self.branch.control_files.get_utf8("email") 
646
 
                    .read()
 
671
            return (self.branch._transport.get_bytes("email")
647
672
                    .decode(bzrlib.user_encoding)
648
673
                    .rstrip("\r\n"))
649
674
        except errors.NoSuchFile, e:
890
915
class TreeConfig(IniBasedConfig):
891
916
    """Branch configuration data associated with its contents, not location"""
892
917
 
 
918
    # XXX: Really needs a better name, as this is not part of the tree! -- mbp 20080507
 
919
 
893
920
    def __init__(self, branch):
894
 
        transport = branch.control_files._transport
895
 
        self._config = TransportConfig(transport, 'branch.conf')
 
921
        # XXX: Really this should be asking the branch for its configuration
 
922
        # data, rather than relying on a Transport, so that it can work 
 
923
        # more cleanly with a RemoteBranch that has no transport.
 
924
        self._config = TransportConfig(branch._transport, 'branch.conf')
896
925
        self.branch = branch
897
926
 
898
927
    def _get_parser(self, file=None):
990
1019
        """
991
1020
        credentials = None
992
1021
        for auth_def_name, auth_def in self._get_config().items():
 
1022
            if type(auth_def) is not configobj.Section:
 
1023
                raise ValueError("%s defined outside a section" % auth_def_name)
 
1024
 
993
1025
            a_scheme, a_host, a_user, a_path = map(
994
1026
                auth_def.get, ['scheme', 'host', 'user', 'path'])
995
1027
 
1027
1059
                # Can't find a user
1028
1060
                continue
1029
1061
            credentials = dict(name=auth_def_name,
1030
 
                               user=a_user, password=auth_def['password'],
 
1062
                               user=a_user,
 
1063
                               password=auth_def.get('password', None),
1031
1064
                               verify_certificates=a_verify_certificates)
1032
1065
            self.decode_password(credentials,
1033
1066
                                 auth_def.get('password_encoding', None))
1082
1115
        credentials = self.get_credentials(scheme, host, port, user, path)
1083
1116
        if credentials is not None:
1084
1117
            password = credentials['password']
 
1118
            if password is not None and scheme is 'ssh':
 
1119
                trace.warning('password ignored in section [%s],'
 
1120
                              ' use an ssh agent instead'
 
1121
                              % credentials['name'])
 
1122
                password = None
1085
1123
        else:
1086
1124
            password = None
1087
1125
        # Prompt user only if we could't find a password
1088
1126
        if password is None:
1089
1127
            if prompt is None:
1090
 
                # Create a default prompt suitable for most of the cases
 
1128
                # Create a default prompt suitable for most cases
1091
1129
                prompt = '%s' % scheme.upper() + ' %(user)s@%(host)s password'
1092
1130
            # Special handling for optional fields in the prompt
1093
1131
            if port is not None:
1135
1173
 
1136
1174
 
1137
1175
class TransportConfig(object):
1138
 
    """A configuration representation that stores data on a Transport.
 
1176
    """A Config that reads/writes a config file on a Transport.
1139
1177
 
1140
1178
    It is a low-level object that considers config data to be name/value pairs
1141
1179
    that may be associated with a section.  Assigning meaning to the these