~ubuntu-branches/ubuntu/karmic/bzr/karmic-proposed

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2008-08-25 19:06:49 UTC
  • mfrom: (1.1.44 upstream)
  • Revision ID: james.westby@ubuntu.com-20080825190649-pq87jonr4uvs7s0y
Tags: 1.6-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
440
440
 
441
441
    def set_user_option(self, option, value):
442
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):
443
465
        # FIXME: RBC 20051029 This should refresh the parser and also take a
444
466
        # file lock on bazaar.conf.
445
467
        conf_dir = os.path.dirname(self._get_filename())
446
468
        ensure_config_dir_exists(conf_dir)
447
 
        if 'DEFAULT' not in self._get_parser():
448
 
            self._get_parser()['DEFAULT'] = {}
449
 
        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):
450
473
        f = open(self._get_filename(), 'wb')
451
474
        self._get_parser().write(f)
452
475
        f.close()
641
664
    def _get_user_id(self):
642
665
        """Return the full user id for the branch.
643
666
    
644
 
        e.g. "John Hacker <jhacker@foo.org>"
 
667
        e.g. "John Hacker <jhacker@example.com>"
645
668
        This is looked up in the email controlfile for the branch.
646
669
        """
647
670
        try:
648
 
            return (self.branch.control_files._transport.get_bytes("email")
 
671
            return (self.branch._transport.get_bytes("email")
649
672
                    .decode(bzrlib.user_encoding)
650
673
                    .rstrip("\r\n"))
651
674
        except errors.NoSuchFile, e:
895
918
    # XXX: Really needs a better name, as this is not part of the tree! -- mbp 20080507
896
919
 
897
920
    def __init__(self, branch):
898
 
        transport = branch.control_files._transport
899
 
        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')
900
925
        self.branch = branch
901
926
 
902
927
    def _get_parser(self, file=None):
1090
1115
        credentials = self.get_credentials(scheme, host, port, user, path)
1091
1116
        if credentials is not None:
1092
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
1093
1123
        else:
1094
1124
            password = None
1095
1125
        # Prompt user only if we could't find a password
1096
1126
        if password is None:
1097
1127
            if prompt is None:
1098
 
                # Create a default prompt suitable for most of the cases
 
1128
                # Create a default prompt suitable for most cases
1099
1129
                prompt = '%s' % scheme.upper() + ' %(user)s@%(host)s password'
1100
1130
            # Special handling for optional fields in the prompt
1101
1131
            if port is not None:
1110
1140
        return credentials
1111
1141
 
1112
1142
 
 
1143
class BzrDirConfig(object):
 
1144
 
 
1145
    def __init__(self, transport):
 
1146
        self._config = TransportConfig(transport, 'control.conf')
 
1147
 
 
1148
    def set_default_stack_on(self, value):
 
1149
        """Set the default stacking location.
 
1150
 
 
1151
        It may be set to a location, or None.
 
1152
 
 
1153
        This policy affects all branches contained by this bzrdir, except for
 
1154
        those under repositories.
 
1155
        """
 
1156
        if value is None:
 
1157
            self._config.set_option('', 'default_stack_on')
 
1158
        else:
 
1159
            self._config.set_option(value, 'default_stack_on')
 
1160
 
 
1161
    def get_default_stack_on(self):
 
1162
        """Return the default stacking location.
 
1163
 
 
1164
        This will either be a location, or None.
 
1165
 
 
1166
        This policy affects all branches contained by this bzrdir, except for
 
1167
        those under repositories.
 
1168
        """
 
1169
        value = self._config.get_option('default_stack_on')
 
1170
        if value == '':
 
1171
            value = None
 
1172
        return value
 
1173
 
 
1174
 
1113
1175
class TransportConfig(object):
1114
1176
    """A Config that reads/writes a config file on a Transport.
1115
1177