~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2009-04-09 20:23:07 UTC
  • mfrom: (4265.1.4 bbc-merge)
  • Revision ID: pqm@pqm.ubuntu.com-20090409202307-n0depb16qepoe21o
(jam) Change _fetch_uses_deltas = False for CHK repos until we can
        write a better fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
146
146
class Config(object):
147
147
    """A configuration policy - what username, editor, gpg needs etc."""
148
148
 
149
 
    def __init__(self):
150
 
        super(Config, self).__init__()
151
 
 
152
149
    def get_editor(self):
153
150
        """Get the users pop up editor."""
154
151
        raise NotImplementedError
177
174
        """Get a generic option - no special process, no default."""
178
175
        return self._get_user_option(option_name)
179
176
 
180
 
    def get_user_option_as_bool(self, option_name):
181
 
        """Get a generic option as a boolean - no special process, no default.
182
 
 
183
 
        :return None if the option doesn't exist or its value can't be
184
 
            interpreted as a boolean. Returns True or False ortherwise.
185
 
        """
186
 
        s = self._get_user_option(option_name)
187
 
        return ui.bool_from_string(s)
188
 
 
189
177
    def gpg_signing_command(self):
190
178
        """What program should be used to sign signatures?"""
191
179
        result = self._gpg_signing_command()
208
196
        """See log_format()."""
209
197
        return None
210
198
 
 
199
    def __init__(self):
 
200
        super(Config, self).__init__()
 
201
 
211
202
    def post_commit(self):
212
203
        """An ordered list of python functions to call.
213
204
 
308
299
class IniBasedConfig(Config):
309
300
    """A configuration policy that draws from ini files."""
310
301
 
311
 
    def __init__(self, get_filename):
312
 
        super(IniBasedConfig, self).__init__()
313
 
        self._get_filename = get_filename
314
 
        self._parser = None
315
 
 
316
302
    def _get_parser(self, file=None):
317
303
        if self._parser is not None:
318
304
            return self._parser
395
381
        """See Config.log_format."""
396
382
        return self._get_user_option('log_format')
397
383
 
 
384
    def __init__(self, get_filename):
 
385
        super(IniBasedConfig, self).__init__()
 
386
        self._get_filename = get_filename
 
387
        self._parser = None
 
388
 
398
389
    def _post_commit(self):
399
390
        """See Config.post_commit."""
400
391
        return self._get_user_option('post_commit')
939
930
            return self._config.get_option(name, section, default)
940
931
        finally:
941
932
            self.branch.unlock()
 
933
        return result
942
934
 
943
935
    def set_option(self, value, name, section=None):
944
936
        """Set a per-branch configuration option"""
1087
1079
                trace.mutter("Using authentication section: %r", auth_def_name)
1088
1080
            break
1089
1081
 
1090
 
        if credentials is None:
1091
 
            # No credentials were found in authentication.conf, try the fallback
1092
 
            # credentials stores.
1093
 
            credentials = credential_store_registry.get_fallback_credentials(
1094
 
                scheme, host, port, user, path, realm)
1095
 
 
1096
1082
        return credentials
1097
1083
 
1098
1084
    def set_credentials(self, name, host, user, scheme=None, password=None,
1140
1126
        config.update({name: values})
1141
1127
        self._save()
1142
1128
 
1143
 
    def get_user(self, scheme, host, port=None, realm=None, path=None,
1144
 
                 prompt=None, ask=False, default=None):
 
1129
    def get_user(self, scheme, host, port=None,
 
1130
                 realm=None, path=None, prompt=None):
1145
1131
        """Get a user from authentication file.
1146
1132
 
1147
1133
        :param scheme: protocol
1154
1140
 
1155
1141
        :param path: the absolute path on the server (optional)
1156
1142
 
1157
 
        :param ask: Ask the user if there is no explicitly configured username 
1158
 
                    (optional)
1159
 
 
1160
 
        :param default: The username returned if none is defined (optional).
1161
 
 
1162
1143
        :return: The found user.
1163
1144
        """
1164
1145
        credentials = self.get_credentials(scheme, host, port, user=None,
1167
1148
            user = credentials['user']
1168
1149
        else:
1169
1150
            user = None
1170
 
        if user is None:
1171
 
            if ask:
1172
 
                if prompt is None:
1173
 
                    # Create a default prompt suitable for most cases
1174
 
                    prompt = scheme.upper() + ' %(host)s username'
1175
 
                # Special handling for optional fields in the prompt
1176
 
                if port is not None:
1177
 
                    prompt_host = '%s:%d' % (host, port)
1178
 
                else:
1179
 
                    prompt_host = host
1180
 
                user = ui.ui_factory.get_username(prompt, host=prompt_host)
1181
 
            else:
1182
 
                user = default
1183
1151
        return user
1184
1152
 
1185
1153
    def get_password(self, scheme, host, user, port=None,
1240
1208
    A credential store provides access to credentials via the password_encoding
1241
1209
    field in authentication.conf sections.
1242
1210
 
1243
 
    Except for stores provided by bzr itself, most stores are expected to be
 
1211
    Except for stores provided by bzr itself,most stores are expected to be
1244
1212
    provided by plugins that will therefore use
1245
1213
    register_lazy(password_encoding, module_name, member_name, help=help,
1246
 
    fallback=fallback) to install themselves.
1247
 
 
1248
 
    A fallback credential store is one that is queried if no credentials can be
1249
 
    found via authentication.conf.
 
1214
    info=info) to install themselves.
1250
1215
    """
1251
1216
 
1252
1217
    def get_credential_store(self, encoding=None):
1255
1220
            cs = cs()
1256
1221
        return cs
1257
1222
 
1258
 
    def is_fallback(self, name):
1259
 
        """Check if the named credentials store should be used as fallback."""
1260
 
        return self.get_info(name)
1261
 
 
1262
 
    def get_fallback_credentials(self, scheme, host, port=None, user=None,
1263
 
                                 path=None, realm=None):
1264
 
        """Request credentials from all fallback credentials stores.
1265
 
 
1266
 
        The first credentials store that can provide credentials wins.
1267
 
        """
1268
 
        credentials = None
1269
 
        for name in self.keys():
1270
 
            if not self.is_fallback(name):
1271
 
                continue
1272
 
            cs = self.get_credential_store(name)
1273
 
            credentials = cs.get_credentials(scheme, host, port, user,
1274
 
                                             path, realm)
1275
 
            if credentials is not None:
1276
 
                # We found some credentials
1277
 
                break
1278
 
        return credentials
1279
 
 
1280
 
    def register(self, key, obj, help=None, override_existing=False,
1281
 
                 fallback=False):
1282
 
        """Register a new object to a name.
1283
 
 
1284
 
        :param key: This is the key to use to request the object later.
1285
 
        :param obj: The object to register.
1286
 
        :param help: Help text for this entry. This may be a string or
1287
 
                a callable. If it is a callable, it should take two
1288
 
                parameters (registry, key): this registry and the key that
1289
 
                the help was registered under.
1290
 
        :param override_existing: Raise KeyErorr if False and something has
1291
 
                already been registered for that key. If True, ignore if there
1292
 
                is an existing key (always register the new value).
1293
 
        :param fallback: Whether this credential store should be 
1294
 
                used as fallback.
1295
 
        """
1296
 
        return super(CredentialStoreRegistry,
1297
 
                     self).register(key, obj, help, info=fallback,
1298
 
                                    override_existing=override_existing)
1299
 
 
1300
 
    def register_lazy(self, key, module_name, member_name,
1301
 
                      help=None, override_existing=False,
1302
 
                      fallback=False):
1303
 
        """Register a new credential store to be loaded on request.
1304
 
 
1305
 
        :param module_name: The python path to the module. Such as 'os.path'.
1306
 
        :param member_name: The member of the module to return.  If empty or
1307
 
                None, get() will return the module itself.
1308
 
        :param help: Help text for this entry. This may be a string or
1309
 
                a callable.
1310
 
        :param override_existing: If True, replace the existing object
1311
 
                with the new one. If False, if there is already something
1312
 
                registered with the same key, raise a KeyError
1313
 
        :param fallback: Whether this credential store should be 
1314
 
                used as fallback.
1315
 
        """
1316
 
        return super(CredentialStoreRegistry, self).register_lazy(
1317
 
            key, module_name, member_name, help,
1318
 
            info=fallback, override_existing=override_existing)
1319
 
 
1320
1223
 
1321
1224
credential_store_registry = CredentialStoreRegistry()
1322
1225
 
1325
1228
    """An abstract class to implement storage for credentials"""
1326
1229
 
1327
1230
    def decode_password(self, credentials):
1328
 
        """Returns a clear text password for the provided credentials."""
 
1231
        """Returns a password for the provided credentials in clear text."""
1329
1232
        raise NotImplementedError(self.decode_password)
1330
1233
 
1331
 
    def get_credentials(self, scheme, host, port=None, user=None, path=None,
1332
 
                        realm=None):
1333
 
        """Return the matching credentials from this credential store.
1334
 
 
1335
 
        This method is only called on fallback credential stores.
1336
 
        """
1337
 
        raise NotImplementedError(self.get_credentials)
1338
 
 
1339
 
 
1340
1234
 
1341
1235
class PlainTextCredentialStore(CredentialStore):
1342
1236
    """Plain text credential store for the authentication.conf file."""
1353
1247
 
1354
1248
class BzrDirConfig(object):
1355
1249
 
1356
 
    def __init__(self, bzrdir):
1357
 
        self._bzrdir = bzrdir
1358
 
        self._config = bzrdir._get_config()
 
1250
    def __init__(self, transport):
 
1251
        self._config = TransportConfig(transport, 'control.conf')
1359
1252
 
1360
1253
    def set_default_stack_on(self, value):
1361
1254
        """Set the default stacking location.
1365
1258
        This policy affects all branches contained by this bzrdir, except for
1366
1259
        those under repositories.
1367
1260
        """
1368
 
        if self._config is None:
1369
 
            raise errors.BzrError("Cannot set configuration in %s" % self._bzrdir)
1370
1261
        if value is None:
1371
1262
            self._config.set_option('', 'default_stack_on')
1372
1263
        else:
1380
1271
        This policy affects all branches contained by this bzrdir, except for
1381
1272
        those under repositories.
1382
1273
        """
1383
 
        if self._config is None:
1384
 
            return None
1385
1274
        value = self._config.get_option('default_stack_on')
1386
1275
        if value == '':
1387
1276
            value = None
1432
1321
            configobj.setdefault(section, {})[name] = value
1433
1322
        self._set_configobj(configobj)
1434
1323
 
1435
 
    def _get_config_file(self):
 
1324
    def _get_configobj(self):
1436
1325
        try:
1437
 
            return self._transport.get(self._filename)
 
1326
            return ConfigObj(self._transport.get(self._filename),
 
1327
                             encoding='utf-8')
1438
1328
        except errors.NoSuchFile:
1439
 
            return StringIO()
1440
 
 
1441
 
    def _get_configobj(self):
1442
 
        return ConfigObj(self._get_config_file(), encoding='utf-8')
 
1329
            return ConfigObj(encoding='utf-8')
1443
1330
 
1444
1331
    def _set_configobj(self, configobj):
1445
1332
        out_file = StringIO()