~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: 2011-08-15 13:49:38 UTC
  • mfrom: (6060.3.1 fix-last-revno-args)
  • Revision ID: pqm@pqm.ubuntu.com-20110815134938-4fuo63g4v2hj8jdt
(jelmer) Cope with the localhost having the name 'localhost' when running
 the test suite. (Jelmer Vernooij)

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
log_format=name-of-format
32
32
validate_signatures_in_log=true|false(default)
33
33
acceptable_keys=pattern1,pattern2
 
34
gpg_signing_key=amy@example.com
34
35
 
35
36
in locations.conf, you specify the url of a branch and options for it.
36
37
Wildcards may be used - * and ? as normal in shell completion. Options
88
89
    bzrdir,
89
90
    debug,
90
91
    errors,
 
92
    lazy_regex,
91
93
    lockdir,
92
94
    mail_client,
93
95
    mergetools,
170
172
# FIXME: Until we can guarantee that each config file is loaded once and
171
173
# only once for a given bzrlib session, we don't want to re-read the file every
172
174
# time we query for an option so we cache the value (bad ! watch out for tests
173
 
# needing to restore the proper value).This shouldn't be part of 2.4.0 final,
174
 
# yell at mgz^W vila and the RM if this is still present at that time
175
 
# -- vila 20110219
 
175
# needing to restore the proper value). -- vila 20110219
176
176
_expand_default_value = None
177
177
def _get_expand_default_value():
178
178
    global _expand_default_value
535
535
            return True
536
536
        return False
537
537
 
 
538
    def gpg_signing_key(self):
 
539
        """GPG user-id to sign commits"""
 
540
        key = self.get_user_option('gpg_signing_key')
 
541
        if key == "default" or key == None:
 
542
            return self.user_email()
 
543
        else:
 
544
            return key
 
545
 
538
546
    def get_alias(self, value):
539
547
        return self._get_alias(value)
540
548
 
1503
1511
            raise errors.BzrError('You must have one of BZR_HOME, APPDATA,'
1504
1512
                                  ' or HOME set')
1505
1513
        return osutils.pathjoin(base, 'bazaar', '2.0')
1506
 
    elif sys.platform == 'darwin':
 
1514
    else:
 
1515
        if base is not None:
 
1516
            base = base.decode(osutils._fs_enc)
 
1517
    if sys.platform == 'darwin':
1507
1518
        if base is None:
1508
1519
            # this takes into account $HOME
1509
1520
            base = os.path.expanduser("~")
1510
1521
        return osutils.pathjoin(base, '.bazaar')
1511
1522
    else:
1512
1523
        if base is None:
1513
 
 
1514
1524
            xdg_dir = os.environ.get('XDG_CONFIG_HOME', None)
1515
1525
            if xdg_dir is None:
1516
1526
                xdg_dir = osutils.pathjoin(os.path.expanduser("~"), ".config")
1519
1529
                trace.mutter(
1520
1530
                    "Using configuration in XDG directory %s." % xdg_dir)
1521
1531
                return xdg_dir
1522
 
 
1523
1532
            base = os.path.expanduser("~")
1524
1533
        return osutils.pathjoin(base, ".bazaar")
1525
1534
 
2261
2270
    The option *values* are stored in config files and found in sections.
2262
2271
 
2263
2272
    Here we define various properties about the option itself, its default
2264
 
    value, in which config files it can be stored, etc (TBC).
 
2273
    value, how to convert it from stores, what to do when invalid values are
 
2274
    encoutered, in which config files it can be stored.
2265
2275
    """
2266
2276
 
2267
 
    def __init__(self, name, default=None):
 
2277
    def __init__(self, name, default=None, help=None, from_unicode=None,
 
2278
                 invalid=None):
 
2279
        """Build an option definition.
 
2280
 
 
2281
        :param name: the name used to refer to the option.
 
2282
 
 
2283
        :param default: the default value to use when none exist in the config
 
2284
            stores.
 
2285
 
 
2286
        :param help: a doc string to explain the option to the user.
 
2287
 
 
2288
        :param from_unicode: a callable to convert the unicode string
 
2289
            representing the option value in a store. This is not called for
 
2290
            the default value.
 
2291
 
 
2292
        :param invalid: the action to be taken when an invalid value is
 
2293
            encountered in a store. This is called only when from_unicode is
 
2294
            invoked to convert a string and returns None or raise ValueError or
 
2295
            TypeError. Accepted values are: None (ignore invalid values),
 
2296
            'warning' (emit a warning), 'error' (emit an error message and
 
2297
            terminates).
 
2298
        """
2268
2299
        self.name = name
2269
2300
        self.default = default
 
2301
        self.help = help
 
2302
        self.from_unicode = from_unicode
 
2303
        if invalid and invalid not in ('warning', 'error'):
 
2304
            raise AssertionError("%s not supported for 'invalid'" % (invalid,))
 
2305
        self.invalid = invalid
2270
2306
 
2271
2307
    def get_default(self):
2272
2308
        return self.default
2273
2309
 
2274
 
 
2275
 
# Options registry
2276
 
 
2277
 
option_registry = registry.Registry()
2278
 
 
2279
 
 
2280
 
option_registry.register(
2281
 
    'editor', Option('editor'),
2282
 
    help='The command called to launch an editor to enter a message.')
 
2310
# Predefined converters to get proper values from store
 
2311
 
 
2312
def bool_from_store(unicode_str):
 
2313
    return ui.bool_from_string(unicode_str)
 
2314
 
 
2315
 
 
2316
def int_from_store(unicode_str):
 
2317
    return int(unicode_str)
 
2318
 
 
2319
 
 
2320
def list_from_store(unicode_str):
 
2321
    # ConfigObj return '' instead of u''. Use 'str' below to catch all cases.
 
2322
    if isinstance(unicode_str, (str, unicode)):
 
2323
        if unicode_str:
 
2324
            # A single value, most probably the user forgot (or didn't care to
 
2325
            # add) the final ','
 
2326
            l = [unicode_str]
 
2327
        else:
 
2328
            # The empty string, convert to empty list
 
2329
            l = []
 
2330
    else:
 
2331
        # We rely on ConfigObj providing us with a list already
 
2332
        l = unicode_str
 
2333
    return l
 
2334
 
 
2335
 
 
2336
class OptionRegistry(registry.Registry):
 
2337
    """Register config options by their name.
 
2338
 
 
2339
    This overrides ``registry.Registry`` to simplify registration by acquiring
 
2340
    some information from the option object itself.
 
2341
    """
 
2342
 
 
2343
    def register(self, option):
 
2344
        """Register a new option to its name.
 
2345
 
 
2346
        :param option: The option to register. Its name is used as the key.
 
2347
        """
 
2348
        super(OptionRegistry, self).register(option.name, option,
 
2349
                                             help=option.help)
 
2350
 
 
2351
    def register_lazy(self, key, module_name, member_name):
 
2352
        """Register a new option to be loaded on request.
 
2353
 
 
2354
        :param key: This is the key to use to request the option later. Since
 
2355
            the registration is lazy, it should be provided and match the
 
2356
            option name.
 
2357
 
 
2358
        :param module_name: The python path to the module. Such as 'os.path'.
 
2359
 
 
2360
        :param member_name: The member of the module to return.  If empty or
 
2361
                None, get() will return the module itself.
 
2362
        """
 
2363
        super(OptionRegistry, self).register_lazy(key,
 
2364
                                                  module_name, member_name)
 
2365
 
 
2366
    def get_help(self, key=None):
 
2367
        """Get the help text associated with the given key"""
 
2368
        option = self.get(key)
 
2369
        the_help = option.help
 
2370
        if callable(the_help):
 
2371
            return the_help(self, key)
 
2372
        return the_help
 
2373
 
 
2374
 
 
2375
option_registry = OptionRegistry()
 
2376
 
 
2377
 
 
2378
# Registered options in lexicographical order
 
2379
 
 
2380
option_registry.register(
 
2381
    Option('dirstate.fdatasync', default=True, from_unicode=bool_from_store,
 
2382
           help='''
 
2383
Flush dirstate changes onto physical disk?
 
2384
 
 
2385
If true (default), working tree metadata changes are flushed through the
 
2386
OS buffers to physical disk.  This is somewhat slower, but means data
 
2387
should not be lost if the machine crashes.  See also repository.fdatasync.
 
2388
'''))
 
2389
option_registry.register(
 
2390
    Option('default_format', default='2a',
 
2391
           help='Format used when creating branches.'))
 
2392
option_registry.register(
 
2393
    Option('editor',
 
2394
           help='The command called to launch an editor to enter a message.'))
 
2395
option_registry.register(
 
2396
    Option('language',
 
2397
           help='Language to translate messages into.'))
 
2398
option_registry.register(
 
2399
    Option('output_encoding',
 
2400
           help= 'Unicode encoding for output'
 
2401
           ' (terminal encoding if not specified).'))
 
2402
option_registry.register(
 
2403
    Option('repository.fdatasync', default=True, from_unicode=bool_from_store,
 
2404
           help='''\
 
2405
Flush repository changes onto physical disk?
 
2406
 
 
2407
If true (default), repository changes are flushed through the OS buffers
 
2408
to physical disk.  This is somewhat slower, but means data should not be
 
2409
lost if the machine crashes.  See also dirstate.fdatasync.
 
2410
'''))
2283
2411
 
2284
2412
 
2285
2413
class Section(object):
2552
2680
class GlobalStore(LockableIniFileStore):
2553
2681
 
2554
2682
    def __init__(self, possible_transports=None):
2555
 
        t = transport.get_transport(config_dir(),
2556
 
                                    possible_transports=possible_transports)
 
2683
        t = transport.get_transport_from_path(
 
2684
            config_dir(), possible_transports=possible_transports)
2557
2685
        super(GlobalStore, self).__init__(t, 'bazaar.conf')
2558
2686
 
2559
2687
 
2560
2688
class LocationStore(LockableIniFileStore):
2561
2689
 
2562
2690
    def __init__(self, possible_transports=None):
2563
 
        t = transport.get_transport(config_dir(),
2564
 
                                    possible_transports=possible_transports)
 
2691
        t = transport.get_transport_from_path(
 
2692
            config_dir(), possible_transports=possible_transports)
2565
2693
        super(LocationStore, self).__init__(t, 'locations.conf')
2566
2694
 
2567
2695
 
2733
2861
                    break
2734
2862
            if value is not None:
2735
2863
                break
 
2864
        # If the option is registered, it may provide additional info about
 
2865
        # value handling
 
2866
        try:
 
2867
            opt = option_registry.get(name)
 
2868
        except KeyError:
 
2869
            # Not registered
 
2870
            opt = None
 
2871
        if (opt is not None and opt.from_unicode is not None
 
2872
            and value is not None):
 
2873
            # If a value exists and the option provides a converter, use it
 
2874
            try:
 
2875
                converted = opt.from_unicode(value)
 
2876
            except (ValueError, TypeError):
 
2877
                # Invalid values are ignored
 
2878
                converted = None
 
2879
            if converted is None and opt.invalid is not None:
 
2880
                # The conversion failed
 
2881
                if opt.invalid == 'warning':
 
2882
                    trace.warning('Value "%s" is not valid for "%s"',
 
2883
                                  value, name)
 
2884
                elif opt.invalid == 'error':
 
2885
                    raise errors.ConfigOptionValueError(name, value)
 
2886
            value = converted
2736
2887
        if value is None:
2737
2888
            # If the option is registered, it may provide a default value
2738
 
            try:
2739
 
                opt = option_registry.get(name)
2740
 
            except KeyError:
2741
 
                # Not registered
2742
 
                opt = None
2743
2889
            if opt is not None:
2744
2890
                value = opt.get_default()
2745
2891
        for hook in ConfigHooks['get']:
2811
2957
class LocationStack(_CompatibleStack):
2812
2958
 
2813
2959
    def __init__(self, location):
 
2960
        """Make a new stack for a location and global configuration.
 
2961
        
 
2962
        :param location: A URL prefix to """
2814
2963
        lstore = LocationStore()
2815
2964
        matcher = LocationMatcher(lstore, location)
2816
2965
        gstore = GlobalStore()
2943
3092
            raise errors.NoSuchConfigOption(name)
2944
3093
 
2945
3094
    def _show_matching_options(self, name, directory, scope):
2946
 
        name = re.compile(name)
 
3095
        name = lazy_regex.lazy_compile(name)
2947
3096
        # We want any error in the regexp to be raised *now* so we need to
2948
 
        # avoid the delay introduced by the lazy regexp.
 
3097
        # avoid the delay introduced by the lazy regexp.  But, we still do
 
3098
        # want the nicer errors raised by lazy_regex.
2949
3099
        name._compile_and_collapse()
2950
3100
        cur_conf_id = None
2951
3101
        cur_section = None