230
178
def _get_signing_policy(self):
231
179
"""Template method to override signature creation policy."""
235
def expand_options(self, string, env=None):
236
"""Expand option references in the string in the configuration context.
238
:param string: The string containing option to expand.
240
:param env: An option dict defining additional configuration options or
241
overriding existing ones.
243
:returns: The expanded string.
245
return self._expand_options_in_string(string, env)
247
def _expand_options_in_list(self, slist, env=None, _ref_stack=None):
248
"""Expand options in a list of strings in the configuration context.
250
:param slist: A list of strings.
252
:param env: An option dict defining additional configuration options or
253
overriding existing ones.
255
:param _ref_stack: Private list containing the options being
256
expanded to detect loops.
258
:returns: The flatten list of expanded strings.
260
# expand options in each value separately flattening lists
263
value = self._expand_options_in_string(s, env, _ref_stack)
264
if isinstance(value, list):
270
def _expand_options_in_string(self, string, env=None, _ref_stack=None):
271
"""Expand options in the string in the configuration context.
273
:param string: The string to be expanded.
275
:param env: An option dict defining additional configuration options or
276
overriding existing ones.
278
:param _ref_stack: Private list containing the options being
279
expanded to detect loops.
281
:returns: The expanded string.
284
# Not much to expand there
286
if _ref_stack is None:
287
# What references are currently resolved (to detect loops)
289
if self.option_ref_re is None:
290
# We want to match the most embedded reference first (i.e. for
291
# '{{foo}}' we will get '{foo}',
292
# for '{bar{baz}}' we will get '{baz}'
293
self.option_ref_re = re.compile('({[^{}]+})')
295
# We need to iterate until no more refs appear ({{foo}} will need two
296
# iterations for example).
298
raw_chunks = self.option_ref_re.split(result)
299
if len(raw_chunks) == 1:
300
# Shorcut the trivial case: no refs
304
# Split will isolate refs so that every other chunk is a ref
306
for chunk in raw_chunks:
309
# Keep only non-empty strings (or we get bogus empty
310
# slots when a list value is involved).
315
if name in _ref_stack:
316
raise errors.OptionExpansionLoop(string, _ref_stack)
317
_ref_stack.append(name)
318
value = self._expand_option(name, env, _ref_stack)
320
raise errors.ExpandingUnknownOption(name, string)
321
if isinstance(value, list):
329
# Once a list appears as the result of an expansion, all
330
# callers will get a list result. This allows a consistent
331
# behavior even when some options in the expansion chain
332
# defined as strings (no comma in their value) but their
333
# expanded value is a list.
334
return self._expand_options_in_list(chunks, env, _ref_stack)
336
result = ''.join(chunks)
339
def _expand_option(self, name, env, _ref_stack):
340
if env is not None and name in env:
341
# Special case, values provided in env takes precedence over
345
# FIXME: This is a limited implementation, what we really need is a
346
# way to query the bzr config for the value of an option,
347
# respecting the scope rules (That is, once we implement fallback
348
# configs, getting the option value should restart from the top
349
# config, not the current one) -- vila 20101222
350
value = self.get_user_option(name, expand=False)
351
if isinstance(value, list):
352
value = self._expand_options_in_list(value, env, _ref_stack)
354
value = self._expand_options_in_string(value, env, _ref_stack)
357
181
def _get_user_option(self, option_name):
358
182
"""Template method to provide a user option."""
361
def get_user_option(self, option_name, expand=None):
362
"""Get a generic option - no special process, no default.
364
:param option_name: The queried option.
366
:param expand: Whether options references should be expanded.
368
:returns: The value of the option.
371
expand = _get_expand_default_value()
372
value = self._get_user_option(option_name)
374
if isinstance(value, list):
375
value = self._expand_options_in_list(value)
376
elif isinstance(value, dict):
377
trace.warning('Cannot expand "%s":'
378
' Dicts do not support option expansion'
381
value = self._expand_options_in_string(value)
382
for hook in OldConfigHooks['get']:
383
hook(self, option_name, value)
386
def get_user_option_as_bool(self, option_name, expand=None, default=None):
387
"""Get a generic option as a boolean.
389
:param expand: Allow expanding references to other config values.
390
:param default: Default value if nothing is configured
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.
391
192
:return None if the option doesn't exist or its value can't be
392
193
interpreted as a boolean. Returns True or False otherwise.
394
s = self.get_user_option(option_name, expand=expand)
195
s = self._get_user_option(option_name)
396
197
# The option doesn't exist
398
199
val = ui.bool_from_string(s)
400
201
# The value can't be interpreted as a boolean
573
def get_merge_tools(self):
575
for (oname, value, section, conf_id, parser) in self._get_options():
576
if oname.startswith('bzr.mergetool.'):
577
tool_name = oname[len('bzr.mergetool.'):]
578
tools[tool_name] = value
579
trace.mutter('loaded merge tools: %r' % tools)
582
def find_merge_tool(self, name):
583
# We fake a defaults mechanism here by checking if the given name can
584
# be found in the known_merge_tools if it's not found in the config.
585
# This should be done through the proposed config defaults mechanism
586
# when it becomes available in the future.
587
command_line = (self.get_user_option('bzr.mergetool.%s' % name,
589
or mergetools.known_merge_tools.get(name, None))
593
class _ConfigHooks(hooks.Hooks):
594
"""A dict mapping hook names and a list of callables for configs.
598
"""Create the default hooks.
600
These are all empty initially, because by default nothing should get
603
super(_ConfigHooks, self).__init__('bzrlib.config', 'ConfigHooks')
604
self.add_hook('load',
605
'Invoked when a config store is loaded.'
606
' The signature is (store).',
608
self.add_hook('save',
609
'Invoked when a config store is saved.'
610
' The signature is (store).',
612
# The hooks for config options
614
'Invoked when a config option is read.'
615
' The signature is (stack, name, value).',
618
'Invoked when a config option is set.'
619
' The signature is (stack, name, value).',
621
self.add_hook('remove',
622
'Invoked when a config option is removed.'
623
' The signature is (stack, name).',
625
ConfigHooks = _ConfigHooks()
628
class _OldConfigHooks(hooks.Hooks):
629
"""A dict mapping hook names and a list of callables for configs.
633
"""Create the default hooks.
635
These are all empty initially, because by default nothing should get
638
super(_OldConfigHooks, self).__init__('bzrlib.config', 'OldConfigHooks')
639
self.add_hook('load',
640
'Invoked when a config store is loaded.'
641
' The signature is (config).',
643
self.add_hook('save',
644
'Invoked when a config store is saved.'
645
' The signature is (config).',
647
# The hooks for config options
649
'Invoked when a config option is read.'
650
' The signature is (config, name, value).',
653
'Invoked when a config option is set.'
654
' The signature is (config, name, value).',
656
self.add_hook('remove',
657
'Invoked when a config option is removed.'
658
' The signature is (config, name).',
660
OldConfigHooks = _OldConfigHooks()
663
351
class IniBasedConfig(Config):
664
352
"""A configuration policy that draws from ini files."""
666
def __init__(self, get_filename=symbol_versioning.DEPRECATED_PARAMETER,
668
"""Base class for configuration files using an ini-like syntax.
670
:param file_name: The configuration file path.
354
def __init__(self, get_filename):
672
355
super(IniBasedConfig, self).__init__()
673
self.file_name = file_name
674
if symbol_versioning.deprecated_passed(get_filename):
675
symbol_versioning.warn(
676
'IniBasedConfig.__init__(get_filename) was deprecated in 2.3.'
677
' Use file_name instead.',
680
if get_filename is not None:
681
self.file_name = get_filename()
683
self.file_name = file_name
356
self._get_filename = get_filename
685
357
self._parser = None
688
def from_string(cls, str_or_unicode, file_name=None, save=False):
689
"""Create a config object from a string.
691
:param str_or_unicode: A string representing the file content. This will
694
:param file_name: The configuration file path.
696
:param _save: Whether the file should be saved upon creation.
698
conf = cls(file_name=file_name)
699
conf._create_from_string(str_or_unicode, save)
702
def _create_from_string(self, str_or_unicode, save):
703
self._content = StringIO(str_or_unicode.encode('utf-8'))
704
# Some tests use in-memory configs, some other always need the config
705
# file to exist on disk.
707
self._write_config_file()
709
def _get_parser(self, file=symbol_versioning.DEPRECATED_PARAMETER):
359
def _get_parser(self, file=None):
710
360
if self._parser is not None:
711
361
return self._parser
712
if symbol_versioning.deprecated_passed(file):
713
symbol_versioning.warn(
714
'IniBasedConfig._get_parser(file=xxx) was deprecated in 2.3.'
715
' Use IniBasedConfig(_content=xxx) instead.',
718
if self._content is not None:
719
co_input = self._content
720
elif self.file_name is None:
721
raise AssertionError('We have no content to create the config')
363
input = self._get_filename()
723
co_input = self.file_name
725
self._parser = ConfigObj(co_input, encoding='utf-8')
367
self._parser = ConfigObj(input, encoding='utf-8')
726
368
except configobj.ConfigObjError, e:
727
369
raise errors.ParseConfigError(e.errors, e.config.filename)
728
except UnicodeDecodeError:
729
raise errors.ConfigContentError(self.file_name)
730
# Make sure self.reload() will use the right file name
731
self._parser.filename = self.file_name
732
for hook in OldConfigHooks['load']:
734
370
return self._parser
737
"""Reload the config file from disk."""
738
if self.file_name is None:
739
raise AssertionError('We need a file name to reload the config')
740
if self._parser is not None:
741
self._parser.reload()
742
for hook in ConfigHooks['load']:
745
372
def _get_matching_sections(self):
746
373
"""Return an ordered list of (section_name, extra_path) pairs.
907
477
def _get_nickname(self):
908
478
return self.get_user_option('nickname')
910
def remove_user_option(self, option_name, section_name=None):
911
"""Remove a user option and save the configuration file.
913
:param option_name: The option to be removed.
915
:param section_name: The section the option is defined in, default to
919
parser = self._get_parser()
920
if section_name is None:
923
section = parser[section_name]
480
def _write_config_file(self):
481
f = file(self._get_filename(), "wb")
925
del section[option_name]
927
raise errors.NoSuchConfigOption(option_name)
928
self._write_config_file()
929
for hook in OldConfigHooks['remove']:
930
hook(self, option_name)
932
def _write_config_file(self):
933
if self.file_name is None:
934
raise AssertionError('We cannot save, self.file_name is None')
935
conf_dir = os.path.dirname(self.file_name)
936
ensure_config_dir_exists(conf_dir)
937
atomic_file = atomicfile.AtomicFile(self.file_name)
938
self._get_parser().write(atomic_file)
941
osutils.copy_ownership_from_path(self.file_name)
942
for hook in OldConfigHooks['save']:
946
class LockableConfig(IniBasedConfig):
947
"""A configuration needing explicit locking for access.
949
If several processes try to write the config file, the accesses need to be
952
Daughter classes should decorate all methods that update a config with the
953
``@needs_write_lock`` decorator (they call, directly or indirectly, the
954
``_write_config_file()`` method. These methods (typically ``set_option()``
955
and variants must reload the config file from disk before calling
956
``_write_config_file()``), this can be achieved by calling the
957
``self.reload()`` method. Note that the lock scope should cover both the
958
reading and the writing of the config file which is why the decorator can't
959
be applied to ``_write_config_file()`` only.
961
This should be enough to implement the following logic:
962
- lock for exclusive write access,
963
- reload the config file from disk,
967
This logic guarantees that a writer can update a value without erasing an
968
update made by another writer.
973
def __init__(self, file_name):
974
super(LockableConfig, self).__init__(file_name=file_name)
975
self.dir = osutils.dirname(osutils.safe_unicode(self.file_name))
976
# FIXME: It doesn't matter that we don't provide possible_transports
977
# below since this is currently used only for local config files ;
978
# local transports are not shared. But if/when we start using
979
# LockableConfig for other kind of transports, we will need to reuse
980
# whatever connection is already established -- vila 20100929
981
self.transport = transport.get_transport(self.dir)
982
self._lock = lockdir.LockDir(self.transport, self.lock_name)
984
def _create_from_string(self, unicode_bytes, save):
985
super(LockableConfig, self)._create_from_string(unicode_bytes, False)
987
# We need to handle the saving here (as opposed to IniBasedConfig)
990
self._write_config_file()
993
def lock_write(self, token=None):
994
"""Takes a write lock in the directory containing the config file.
996
If the directory doesn't exist it is created.
998
ensure_config_dir_exists(self.dir)
999
return self._lock.lock_write(token)
1004
def break_lock(self):
1005
self._lock.break_lock()
1008
def remove_user_option(self, option_name, section_name=None):
1009
super(LockableConfig, self).remove_user_option(option_name,
1012
def _write_config_file(self):
1013
if self._lock is None or not self._lock.is_held:
1014
# NB: if the following exception is raised it probably means a
1015
# missing @needs_write_lock decorator on one of the callers.
1016
raise errors.ObjectNotLocked(self)
1017
super(LockableConfig, self)._write_config_file()
1020
class GlobalConfig(LockableConfig):
483
osutils.copy_ownership_from_path(f.name)
484
self._get_parser().write(f)
489
class GlobalConfig(IniBasedConfig):
1021
490
"""The configuration that should be used for a specific location."""
1024
super(GlobalConfig, self).__init__(file_name=config_filename())
1026
def config_id(self):
1030
def from_string(cls, str_or_unicode, save=False):
1031
"""Create a config object from a string.
1033
:param str_or_unicode: A string representing the file content. This
1034
will be utf-8 encoded.
1036
:param save: Whether the file should be saved upon creation.
1039
conf._create_from_string(str_or_unicode, save)
1042
@deprecated_method(deprecated_in((2, 4, 0)))
1043
492
def get_editor(self):
1044
493
return self._get_user_option('editor')
496
super(GlobalConfig, self).__init__(config_filename)
1047
498
def set_user_option(self, option, value):
1048
499
"""Save option and its value in the configuration."""
1049
500
self._set_option(option, value, 'DEFAULT')
2208
1441
configobj[name] = value
2210
1443
configobj.setdefault(section, {})[name] = value
2211
for hook in OldConfigHooks['set']:
2212
hook(self, name, value)
2213
self._set_configobj(configobj)
2215
def remove_option(self, option_name, section_name=None):
2216
configobj = self._get_configobj()
2217
if section_name is None:
2218
del configobj[option_name]
2220
del configobj[section_name][option_name]
2221
for hook in OldConfigHooks['remove']:
2222
hook(self, option_name)
2223
1444
self._set_configobj(configobj)
2225
1446
def _get_config_file(self):
2227
f = StringIO(self._transport.get_bytes(self._filename))
2228
for hook in OldConfigHooks['load']:
1448
return StringIO(self._transport.get_bytes(self._filename))
2231
1449
except errors.NoSuchFile:
2232
1450
return StringIO()
2234
def _external_url(self):
2235
return urlutils.join(self._transport.external_url(), self._filename)
2237
1452
def _get_configobj(self):
2238
1453
f = self._get_config_file()
2241
conf = ConfigObj(f, encoding='utf-8')
2242
except configobj.ConfigObjError, e:
2243
raise errors.ParseConfigError(e.errors, self._external_url())
2244
except UnicodeDecodeError:
2245
raise errors.ConfigContentError(self._external_url())
1455
return ConfigObj(f, encoding='utf-8')
2250
1459
def _set_configobj(self, configobj):
2251
1460
out_file = StringIO()
2252
1461
configobj.write(out_file)
2253
1462
out_file.seek(0)
2254
1463
self._transport.put_file(self._filename, out_file)
2255
for hook in OldConfigHooks['save']:
2259
class Option(object):
2260
"""An option definition.
2262
The option *values* are stored in config files and found in sections.
2264
Here we define various properties about the option itself, its default
2265
value, in which config files it can be stored, etc (TBC).
2268
def __init__(self, name, default=None):
2270
self.default = default
2272
def get_default(self):
2278
option_registry = registry.Registry()
2281
option_registry.register(
2282
'editor', Option('editor'),
2283
help='The command called to launch an editor to enter a message.')
2286
class Section(object):
2287
"""A section defines a dict of option name => value.
2289
This is merely a read-only dict which can add some knowledge about the
2290
options. It is *not* a python dict object though and doesn't try to mimic
2294
def __init__(self, section_id, options):
2295
self.id = section_id
2296
# We re-use the dict-like object received
2297
self.options = options
2299
def get(self, name, default=None):
2300
return self.options.get(name, default)
2303
# Mostly for debugging use
2304
return "<config.%s id=%s>" % (self.__class__.__name__, self.id)
2307
_NewlyCreatedOption = object()
2308
"""Was the option created during the MutableSection lifetime"""
2311
class MutableSection(Section):
2312
"""A section allowing changes and keeping track of the original values."""
2314
def __init__(self, section_id, options):
2315
super(MutableSection, self).__init__(section_id, options)
2318
def set(self, name, value):
2319
if name not in self.options:
2320
# This is a new option
2321
self.orig[name] = _NewlyCreatedOption
2322
elif name not in self.orig:
2323
self.orig[name] = self.get(name, None)
2324
self.options[name] = value
2326
def remove(self, name):
2327
if name not in self.orig:
2328
self.orig[name] = self.get(name, None)
2329
del self.options[name]
2332
class Store(object):
2333
"""Abstract interface to persistent storage for configuration options."""
2335
readonly_section_class = Section
2336
mutable_section_class = MutableSection
2338
def is_loaded(self):
2339
"""Returns True if the Store has been loaded.
2341
This is used to implement lazy loading and ensure the persistent
2342
storage is queried only when needed.
2344
raise NotImplementedError(self.is_loaded)
2347
"""Loads the Store from persistent storage."""
2348
raise NotImplementedError(self.load)
2350
def _load_from_string(self, bytes):
2351
"""Create a store from a string in configobj syntax.
2353
:param bytes: A string representing the file content.
2355
raise NotImplementedError(self._load_from_string)
2358
"""Unloads the Store.
2360
This should make is_loaded() return False. This is used when the caller
2361
knows that the persistent storage has changed or may have change since
2364
raise NotImplementedError(self.unload)
2367
"""Saves the Store to persistent storage."""
2368
raise NotImplementedError(self.save)
2370
def external_url(self):
2371
raise NotImplementedError(self.external_url)
2373
def get_sections(self):
2374
"""Returns an ordered iterable of existing sections.
2376
:returns: An iterable of (name, dict).
2378
raise NotImplementedError(self.get_sections)
2380
def get_mutable_section(self, section_name=None):
2381
"""Returns the specified mutable section.
2383
:param section_name: The section identifier
2385
raise NotImplementedError(self.get_mutable_section)
2388
# Mostly for debugging use
2389
return "<config.%s(%s)>" % (self.__class__.__name__,
2390
self.external_url())
2393
class IniFileStore(Store):
2394
"""A config Store using ConfigObj for storage.
2396
:ivar transport: The transport object where the config file is located.
2398
:ivar file_name: The config file basename in the transport directory.
2400
:ivar _config_obj: Private member to hold the ConfigObj instance used to
2401
serialize/deserialize the config file.
2404
def __init__(self, transport, file_name):
2405
"""A config Store using ConfigObj for storage.
2407
:param transport: The transport object where the config file is located.
2409
:param file_name: The config file basename in the transport directory.
2411
super(IniFileStore, self).__init__()
2412
self.transport = transport
2413
self.file_name = file_name
2414
self._config_obj = None
2416
def is_loaded(self):
2417
return self._config_obj != None
2420
self._config_obj = None
2423
"""Load the store from the associated file."""
2424
if self.is_loaded():
2426
content = self.transport.get_bytes(self.file_name)
2427
self._load_from_string(content)
2428
for hook in ConfigHooks['load']:
2431
def _load_from_string(self, bytes):
2432
"""Create a config store from a string.
2434
:param bytes: A string representing the file content.
2436
if self.is_loaded():
2437
raise AssertionError('Already loaded: %r' % (self._config_obj,))
2438
co_input = StringIO(bytes)
2440
# The config files are always stored utf8-encoded
2441
self._config_obj = ConfigObj(co_input, encoding='utf-8')
2442
except configobj.ConfigObjError, e:
2443
self._config_obj = None
2444
raise errors.ParseConfigError(e.errors, self.external_url())
2445
except UnicodeDecodeError:
2446
raise errors.ConfigContentError(self.external_url())
2449
if not self.is_loaded():
2453
self._config_obj.write(out)
2454
self.transport.put_bytes(self.file_name, out.getvalue())
2455
for hook in ConfigHooks['save']:
2458
def external_url(self):
2459
# FIXME: external_url should really accepts an optional relpath
2460
# parameter (bug #750169) :-/ -- vila 2011-04-04
2461
# The following will do in the interim but maybe we don't want to
2462
# expose a path here but rather a config ID and its associated
2463
# object </hand wawe>.
2464
return urlutils.join(self.transport.external_url(), self.file_name)
2466
def get_sections(self):
2467
"""Get the configobj section in the file order.
2469
:returns: An iterable of (name, dict).
2471
# We need a loaded store
2474
except errors.NoSuchFile:
2475
# If the file doesn't exist, there is no sections
2477
cobj = self._config_obj
2479
yield self.readonly_section_class(None, cobj)
2480
for section_name in cobj.sections:
2481
yield self.readonly_section_class(section_name, cobj[section_name])
2483
def get_mutable_section(self, section_name=None):
2484
# We need a loaded store
2487
except errors.NoSuchFile:
2488
# The file doesn't exist, let's pretend it was empty
2489
self._load_from_string('')
2490
if section_name is None:
2491
section = self._config_obj
2493
section = self._config_obj.setdefault(section_name, {})
2494
return self.mutable_section_class(section_name, section)
2497
# Note that LockableConfigObjStore inherits from ConfigObjStore because we need
2498
# unlockable stores for use with objects that can already ensure the locking
2499
# (think branches). If different stores (not based on ConfigObj) are created,
2500
# they may face the same issue.
2503
class LockableIniFileStore(IniFileStore):
2504
"""A ConfigObjStore using locks on save to ensure store integrity."""
2506
def __init__(self, transport, file_name, lock_dir_name=None):
2507
"""A config Store using ConfigObj for storage.
2509
:param transport: The transport object where the config file is located.
2511
:param file_name: The config file basename in the transport directory.
2513
if lock_dir_name is None:
2514
lock_dir_name = 'lock'
2515
self.lock_dir_name = lock_dir_name
2516
super(LockableIniFileStore, self).__init__(transport, file_name)
2517
self._lock = lockdir.LockDir(self.transport, self.lock_dir_name)
2519
def lock_write(self, token=None):
2520
"""Takes a write lock in the directory containing the config file.
2522
If the directory doesn't exist it is created.
2524
# FIXME: This doesn't check the ownership of the created directories as
2525
# ensure_config_dir_exists does. It should if the transport is local
2526
# -- vila 2011-04-06
2527
self.transport.create_prefix()
2528
return self._lock.lock_write(token)
2533
def break_lock(self):
2534
self._lock.break_lock()
2538
# We need to be able to override the undecorated implementation
2539
self.save_without_locking()
2541
def save_without_locking(self):
2542
super(LockableIniFileStore, self).save()
2545
# FIXME: global, bazaar, shouldn't that be 'user' instead or even
2546
# 'user_defaults' as opposed to 'user_overrides', 'system_defaults'
2547
# (/etc/bzr/bazaar.conf) and 'system_overrides' ? -- vila 2011-04-05
2549
# FIXME: Moreover, we shouldn't need classes for these stores either, factory
2550
# functions or a registry will make it easier and clearer for tests, focusing
2551
# on the relevant parts of the API that needs testing -- vila 20110503 (based
2552
# on a poolie's remark)
2553
class GlobalStore(LockableIniFileStore):
2555
def __init__(self, possible_transports=None):
2556
t = transport.get_transport(config_dir(),
2557
possible_transports=possible_transports)
2558
super(GlobalStore, self).__init__(t, 'bazaar.conf')
2561
class LocationStore(LockableIniFileStore):
2563
def __init__(self, possible_transports=None):
2564
t = transport.get_transport(config_dir(),
2565
possible_transports=possible_transports)
2566
super(LocationStore, self).__init__(t, 'locations.conf')
2569
class BranchStore(IniFileStore):
2571
def __init__(self, branch):
2572
super(BranchStore, self).__init__(branch.control_transport,
2574
self.branch = branch
2576
def lock_write(self, token=None):
2577
return self.branch.lock_write(token)
2580
return self.branch.unlock()
2584
# We need to be able to override the undecorated implementation
2585
self.save_without_locking()
2587
def save_without_locking(self):
2588
super(BranchStore, self).save()
2591
class SectionMatcher(object):
2592
"""Select sections into a given Store.
2594
This intended to be used to postpone getting an iterable of sections from a
2598
def __init__(self, store):
2601
def get_sections(self):
2602
# This is where we require loading the store so we can see all defined
2604
sections = self.store.get_sections()
2605
# Walk the revisions in the order provided
2610
def match(self, secion):
2611
raise NotImplementedError(self.match)
2614
class LocationSection(Section):
2616
def __init__(self, section, length, extra_path):
2617
super(LocationSection, self).__init__(section.id, section.options)
2618
self.length = length
2619
self.extra_path = extra_path
2621
def get(self, name, default=None):
2622
value = super(LocationSection, self).get(name, default)
2623
if value is not None:
2624
policy_name = self.get(name + ':policy', None)
2625
policy = _policy_value.get(policy_name, POLICY_NONE)
2626
if policy == POLICY_APPENDPATH:
2627
value = urlutils.join(value, self.extra_path)
2631
class LocationMatcher(SectionMatcher):
2633
def __init__(self, store, location):
2634
super(LocationMatcher, self).__init__(store)
2635
if location.startswith('file://'):
2636
location = urlutils.local_path_from_url(location)
2637
self.location = location
2639
def _get_matching_sections(self):
2640
"""Get all sections matching ``location``."""
2641
# We slightly diverge from LocalConfig here by allowing the no-name
2642
# section as the most generic one and the lower priority.
2643
no_name_section = None
2645
# Filter out the no_name_section so _iter_for_location_by_parts can be
2646
# used (it assumes all sections have a name).
2647
for section in self.store.get_sections():
2648
if section.id is None:
2649
no_name_section = section
2651
sections.append(section)
2652
# Unfortunately _iter_for_location_by_parts deals with section names so
2653
# we have to resync.
2654
filtered_sections = _iter_for_location_by_parts(
2655
[s.id for s in sections], self.location)
2656
iter_sections = iter(sections)
2657
matching_sections = []
2658
if no_name_section is not None:
2659
matching_sections.append(
2660
LocationSection(no_name_section, 0, self.location))
2661
for section_id, extra_path, length in filtered_sections:
2662
# a section id is unique for a given store so it's safe to iterate
2664
section = iter_sections.next()
2665
if section_id == section.id:
2666
matching_sections.append(
2667
LocationSection(section, length, extra_path))
2668
return matching_sections
2670
def get_sections(self):
2671
# Override the default implementation as we want to change the order
2672
matching_sections = self._get_matching_sections()
2673
# We want the longest (aka more specific) locations first
2674
sections = sorted(matching_sections,
2675
key=lambda section: (section.length, section.id),
2677
# Sections mentioning 'ignore_parents' restrict the selection
2678
for section in sections:
2679
# FIXME: We really want to use as_bool below -- vila 2011-04-07
2680
ignore = section.get('ignore_parents', None)
2681
if ignore is not None:
2682
ignore = ui.bool_from_string(ignore)
2685
# Finally, we have a valid section
2689
class Stack(object):
2690
"""A stack of configurations where an option can be defined"""
2692
def __init__(self, sections_def, store=None, mutable_section_name=None):
2693
"""Creates a stack of sections with an optional store for changes.
2695
:param sections_def: A list of Section or callables that returns an
2696
iterable of Section. This defines the Sections for the Stack and
2697
can be called repeatedly if needed.
2699
:param store: The optional Store where modifications will be
2700
recorded. If none is specified, no modifications can be done.
2702
:param mutable_section_name: The name of the MutableSection where
2703
changes are recorded. This requires the ``store`` parameter to be
2706
self.sections_def = sections_def
2708
self.mutable_section_name = mutable_section_name
2710
def get(self, name):
2711
"""Return the *first* option value found in the sections.
2713
This is where we guarantee that sections coming from Store are loaded
2714
lazily: the loading is delayed until we need to either check that an
2715
option exists or get its value, which in turn may require to discover
2716
in which sections it can be defined. Both of these (section and option
2717
existence) require loading the store (even partially).
2719
# FIXME: No caching of options nor sections yet -- vila 20110503
2721
# Ensuring lazy loading is achieved by delaying section matching (which
2722
# implies querying the persistent storage) until it can't be avoided
2723
# anymore by using callables to describe (possibly empty) section
2725
for section_or_callable in self.sections_def:
2726
# Each section can expand to multiple ones when a callable is used
2727
if callable(section_or_callable):
2728
sections = section_or_callable()
2730
sections = [section_or_callable]
2731
for section in sections:
2732
value = section.get(name)
2733
if value is not None:
2735
if value is not None:
2738
# If the option is registered, it may provide a default value
2740
opt = option_registry.get(name)
2745
value = opt.get_default()
2746
for hook in ConfigHooks['get']:
2747
hook(self, name, value)
2750
def _get_mutable_section(self):
2751
"""Get the MutableSection for the Stack.
2753
This is where we guarantee that the mutable section is lazily loaded:
2754
this means we won't load the corresponding store before setting a value
2755
or deleting an option. In practice the store will often be loaded but
2756
this allows helps catching some programming errors.
2758
section = self.store.get_mutable_section(self.mutable_section_name)
2761
def set(self, name, value):
2762
"""Set a new value for the option."""
2763
section = self._get_mutable_section()
2764
section.set(name, value)
2765
for hook in ConfigHooks['set']:
2766
hook(self, name, value)
2768
def remove(self, name):
2769
"""Remove an existing option."""
2770
section = self._get_mutable_section()
2771
section.remove(name)
2772
for hook in ConfigHooks['remove']:
2776
# Mostly for debugging use
2777
return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
2780
class _CompatibleStack(Stack):
2781
"""Place holder for compatibility with previous design.
2783
This is intended to ease the transition from the Config-based design to the
2784
Stack-based design and should not be used nor relied upon by plugins.
2786
One assumption made here is that the daughter classes will all use Stores
2787
derived from LockableIniFileStore).
2789
It implements set() by re-loading the store before applying the
2790
modification and saving it.
2792
The long term plan being to implement a single write by store to save
2793
all modifications, this class should not be used in the interim.
2796
def set(self, name, value):
2799
super(_CompatibleStack, self).set(name, value)
2800
# Force a write to persistent storage
2804
class GlobalStack(_CompatibleStack):
2808
gstore = GlobalStore()
2809
super(GlobalStack, self).__init__([gstore.get_sections], gstore)
2812
class LocationStack(_CompatibleStack):
2814
def __init__(self, location):
2815
lstore = LocationStore()
2816
matcher = LocationMatcher(lstore, location)
2817
gstore = GlobalStore()
2818
super(LocationStack, self).__init__(
2819
[matcher.get_sections, gstore.get_sections], lstore)
2821
class BranchStack(_CompatibleStack):
2823
def __init__(self, branch):
2824
bstore = BranchStore(branch)
2825
lstore = LocationStore()
2826
matcher = LocationMatcher(lstore, branch.base)
2827
gstore = GlobalStore()
2828
super(BranchStack, self).__init__(
2829
[matcher.get_sections, bstore.get_sections, gstore.get_sections],
2831
self.branch = branch
2834
class cmd_config(commands.Command):
2835
__doc__ = """Display, set or remove a configuration option.
2837
Display the active value for a given option.
2839
If --all is specified, NAME is interpreted as a regular expression and all
2840
matching options are displayed mentioning their scope. The active value
2841
that bzr will take into account is the first one displayed for each option.
2843
If no NAME is given, --all .* is implied.
2845
Setting a value is achieved by using name=value without spaces. The value
2846
is set in the most relevant scope and can be checked by displaying the
2850
takes_args = ['name?']
2854
# FIXME: This should be a registry option so that plugins can register
2855
# their own config files (or not) -- vila 20101002
2856
commands.Option('scope', help='Reduce the scope to the specified'
2857
' configuration file',
2859
commands.Option('all',
2860
help='Display all the defined values for the matching options.',
2862
commands.Option('remove', help='Remove the option from'
2863
' the configuration file'),
2866
_see_also = ['configuration']
2868
@commands.display_command
2869
def run(self, name=None, all=False, directory=None, scope=None,
2871
if directory is None:
2873
directory = urlutils.normalize_url(directory)
2875
raise errors.BzrError(
2876
'--all and --remove are mutually exclusive.')
2878
# Delete the option in the given scope
2879
self._remove_config_option(name, directory, scope)
2881
# Defaults to all options
2882
self._show_matching_options('.*', directory, scope)
2885
name, value = name.split('=', 1)
2887
# Display the option(s) value(s)
2889
self._show_matching_options(name, directory, scope)
2891
self._show_value(name, directory, scope)
2894
raise errors.BzrError(
2895
'Only one option can be set.')
2896
# Set the option value
2897
self._set_config_option(name, value, directory, scope)
2899
def _get_configs(self, directory, scope=None):
2900
"""Iterate the configurations specified by ``directory`` and ``scope``.
2902
:param directory: Where the configurations are derived from.
2904
:param scope: A specific config to start from.
2906
if scope is not None:
2907
if scope == 'bazaar':
2908
yield GlobalConfig()
2909
elif scope == 'locations':
2910
yield LocationConfig(directory)
2911
elif scope == 'branch':
2912
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2914
yield br.get_config()
2917
(_, br, _) = bzrdir.BzrDir.open_containing_tree_or_branch(
2919
yield br.get_config()
2920
except errors.NotBranchError:
2921
yield LocationConfig(directory)
2922
yield GlobalConfig()
2924
def _show_value(self, name, directory, scope):
2926
for c in self._get_configs(directory, scope):
2929
for (oname, value, section, conf_id, parser) in c._get_options():
2931
# Display only the first value and exit
2933
# FIXME: We need to use get_user_option to take policies
2934
# into account and we need to make sure the option exists
2935
# too (hence the two for loops), this needs a better API
2937
value = c.get_user_option(name)
2938
# Quote the value appropriately
2939
value = parser._quote(value)
2940
self.outf.write('%s\n' % (value,))
2944
raise errors.NoSuchConfigOption(name)
2946
def _show_matching_options(self, name, directory, scope):
2947
name = lazy_regex.lazy_compile(name)
2948
# We want any error in the regexp to be raised *now* so we need to
2949
# avoid the delay introduced by the lazy regexp. But, we still do
2950
# want the nicer errors raised by lazy_regex.
2951
name._compile_and_collapse()
2954
for c in self._get_configs(directory, scope):
2955
for (oname, value, section, conf_id, parser) in c._get_options():
2956
if name.search(oname):
2957
if cur_conf_id != conf_id:
2958
# Explain where the options are defined
2959
self.outf.write('%s:\n' % (conf_id,))
2960
cur_conf_id = conf_id
2962
if (section not in (None, 'DEFAULT')
2963
and cur_section != section):
2964
# Display the section if it's not the default (or only)
2966
self.outf.write(' [%s]\n' % (section,))
2967
cur_section = section
2968
self.outf.write(' %s = %s\n' % (oname, value))
2970
def _set_config_option(self, name, value, directory, scope):
2971
for conf in self._get_configs(directory, scope):
2972
conf.set_user_option(name, value)
2975
raise errors.NoSuchConfig(scope)
2977
def _remove_config_option(self, name, directory, scope):
2979
raise errors.BzrCommandError(
2980
'--remove expects an option to remove.')
2982
for conf in self._get_configs(directory, scope):
2983
for (section_name, section, conf_id) in conf._get_sections():
2984
if scope is not None and conf_id != scope:
2985
# Not the right configuration file
2988
if conf_id != conf.config_id():
2989
conf = self._get_configs(directory, conf_id).next()
2990
# We use the first section in the first config where the
2991
# option is defined to remove it
2992
conf.remove_user_option(name, section_name)
2997
raise errors.NoSuchConfig(scope)
2999
raise errors.NoSuchConfigOption(name)
3003
# We need adapters that can build a Store or a Stack in a test context. Test
3004
# classes, based on TestCaseWithTransport, can use the registry to parametrize
3005
# themselves. The builder will receive a test instance and should return a
3006
# ready-to-use store or stack. Plugins that define new store/stacks can also
3007
# register themselves here to be tested against the tests defined in
3008
# bzrlib.tests.test_config. Note that the builder can be called multiple times
3009
# for the same tests.
3011
# The registered object should be a callable receiving a test instance
3012
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Store
3014
test_store_builder_registry = registry.Registry()
3016
# The registered object should be a callable receiving a test instance
3017
# parameter (inheriting from tests.TestCaseWithTransport) and returning a Stack
3019
test_stack_builder_registry = registry.Registry()