~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: John Arbash Meinel
  • Date: 2013-06-24 12:03:12 UTC
  • mfrom: (6437.77.2 2.5)
  • mto: This revision was merged to the branch mainline in revision 6579.
  • Revision ID: john@arbash-meinel.com-20130624120312-pmvck24x052csigx
Merge lp:bzr/2.5 r6515 to get the fix for bug #855155 (Dirstate.update_basis_by_delta)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005-2014, 2016 Canonical Ltd
 
1
# Copyright (C) 2005-2012 Canonical Ltd
2
2
#   Authors: Robert Collins <robert.collins@canonical.com>
3
3
#            and others
4
4
#
1488
1488
    """Return per-user configuration directory as unicode string
1489
1489
 
1490
1490
    By default this is %APPDATA%/bazaar/2.0 on Windows, ~/.bazaar on Mac OS X
1491
 
    and Linux.  On Mac OS X and Linux, if there is a $XDG_CONFIG_HOME/bazaar directory,
 
1491
    and Linux.  On Linux, if there is a $XDG_CONFIG_HOME/bazaar directory,
1492
1492
    that will be used instead.
1493
1493
 
1494
1494
    TODO: Global option --config-dir to override this.
1503
1503
        #                APPDATA, but hard to move. See bug 348640 for more.
1504
1504
        return osutils.pathjoin(base, 'bazaar', '2.0')
1505
1505
    if base is None:
1506
 
        xdg_dir = osutils.path_from_environ('XDG_CONFIG_HOME')
1507
 
        if xdg_dir is None:
1508
 
            xdg_dir = osutils.pathjoin(osutils._get_home_dir(), ".config")
1509
 
        xdg_dir = osutils.pathjoin(xdg_dir, 'bazaar')
1510
 
        if osutils.isdir(xdg_dir):
1511
 
            trace.mutter(
1512
 
                "Using configuration in XDG directory %s." % xdg_dir)
1513
 
            return xdg_dir
 
1506
        # GZ 2012-02-01: What should OSX use instead of XDG if anything?
 
1507
        if sys.platform != 'darwin':
 
1508
            xdg_dir = osutils.path_from_environ('XDG_CONFIG_HOME')
 
1509
            if xdg_dir is None:
 
1510
                xdg_dir = osutils.pathjoin(osutils._get_home_dir(), ".config")
 
1511
            xdg_dir = osutils.pathjoin(xdg_dir, 'bazaar')
 
1512
            if osutils.isdir(xdg_dir):
 
1513
                trace.mutter(
 
1514
                    "Using configuration in XDG directory %s." % xdg_dir)
 
1515
                return xdg_dir
1514
1516
        base = osutils._get_home_dir()
1515
1517
    return osutils.pathjoin(base, ".bazaar")
1516
1518
 
1555
1557
def xdg_cache_dir():
1556
1558
    # See http://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
1557
1559
    # Possibly this should be different on Windows?
1558
 
    e = os.environ.get('XDG_CACHE_HOME', None)
 
1560
    e = os.environ.get('XDG_CACHE_DIR', None)
1559
1561
    if e:
1560
1562
        return e
1561
1563
    else:
2314
2316
        :param help: a doc string to explain the option to the user.
2315
2317
 
2316
2318
        :param from_unicode: a callable to convert the unicode string
2317
 
            representing the option value in a store or its default value.
 
2319
            representing the option value in a store. This is not called for
 
2320
            the default value.
2318
2321
 
2319
2322
        :param invalid: the action to be taken when an invalid value is
2320
2323
            encountered in a store. This is called only when from_unicode is
2550
2553
        return "".join(ret)
2551
2554
 
2552
2555
 
2553
 
_option_ref_re = lazy_regex.lazy_compile('({[^\d\W](?:\.\w|-\w|\w)*})')
2554
 
"""Describes an expandable option reference.
2555
 
 
2556
 
We want to match the most embedded reference first.
2557
 
 
2558
 
I.e. for '{{foo}}' we will get '{foo}',
2559
 
for '{bar{baz}}' we will get '{baz}'
2560
 
"""
2561
 
 
2562
 
def iter_option_refs(string):
2563
 
    # Split isolate refs so every other chunk is a ref
2564
 
    is_ref = False
2565
 
    for chunk  in _option_ref_re.split(string):
2566
 
        yield is_ref, chunk
2567
 
        is_ref = not is_ref
2568
 
 
2569
 
 
2570
2556
class OptionRegistry(registry.Registry):
2571
2557
    """Register config options by their name.
2572
2558
 
2574
2560
    some information from the option object itself.
2575
2561
    """
2576
2562
 
2577
 
    def _check_option_name(self, option_name):
2578
 
        """Ensures an option name is valid.
2579
 
 
2580
 
        :param option_name: The name to validate.
2581
 
        """
2582
 
        if _option_ref_re.match('{%s}' % option_name) is None:
2583
 
            raise errors.IllegalOptionName(option_name)
2584
 
 
2585
2563
    def register(self, option):
2586
2564
        """Register a new option to its name.
2587
2565
 
2588
2566
        :param option: The option to register. Its name is used as the key.
2589
2567
        """
2590
 
        self._check_option_name(option.name)
2591
2568
        super(OptionRegistry, self).register(option.name, option,
2592
2569
                                             help=option.help)
2593
2570
 
2602
2579
        :param member_name: the member of the module to return.  If empty or 
2603
2580
                None, get() will return the module itself.
2604
2581
        """
2605
 
        self._check_option_name(key)
2606
2582
        super(OptionRegistry, self).register_lazy(key,
2607
2583
                                                  module_name, member_name)
2608
2584
 
3424
3400
 
3425
3401
 
3426
3402
class LocationStore(LockableIniFileStore):
3427
 
    """A config store for options specific to a location.
 
3403
    """A config store for global options.
3428
3404
 
3429
 
    There is a single LocationStore for a given process.
 
3405
    There is a single GlobalStore for a given process.
3430
3406
    """
3431
3407
 
3432
3408
    def __init__(self, possible_transports=None):
3559
3535
        """
3560
3536
        location_parts = self.location.rstrip('/').split('/')
3561
3537
        store = self.store
 
3538
        sections = []
3562
3539
        # Later sections are more specific, they should be returned first
3563
3540
        for _, section in reversed(list(store.get_sections())):
3564
3541
            if section.id is None:
3646
3623
            yield self.store, section
3647
3624
 
3648
3625
 
 
3626
_option_ref_re = lazy_regex.lazy_compile('({[^{}\n]+})')
 
3627
"""Describes an expandable option reference.
 
3628
 
 
3629
We want to match the most embedded reference first.
 
3630
 
 
3631
I.e. for '{{foo}}' we will get '{foo}',
 
3632
for '{bar{baz}}' we will get '{baz}'
 
3633
"""
 
3634
 
 
3635
def iter_option_refs(string):
 
3636
    # Split isolate refs so every other chunk is a ref
 
3637
    is_ref = False
 
3638
    for chunk  in _option_ref_re.split(string):
 
3639
        yield is_ref, chunk
 
3640
        is_ref = not is_ref
 
3641
 
3649
3642
# FIXME: _shared_stores should be an attribute of a library state once a
3650
3643
# library_state object is always available.
3651
3644
_shared_stores = {}
4245
4238
    def _set_config_option(self, name, value, directory, scope):
4246
4239
        conf = self._get_stack(directory, scope, write_access=True)
4247
4240
        conf.set(name, value)
4248
 
        # Explicitly save the changes
4249
 
        conf.store.save_changes()
4250
4241
 
4251
4242
    def _remove_config_option(self, name, directory, scope):
4252
4243
        if name is None:
4255
4246
        conf = self._get_stack(directory, scope, write_access=True)
4256
4247
        try:
4257
4248
            conf.remove(name)
4258
 
            # Explicitly save the changes
4259
 
            conf.store.save_changes()
4260
4249
        except KeyError:
4261
4250
            raise errors.NoSuchConfigOption(name)
4262
4251