178
229
def _get_signing_policy(self):
179
230
"""Template method to override signature creation policy."""
234
def expand_options(self, string, env=None):
235
"""Expand option references in the string in the configuration context.
237
:param string: The string containing option to expand.
239
:param env: An option dict defining additional configuration options or
240
overriding existing ones.
242
:returns: The expanded string.
244
return self._expand_options_in_string(string, env)
246
def _expand_options_in_list(self, slist, env=None, _ref_stack=None):
247
"""Expand options in a list of strings in the configuration context.
249
:param slist: A list of strings.
251
:param env: An option dict defining additional configuration options or
252
overriding existing ones.
254
:param _ref_stack: Private list containing the options being
255
expanded to detect loops.
257
:returns: The flatten list of expanded strings.
259
# expand options in each value separately flattening lists
262
value = self._expand_options_in_string(s, env, _ref_stack)
263
if isinstance(value, list):
269
def _expand_options_in_string(self, string, env=None, _ref_stack=None):
270
"""Expand options in the string in the configuration context.
272
:param string: The string to be expanded.
274
:param env: An option dict defining additional configuration options or
275
overriding existing ones.
277
:param _ref_stack: Private list containing the options being
278
expanded to detect loops.
280
:returns: The expanded string.
283
# Not much to expand there
285
if _ref_stack is None:
286
# What references are currently resolved (to detect loops)
288
if self.option_ref_re is None:
289
# We want to match the most embedded reference first (i.e. for
290
# '{{foo}}' we will get '{foo}',
291
# for '{bar{baz}}' we will get '{baz}'
292
self.option_ref_re = re.compile('({[^{}]+})')
294
# We need to iterate until no more refs appear ({{foo}} will need two
295
# iterations for example).
297
raw_chunks = self.option_ref_re.split(result)
298
if len(raw_chunks) == 1:
299
# Shorcut the trivial case: no refs
303
# Split will isolate refs so that every other chunk is a ref
305
for chunk in raw_chunks:
308
# Keep only non-empty strings (or we get bogus empty
309
# slots when a list value is involved).
314
if name in _ref_stack:
315
raise errors.OptionExpansionLoop(string, _ref_stack)
316
_ref_stack.append(name)
317
value = self._expand_option(name, env, _ref_stack)
319
raise errors.ExpandingUnknownOption(name, string)
320
if isinstance(value, list):
328
# Once a list appears as the result of an expansion, all
329
# callers will get a list result. This allows a consistent
330
# behavior even when some options in the expansion chain
331
# defined as strings (no comma in their value) but their
332
# expanded value is a list.
333
return self._expand_options_in_list(chunks, env, _ref_stack)
335
result = ''.join(chunks)
338
def _expand_option(self, name, env, _ref_stack):
339
if env is not None and name in env:
340
# Special case, values provided in env takes precedence over
344
# FIXME: This is a limited implementation, what we really need is a
345
# way to query the bzr config for the value of an option,
346
# respecting the scope rules (That is, once we implement fallback
347
# configs, getting the option value should restart from the top
348
# config, not the current one) -- vila 20101222
349
value = self.get_user_option(name, expand=False)
350
if isinstance(value, list):
351
value = self._expand_options_in_list(value, env, _ref_stack)
353
value = self._expand_options_in_string(value, env, _ref_stack)
181
356
def _get_user_option(self, option_name):
182
357
"""Template method to provide a user option."""
185
def get_user_option(self, option_name):
186
"""Get a generic option - no special process, no default."""
187
return self._get_user_option(option_name)
189
def get_user_option_as_bool(self, option_name):
190
"""Get a generic option as a boolean - no special process, no default.
360
def get_user_option(self, option_name, expand=None):
361
"""Get a generic option - no special process, no default.
363
:param option_name: The queried option.
365
:param expand: Whether options references should be expanded.
367
:returns: The value of the option.
370
expand = _get_expand_default_value()
371
value = self._get_user_option(option_name)
373
if isinstance(value, list):
374
value = self._expand_options_in_list(value)
375
elif isinstance(value, dict):
376
trace.warning('Cannot expand "%s":'
377
' Dicts do not support option expansion'
380
value = self._expand_options_in_string(value)
381
for hook in OldConfigHooks['get']:
382
hook(self, option_name, value)
385
def get_user_option_as_bool(self, option_name, expand=None, default=None):
386
"""Get a generic option as a boolean.
388
:param expand: Allow expanding references to other config values.
389
:param default: Default value if nothing is configured
192
390
:return None if the option doesn't exist or its value can't be
193
391
interpreted as a boolean. Returns True or False otherwise.
195
s = self._get_user_option(option_name)
393
s = self.get_user_option(option_name, expand=expand)
197
395
# The option doesn't exist
199
397
val = ui.bool_from_string(s)
201
399
# The value can't be interpreted as a boolean
580
def get_merge_tools(self):
582
for (oname, value, section, conf_id, parser) in self._get_options():
583
if oname.startswith('bzr.mergetool.'):
584
tool_name = oname[len('bzr.mergetool.'):]
585
tools[tool_name] = value
586
trace.mutter('loaded merge tools: %r' % tools)
589
def find_merge_tool(self, name):
590
# We fake a defaults mechanism here by checking if the given name can
591
# be found in the known_merge_tools if it's not found in the config.
592
# This should be done through the proposed config defaults mechanism
593
# when it becomes available in the future.
594
command_line = (self.get_user_option('bzr.mergetool.%s' % name,
596
or mergetools.known_merge_tools.get(name, None))
600
class _ConfigHooks(hooks.Hooks):
601
"""A dict mapping hook names and a list of callables for configs.
605
"""Create the default hooks.
607
These are all empty initially, because by default nothing should get
610
super(_ConfigHooks, self).__init__('bzrlib.config', 'ConfigHooks')
611
self.add_hook('load',
612
'Invoked when a config store is loaded.'
613
' The signature is (store).',
615
self.add_hook('save',
616
'Invoked when a config store is saved.'
617
' The signature is (store).',
619
# The hooks for config options
621
'Invoked when a config option is read.'
622
' The signature is (stack, name, value).',
625
'Invoked when a config option is set.'
626
' The signature is (stack, name, value).',
628
self.add_hook('remove',
629
'Invoked when a config option is removed.'
630
' The signature is (stack, name).',
632
ConfigHooks = _ConfigHooks()
635
class _OldConfigHooks(hooks.Hooks):
636
"""A dict mapping hook names and a list of callables for configs.
640
"""Create the default hooks.
642
These are all empty initially, because by default nothing should get
645
super(_OldConfigHooks, self).__init__('bzrlib.config', 'OldConfigHooks')
646
self.add_hook('load',
647
'Invoked when a config store is loaded.'
648
' The signature is (config).',
650
self.add_hook('save',
651
'Invoked when a config store is saved.'
652
' The signature is (config).',
654
# The hooks for config options
656
'Invoked when a config option is read.'
657
' The signature is (config, name, value).',
660
'Invoked when a config option is set.'
661
' The signature is (config, name, value).',
663
self.add_hook('remove',
664
'Invoked when a config option is removed.'
665
' The signature is (config, name).',
667
OldConfigHooks = _OldConfigHooks()
350
670
class IniBasedConfig(Config):
351
671
"""A configuration policy that draws from ini files."""
353
def __init__(self, get_filename):
673
def __init__(self, get_filename=symbol_versioning.DEPRECATED_PARAMETER,
675
"""Base class for configuration files using an ini-like syntax.
677
:param file_name: The configuration file path.
354
679
super(IniBasedConfig, self).__init__()
355
self._get_filename = get_filename
680
self.file_name = file_name
681
if symbol_versioning.deprecated_passed(get_filename):
682
symbol_versioning.warn(
683
'IniBasedConfig.__init__(get_filename) was deprecated in 2.3.'
684
' Use file_name instead.',
687
if get_filename is not None:
688
self.file_name = get_filename()
690
self.file_name = file_name
356
692
self._parser = None
358
def _get_parser(self, file=None):
695
def from_string(cls, str_or_unicode, file_name=None, save=False):
696
"""Create a config object from a string.
698
:param str_or_unicode: A string representing the file content. This will
701
:param file_name: The configuration file path.
703
:param _save: Whether the file should be saved upon creation.
705
conf = cls(file_name=file_name)
706
conf._create_from_string(str_or_unicode, save)
709
def _create_from_string(self, str_or_unicode, save):
710
self._content = StringIO(str_or_unicode.encode('utf-8'))
711
# Some tests use in-memory configs, some other always need the config
712
# file to exist on disk.
714
self._write_config_file()
716
def _get_parser(self, file=symbol_versioning.DEPRECATED_PARAMETER):
359
717
if self._parser is not None:
360
718
return self._parser
362
input = self._get_filename()
719
if symbol_versioning.deprecated_passed(file):
720
symbol_versioning.warn(
721
'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
722
' Use IniBasedConfig(_content=xxx) instead.',
725
if self._content is not None:
726
co_input = self._content
727
elif self.file_name is None:
728
raise AssertionError('We have no content to create the config')
730
co_input = self.file_name
366
self._parser = ConfigObj(input, encoding='utf-8')
732
self._parser = ConfigObj(co_input, encoding='utf-8')
367
733
except configobj.ConfigObjError, e:
368
734
raise errors.ParseConfigError(e.errors, e.config.filename)
735
except UnicodeDecodeError:
736
raise errors.ConfigContentError(self.file_name)
737
# Make sure self.reload() will use the right file name
738
self._parser.filename = self.file_name
739
for hook in OldConfigHooks['load']:
369
741
return self._parser
744
"""Reload the config file from disk."""
745
if self.file_name is None:
746
raise AssertionError('We need a file name to reload the config')
747
if self._parser is not None:
748
self._parser.reload()
749
for hook in ConfigHooks['load']:
371
752
def _get_matching_sections(self):
372
753
"""Return an ordered list of (section_name, extra_path) pairs.
1508
2216
configobj[name] = value
1510
2218
configobj.setdefault(section, {})[name] = value
2219
for hook in OldConfigHooks['set']:
2220
hook(self, name, value)
2221
self._set_configobj(configobj)
2223
def remove_option(self, option_name, section_name=None):
2224
configobj = self._get_configobj()
2225
if section_name is None:
2226
del configobj[option_name]
2228
del configobj[section_name][option_name]
2229
for hook in OldConfigHooks['remove']:
2230
hook(self, option_name)
1511
2231
self._set_configobj(configobj)
1513
2233
def _get_config_file(self):
1515
return StringIO(self._transport.get_bytes(self._filename))
2235
f = StringIO(self._transport.get_bytes(self._filename))
2236
for hook in OldConfigHooks['load']:
1516
2239
except errors.NoSuchFile:
1517
2240
return StringIO()
2242
def _external_url(self):
2243
return urlutils.join(self._transport.external_url(), self._filename)
1519
2245
def _get_configobj(self):
1520
return ConfigObj(self._get_config_file(), encoding='utf-8')
2246
f = self._get_config_file()
2249
conf = ConfigObj(f, encoding='utf-8')
2250
except configobj.ConfigObjError, e:
2251
raise errors.ParseConfigError(e.errors, self._external_url())
2252
except UnicodeDecodeError:
2253
raise errors.ConfigContentError(self._external_url())
1522
2258
def _set_configobj(self, configobj):
1523
2259
out_file = StringIO()
1524
2260
configobj.write(out_file)
1525
2261
out_file.seek(0)
1526
2262
self._transport.put_file(self._filename, out_file)
2263
for hook in OldConfigHooks['save']:
2267
class Option(object):
2268
"""An option definition.
2270
The option *values* are stored in config files and found in sections.
2272
Here we define various properties about the option itself, its default
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.
2277
def __init__(self, name, default=None, help=None, from_unicode=None,
2279
"""Build an option definition.
2281
:param name: the name used to refer to the option.
2283
:param default: the default value to use when none exist in the config
2286
:param help: a doc string to explain the option to the user.
2288
:param from_unicode: a callable to convert the unicode string
2289
representing the option value in a store. This is not called for
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
2300
self.default = default
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
2307
def get_default(self):
2310
def get_help_text(self, additional_see_also=None, plain=True):
2312
from bzrlib import help_topics
2313
result += help_topics._format_see_also(additional_see_also)
2315
result = help_topics.help_as_plain_text(result)
2319
# Predefined converters to get proper values from store
2321
def bool_from_store(unicode_str):
2322
return ui.bool_from_string(unicode_str)
2325
def int_from_store(unicode_str):
2326
return int(unicode_str)
2329
def list_from_store(unicode_str):
2330
# ConfigObj return '' instead of u''. Use 'str' below to catch all cases.
2331
if isinstance(unicode_str, (str, unicode)):
2333
# A single value, most probably the user forgot (or didn't care to
2334
# add) the final ','
2337
# The empty string, convert to empty list
2340
# We rely on ConfigObj providing us with a list already
2345
class OptionRegistry(registry.Registry):
2346
"""Register config options by their name.
2348
This overrides ``registry.Registry`` to simplify registration by acquiring
2349
some information from the option object itself.
2352
def register(self, option):
2353
"""Register a new option to its name.
2355
:param option: The option to register. Its name is used as the key.
2357
super(OptionRegistry, self).register(option.name, option,
2360
def register_lazy(self, key, module_name, member_name):
2361
"""Register a new option to be loaded on request.
2363
:param key: the key to request the option later. Since the registration
2364
is lazy, it should be provided and match the option name.
2366
:param module_name: the python path to the module. Such as 'os.path'.
2368
:param member_name: the member of the module to return. If empty or
2369
None, get() will return the module itself.
2371
super(OptionRegistry, self).register_lazy(key,
2372
module_name, member_name)
2374
def get_help(self, key=None):
2375
"""Get the help text associated with the given key"""
2376
option = self.get(key)
2377
the_help = option.help
2378
if callable(the_help):
2379
return the_help(self, key)
2383
option_registry = OptionRegistry()
2386
# Registered options in lexicographical order
2388
option_registry.register(
2389
Option('dirstate.fdatasync', default=True, from_unicode=bool_from_store,
2391
Flush dirstate changes onto physical disk?
2393
If true (default), working tree metadata changes are flushed through the
2394
OS buffers to physical disk. This is somewhat slower, but means data
2395
should not be lost if the machine crashes. See also repository.fdatasync.
2397
option_registry.register(
2398
Option('default_format', default='2a',
2399
help='Format used when creating branches.'))
2400
option_registry.register(
2402
help='The command called to launch an editor to enter a message.'))
2403
option_registry.register(
2405
help='Language to translate messages into.'))
2406
option_registry.register(
2407
Option('output_encoding',
2408
help= 'Unicode encoding for output'
2409
' (terminal encoding if not specified).'))
2410
option_registry.register(
2411
Option('repository.fdatasync', default=True, from_unicode=bool_from_store,
2413
Flush repository changes onto physical disk?
2415
If true (default), repository changes are flushed through the OS buffers
2416
to physical disk. This is somewhat slower, but means data should not be
2417
lost if the machine crashes. See also dirstate.fdatasync.
2421
class Section(object):
2422
"""A section defines a dict of option name => value.
2424
This is merely a read-only dict which can add some knowledge about the
2425
options. It is *not* a python dict object though and doesn't try to mimic
2429
def __init__(self, section_id, options):
2430
self.id = section_id
2431
# We re-use the dict-like object received
2432
self.options = options
2434
def get(self, name, default=None):
2435
return self.options.get(name, default)
2438
# Mostly for debugging use
2439
return "<config.%s id=%s>" % (self.__class__.__name__, self.id)
2442
_NewlyCreatedOption = object()
2443
"""Was the option created during the MutableSection lifetime"""
2446
class MutableSection(Section):
2447
"""A section allowing changes and keeping track of the original values."""
2449
def __init__(self, section_id, options):
2450
super(MutableSection, self).__init__(section_id, options)
2453
def set(self, name, value):
2454
if name not in self.options:
2455
# This is a new option
2456
self.orig[name] = _NewlyCreatedOption
2457
elif name not in self.orig:
2458
self.orig[name] = self.get(name, None)
2459
self.options[name] = value
2461
def remove(self, name):
2462
if name not in self.orig:
2463
self.orig[name] = self.get(name, None)
2464
del self.options[name]
2467
class Store(object):
2468
"""Abstract interface to persistent storage for configuration options."""
2470
readonly_section_class = Section
2471
mutable_section_class = MutableSection
2473
def is_loaded(self):
2474
"""Returns True if the Store has been loaded.
2476
This is used to implement lazy loading and ensure the persistent
2477
storage is queried only when needed.
2479
raise NotImplementedError(self.is_loaded)
2482
"""Loads the Store from persistent storage."""
2483
raise NotImplementedError(self.load)
2485
def _load_from_string(self, bytes):
2486
"""Create a store from a string in configobj syntax.
2488
:param bytes: A string representing the file content.
2490
raise NotImplementedError(self._load_from_string)
2493
"""Unloads the Store.
2495
This should make is_loaded() return False. This is used when the caller
2496
knows that the persistent storage has changed or may have change since
2499
raise NotImplementedError(self.unload)
2502
"""Saves the Store to persistent storage."""
2503
raise NotImplementedError(self.save)
2505
def external_url(self):
2506
raise NotImplementedError(self.external_url)
2508
def get_sections(self):
2509
"""Returns an ordered iterable of existing sections.
2511
:returns: An iterable of (name, dict).
2513
raise NotImplementedError(self.get_sections)
2515
def get_mutable_section(self, section_name=None):
2516
"""Returns the specified mutable section.
2518
:param section_name: The section identifier
2520
raise NotImplementedError(self.get_mutable_section)
2523
# Mostly for debugging use
2524
return "<config.%s(%s)>" % (self.__class__.__name__,
2525
self.external_url())
2528
class IniFileStore(Store):
2529
"""A config Store using ConfigObj for storage.
2531
:ivar transport: The transport object where the config file is located.
2533
:ivar file_name: The config file basename in the transport directory.
2535
:ivar _config_obj: Private member to hold the ConfigObj instance used to
2536
serialize/deserialize the config file.
2539
def __init__(self, transport, file_name):
2540
"""A config Store using ConfigObj for storage.
2542
:param transport: The transport object where the config file is located.
2544
:param file_name: The config file basename in the transport directory.
2546
super(IniFileStore, self).__init__()
2547
self.transport = transport
2548
self.file_name = file_name
2549
self._config_obj = None
2551
def is_loaded(self):
2552
return self._config_obj != None
2555
self._config_obj = None
2558
"""Load the store from the associated file."""
2559
if self.is_loaded():
2561
content = self.transport.get_bytes(self.file_name)
2562
self._load_from_string(content)
2563
for hook in ConfigHooks['load']:
2566
def _load_from_string(self, bytes):
2567
"""Create a config store from a string.
2569
:param bytes: A string representing the file content.
2571
if self.is_loaded():
2572
raise AssertionError('Already loaded: %r' % (self._config_obj,))
2573
co_input = StringIO(bytes)
2575
# The config files are always stored utf8-encoded
2576
self._config_obj = ConfigObj(co_input, encoding='utf-8')
2577
except configobj.ConfigObjError, e:
2578
self._config_obj = None
2579
raise errors.ParseConfigError(e.errors, self.external_url())
2580
except UnicodeDecodeError:
2581
raise errors.ConfigContentError(self.external_url())
2584
if not self.is_loaded():
2588
self._config_obj.write(out)
2589
self.transport.put_bytes(self.file_name, out.getvalue())
2590
for hook in ConfigHooks['save']:
2593
def external_url(self):
2594
# FIXME: external_url should really accepts an optional relpath
2595
# parameter (bug #750169) :-/ -- vila 2011-04-04
2596
# The following will do in the interim but maybe we don't want to
2597
# expose a path here but rather a config ID and its associated
2598
# object </hand wawe>.
2599
return urlutils.join(self.transport.external_url(), self.file_name)
2601
def get_sections(self):
2602
"""Get the configobj section in the file order.
2604
:returns: An iterable of (name, dict).
2606
# We need a loaded store
2609
except errors.NoSuchFile:
2610
# If the file doesn't exist, there is no sections
2612
cobj = self._config_obj
2614
yield self.readonly_section_class(None, cobj)
2615
for section_name in cobj.sections:
2616
yield self.readonly_section_class(section_name, cobj[section_name])
2618
def get_mutable_section(self, section_name=None):
2619
# We need a loaded store
2622
except errors.NoSuchFile:
2623
# The file doesn't exist, let's pretend it was empty
2624
self._load_from_string('')
2625
if section_name is None:
2626
section = self._config_obj
2628
section = self._config_obj.setdefault(section_name, {})
2629
return self.mutable_section_class(section_name, section)
2632
# Note that LockableConfigObjStore inherits from ConfigObjStore because we need
2633
# unlockable stores for use with objects that can already ensure the locking
2634
# (think branches). If different stores (not based on ConfigObj) are created,
2635
# they may face the same issue.
2638
class LockableIniFileStore(IniFileStore):
2639
"""A ConfigObjStore using locks on save to ensure store integrity."""
2641
def __init__(self, transport, file_name, lock_dir_name=None):
2642
"""A config Store using ConfigObj for storage.
2644
:param transport: The transport object where the config file is located.
2646
:param file_name: The config file basename in the transport directory.
2648
if lock_dir_name is None:
2649
lock_dir_name = 'lock'
2650
self.lock_dir_name = lock_dir_name
2651
super(LockableIniFileStore, self).__init__(transport, file_name)
2652
self._lock = lockdir.LockDir(self.transport, self.lock_dir_name)
2654
def lock_write(self, token=None):
2655
"""Takes a write lock in the directory containing the config file.
2657
If the directory doesn't exist it is created.
2659
# FIXME: This doesn't check the ownership of the created directories as
2660
# ensure_config_dir_exists does. It should if the transport is local
2661
# -- vila 2011-04-06
2662
self.transport.create_prefix()
2663
return self._lock.lock_write(token)
2668
def break_lock(self):
2669
self._lock.break_lock()
2673
# We need to be able to override the undecorated implementation
2674
self.save_without_locking()
2676
def save_without_locking(self):
2677
super(LockableIniFileStore, self).save()
2680
# FIXME: global, bazaar, shouldn't that be 'user' instead or even
2681
# 'user_defaults' as opposed to 'user_overrides', 'system_defaults'
2682
# (/etc/bzr/bazaar.conf) and 'system_overrides' ? -- vila 2011-04-05
2684
# FIXME: Moreover, we shouldn't need classes for these stores either, factory
2685
# functions or a registry will make it easier and clearer for tests, focusing
2686
# on the relevant parts of the API that needs testing -- vila 20110503 (based
2687
# on a poolie's remark)
2688
class GlobalStore(LockableIniFileStore):
2690
def __init__(self, possible_transports=None):
2691
t = transport.get_transport_from_path(
2692
config_dir(), possible_transports=possible_transports)
2693
super(GlobalStore, self).__init__(t, 'bazaar.conf')
2696
class LocationStore(LockableIniFileStore):
2698
def __init__(self, possible_transports=None):
2699
t = transport.get_transport_from_path(
2700
config_dir(), possible_transports=possible_transports)
2701
super(LocationStore, self).__init__(t, 'locations.conf')
2704
class BranchStore(IniFileStore):
2706
def __init__(self, branch):
2707
super(BranchStore, self).__init__(branch.control_transport,
2709
self.branch = branch
2711
def lock_write(self, token=None):
2712
return self.branch.lock_write(token)
2715
return self.branch.unlock()
2719
# We need to be able to override the undecorated implementation
2720
self.save_without_locking()
2722
def save_without_locking(self):
2723
super(BranchStore, self).save()
2726
class SectionMatcher(object):
2727
"""Select sections into a given Store.
2729
This intended to be used to postpone getting an iterable of sections from a
2733
def __init__(self, store):
2736
def get_sections(self):
2737
# This is where we require loading the store so we can see all defined
2739
sections = self.store.get_sections()
2740
# Walk the revisions in the order provided
2745
def match(self, secion):
2746
raise NotImplementedError(self.match)
2749
class LocationSection(Section):
2751
def __init__(self, section, length, extra_path):
2752
super(LocationSection, self).__init__(section.id, section.options)
2753
self.length = length
2754
self.extra_path = extra_path
2756
def get(self, name, default=None):
2757
value = super(LocationSection, self).get(name, default)
2758
if value is not None:
2759
policy_name = self.get(name + ':policy', None)
2760
policy = _policy_value.get(policy_name, POLICY_NONE)
2761
if policy == POLICY_APPENDPATH:
2762
value = urlutils.join(value, self.extra_path)
2766
class LocationMatcher(SectionMatcher):
2768
def __init__(self, store, location):
2769
super(LocationMatcher, self).__init__(store)
2770
if location.startswith('file://'):
2771
location = urlutils.local_path_from_url(location)
2772
self.location = location
2774
def _get_matching_sections(self):
2775
"""Get all sections matching ``location``."""
2776
# We slightly diverge from LocalConfig here by allowing the no-name
2777
# section as the most generic one and the lower priority.
2778
no_name_section = None
2780
# Filter out the no_name_section so _iter_for_location_by_parts can be
2781
# used (it assumes all sections have a name).
2782
for section in self.store.get_sections():
2783
if section.id is None:
2784
no_name_section = section
2786
sections.append(section)
2787
# Unfortunately _iter_for_location_by_parts deals with section names so
2788
# we have to resync.
2789
filtered_sections = _iter_for_location_by_parts(
2790
[s.id for s in sections], self.location)
2791
iter_sections = iter(sections)
2792
matching_sections = []
2793
if no_name_section is not None:
2794
matching_sections.append(
2795
LocationSection(no_name_section, 0, self.location))
2796
for section_id, extra_path, length in filtered_sections:
2797
# a section id is unique for a given store so it's safe to iterate
2799
section = iter_sections.next()
2800
if section_id == section.id:
2801
matching_sections.append(
2802
LocationSection(section, length, extra_path))
2803
return matching_sections
2805
def get_sections(self):
2806
# Override the default implementation as we want to change the order
2807
matching_sections = self._get_matching_sections()
2808
# We want the longest (aka more specific) locations first
2809
sections = sorted(matching_sections,
2810
key=lambda section: (section.length, section.id),
2812
# Sections mentioning 'ignore_parents' restrict the selection
2813
for section in sections:
2814
# FIXME: We really want to use as_bool below -- vila 2011-04-07
2815
ignore = section.get('ignore_parents', None)
2816
if ignore is not None:
2817
ignore = ui.bool_from_string(ignore)
2820
# Finally, we have a valid section
2824
class Stack(object):
2825
"""A stack of configurations where an option can be defined"""
2827
def __init__(self, sections_def, store=None, mutable_section_name=None):
2828
"""Creates a stack of sections with an optional store for changes.
2830
:param sections_def: A list of Section or callables that returns an
2831
iterable of Section. This defines the Sections for the Stack and
2832
can be called repeatedly if needed.
2834
:param store: The optional Store where modifications will be
2835
recorded. If none is specified, no modifications can be done.
2837
:param mutable_section_name: The name of the MutableSection where
2838
changes are recorded. This requires the ``store`` parameter to be
2841
self.sections_def = sections_def
2843
self.mutable_section_name = mutable_section_name
2845
def get(self, name):
2846
"""Return the *first* option value found in the sections.
2848
This is where we guarantee that sections coming from Store are loaded
2849
lazily: the loading is delayed until we need to either check that an
2850
option exists or get its value, which in turn may require to discover
2851
in which sections it can be defined. Both of these (section and option
2852
existence) require loading the store (even partially).
2854
# FIXME: No caching of options nor sections yet -- vila 20110503
2856
# Ensuring lazy loading is achieved by delaying section matching (which
2857
# implies querying the persistent storage) until it can't be avoided
2858
# anymore by using callables to describe (possibly empty) section
2860
for section_or_callable in self.sections_def:
2861
# Each section can expand to multiple ones when a callable is used
2862
if callable(section_or_callable):
2863
sections = section_or_callable()
2865
sections = [section_or_callable]
2866
for section in sections:
2867
value = section.get(name)
2868
if value is not None:
2870
if value is not None:
2872
# If the option is registered, it may provide additional info about
2875
opt = option_registry.get(name)
2879
if (opt is not None and opt.from_unicode is not None
2880
and value is not None):
2881
# If a value exists and the option provides a converter, use it
2883
converted = opt.from_unicode(value)
2884
except (ValueError, TypeError):
2885
# Invalid values are ignored
2887
if converted is None and opt.invalid is not None:
2888
# The conversion failed
2889
if opt.invalid == 'warning':
2890
trace.warning('Value "%s" is not valid for "%s"',
2892
elif opt.invalid == 'error':
2893
raise errors.ConfigOptionValueError(name, value)
2896
# If the option is registered, it may provide a default value
2898
value = opt.get_default()
2899
for hook in ConfigHooks['get']:
2900
hook(self, name, value)
2903
def _get_mutable_section(self):
2904
"""Get the MutableSection for the Stack.
2906
This is where we guarantee that the mutable section is lazily loaded:
2907
this means we won't load the corresponding store before setting a value
2908
or deleting an option. In practice the store will often be loaded but
2909
this allows helps catching some programming errors.
2911
section = self.store.get_mutable_section(self.mutable_section_name)
2914
def set(self, name, value):
2915
"""Set a new value for the option."""
2916
section = self._get_mutable_section()
2917
section.set(name, value)
2918
for hook in ConfigHooks['set']:
2919
hook(self, name, value)
2921
def remove(self, name):
2922
"""Remove an existing option."""
2923
section = self._get_mutable_section()
2924
section.remove(name)
2925
for hook in ConfigHooks['remove']:
2929
# Mostly for debugging use
2930
return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
2933
class _CompatibleStack(Stack):
2934
"""Place holder for compatibility with previous design.
2936
This is intended to ease the transition from the Config-based design to the
2937
Stack-based design and should not be used nor relied upon by plugins.
2939
One assumption made here is that the daughter classes will all use Stores
2940
derived from LockableIniFileStore).
2942
It implements set() by re-loading the store before applying the
2943
modification and saving it.
2945
The long term plan being to implement a single write by store to save
2946
all modifications, this class should not be used in the interim.
2949
def set(self, name, value):
2952
super(_CompatibleStack, self).set(name, value)
2953
# Force a write to persistent storage
2957
class GlobalStack(_CompatibleStack):
2961
gstore = GlobalStore()
2962
super(GlobalStack, self).__init__([gstore.get_sections], gstore)
2965
class LocationStack(_CompatibleStack):
2967
def __init__(self, location):
2968
"""Make a new stack for a location and global configuration.
2970
:param location: A URL prefix to """
2971
lstore = LocationStore()
2972
matcher = LocationMatcher(lstore, location)
2973
gstore = GlobalStore()
2974
super(LocationStack, self).__init__(
2975
[matcher.get_sections, gstore.get_sections], lstore)
2977
class BranchStack(_CompatibleStack):
2979
def __init__(self, branch):
2980
bstore = BranchStore(branch)
2981
lstore = LocationStore()
2982
matcher = LocationMatcher(lstore, branch.base)
2983
gstore = GlobalStore()
2984
super(BranchStack, self).__init__(
2985
[matcher.get_sections, bstore.get_sections, gstore.get_sections],
2987
self.branch = branch
2990
class cmd_config(commands.Command):
2991
__doc__ = """Display, set or remove a configuration option.
2993
Display the active value for a given option.
2995
If --all is specified, NAME is interpreted as a regular expression and all
2996
matching options are displayed mentioning their scope. The active value
2997
that bzr will take into account is the first one displayed for each option.
2999
If no NAME is given, --all .* is implied.
3001
Setting a value is achieved by using name=value without spaces. The value
3002
is set in the most relevant scope and can be checked by displaying the
3006
takes_args = ['name?']
3010
# FIXME: This should be a registry option so that plugins can register
3011
# their own config files (or not) -- vila 20101002
3012
commands.Option('scope', help='Reduce the scope to the specified'
3013
' configuration file',
3015
commands.Option('all',
3016
help='Display all the defined values for the matching options.',
3018
commands.Option('remove', help='Remove the option from'
3019
' the configuration file'),
3022
_see_also = ['configuration']
3024
@commands.display_command
3025
def run(self, name=None, all=False, directory=None, scope=None,
3027
if directory is None:
3029
directory = urlutils.normalize_url(directory)
3031
raise errors.BzrError(
3032
'--all and --remove are mutually exclusive.')
3034
# Delete the option in the given scope
3035
self._remove_config_option(name, directory, scope)
3037
# Defaults to all options
3038
self._show_matching_options('.*', directory, scope)
3041
name, value = name.split('=', 1)
3043
# Display the option(s) value(s)
3045
self._show_matching_options(name, directory, scope)
3047
self._show_value(name, directory, scope)
3050
raise errors.BzrError(
3051
'Only one option can be set.')
3052
# Set the option value
3053
self._set_config_option(name, value, directory, scope)
3055
def _get_configs(self, directory, scope=None):
3056
"""Iterate the configurations specified by ``directory`` and ``scope``.
3058
:param directory: Where the configurations are derived from.
3060
:param scope: A specific config to start from.
3062
if scope is not None:
3063
if scope == 'bazaar':
3064
yield GlobalConfig()
3065
elif scope == 'locations':
3066
yield LocationConfig(directory)
3067
elif scope == 'branch':
3068
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
3070
yield br.get_config()
3073
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
3075
yield br.get_config()
3076
except errors.NotBranchError:
3077
yield LocationConfig(directory)
3078
yield GlobalConfig()
3080
def _show_value(self, name, directory, scope):
3082
for c in self._get_configs(directory, scope):
3085
for (oname, value, section, conf_id, parser) in c._get_options():
3087
# Display only the first value and exit
3089
# FIXME: We need to use get_user_option to take policies
3090
# into account and we need to make sure the option exists
3091
# too (hence the two for loops), this needs a better API
3093
value = c.get_user_option(name)
3094
# Quote the value appropriately
3095
value = parser._quote(value)
3096
self.outf.write('%s\n' % (value,))
3100
raise errors.NoSuchConfigOption(name)
3102
def _show_matching_options(self, name, directory, scope):
3103
name = lazy_regex.lazy_compile(name)
3104
# We want any error in the regexp to be raised *now* so we need to
3105
# avoid the delay introduced by the lazy regexp. But, we still do
3106
# want the nicer errors raised by lazy_regex.
3107
name._compile_and_collapse()
3110
for c in self._get_configs(directory, scope):
3111
for (oname, value, section, conf_id, parser) in c._get_options():
3112
if name.search(oname):
3113
if cur_conf_id != conf_id:
3114
# Explain where the options are defined
3115
self.outf.write('%s:\n' % (conf_id,))
3116
cur_conf_id = conf_id
3118
if (section not in (None, 'DEFAULT')
3119
and cur_section != section):
3120
# Display the section if it's not the default (or only)
3122
self.outf.write(' [%s]\n' % (section,))
3123
cur_section = section
3124
self.outf.write(' %s = %s\n' % (oname, value))
3126
def _set_config_option(self, name, value, directory, scope):
3127
for conf in self._get_configs(directory, scope):
3128
conf.set_user_option(name, value)
3131
raise errors.NoSuchConfig(scope)
3133
def _remove_config_option(self, name, directory, scope):
3135
raise errors.BzrCommandError(
3136
'--remove expects an option to remove.')
3138
for conf in self._get_configs(directory, scope):
3139
for (section_name, section, conf_id) in conf._get_sections():
3140
if scope is not None and conf_id != scope:
3141
# Not the right configuration file
3144
if conf_id != conf.config_id():
3145
conf = self._get_configs(directory, conf_id).next()
3146
# We use the first section in the first config where the
3147
# option is defined to remove it
3148
conf.remove_user_option(name, section_name)
3153
raise errors.NoSuchConfig(scope)
3155
raise errors.NoSuchConfigOption(name)
3159
# We need adapters that can build a Store or a Stack in a test context. Test
3160
# classes, based on TestCaseWithTransport, can use the registry to parametrize
3161
# themselves. The builder will receive a test instance and should return a
3162
# ready-to-use store or stack. Plugins that define new store/stacks can also
3163
# register themselves here to be tested against the tests defined in
3164
# bzrlib.tests.test_config. Note that the builder can be called multiple times
3165
# for the same tests.
3167
# The registered object should be a callable receiving a test instance
3168
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Store
3170
test_store_builder_registry = registry.Registry()
3172
# The registered object should be a callable receiving a test instance
3173
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Stack
3175
test_stack_builder_registry = registry.Registry()