597
547
return command_line
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()
670
550
class IniBasedConfig(Config):
671
551
"""A configuration policy that draws from ini files."""
2226
2072
del configobj[option_name]
2228
2074
del configobj[section_name][option_name]
2229
for hook in OldConfigHooks['remove']:
2230
hook(self, option_name)
2231
2075
self._set_configobj(configobj)
2233
2077
def _get_config_file(self):
2235
f = StringIO(self._transport.get_bytes(self._filename))
2236
for hook in OldConfigHooks['load']:
2079
return StringIO(self._transport.get_bytes(self._filename))
2239
2080
except errors.NoSuchFile:
2240
2081
return StringIO()
2242
def _external_url(self):
2243
return urlutils.join(self._transport.external_url(), self._filename)
2245
2083
def _get_configobj(self):
2246
2084
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())
2086
return ConfigObj(f, encoding='utf-8')
2258
2090
def _set_configobj(self, configobj):
2259
2091
out_file = StringIO()
2260
2092
configobj.write(out_file)
2261
2093
out_file.seek(0)
2262
2094
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('bzr.workingtree.worth_saving_limit', default=10,
2390
from_unicode=int_from_store, invalid='warning',
2392
How many changes before saving the dirstate.
2394
-1 means that we will never rewrite the dirstate file for only
2395
stat-cache changes. Regardless of this setting, we will always rewrite
2396
the dirstate file if a file is added/removed/renamed/etc. This flag only
2397
affects the behavior of updating the dirstate file after we notice that
2398
a file has been touched.
2400
option_registry.register(
2401
Option('dirstate.fdatasync', default=True,
2402
from_unicode=bool_from_store,
2404
Flush dirstate changes onto physical disk?
2406
If true (default), working tree metadata changes are flushed through the
2407
OS buffers to physical disk. This is somewhat slower, but means data
2408
should not be lost if the machine crashes. See also repository.fdatasync.
2410
option_registry.register(
2411
Option('debug_flags', default=[], from_unicode=list_from_store,
2412
help='Debug flags to activate.'))
2413
option_registry.register(
2414
Option('default_format', default='2a',
2415
help='Format used when creating branches.'))
2416
option_registry.register(
2418
help='The command called to launch an editor to enter a message.'))
2419
option_registry.register(
2420
Option('ignore_missing_extensions', default=False,
2421
from_unicode=bool_from_store,
2423
Control the missing extensions warning display.
2425
The warning will not be emitted if set to True.
2427
option_registry.register(
2429
help='Language to translate messages into.'))
2430
option_registry.register(
2431
Option('locks.steal_dead', default=False, from_unicode=bool_from_store,
2433
Steal locks that appears to be dead.
2435
If set to True, bzr will check if a lock is supposed to be held by an
2436
active process from the same user on the same machine. If the user and
2437
machine match, but no process with the given PID is active, then bzr
2438
will automatically break the stale lock, and create a new lock for
2440
Otherwise, bzr will prompt as normal to break the lock.
2442
option_registry.register(
2443
Option('output_encoding',
2444
help= 'Unicode encoding for output'
2445
' (terminal encoding if not specified).'))
2446
option_registry.register(
2447
Option('repository.fdatasync', default=True, from_unicode=bool_from_store,
2449
Flush repository changes onto physical disk?
2451
If true (default), repository changes are flushed through the OS buffers
2452
to physical disk. This is somewhat slower, but means data should not be
2453
lost if the machine crashes. See also dirstate.fdatasync.
2457
2097
class Section(object):
2458
"""A section defines a dict of option name => value.
2098
"""A section defines a dict of options.
2460
2100
This is merely a read-only dict which can add some knowledge about the
2461
2101
options. It is *not* a python dict object though and doesn't try to mimic
2951
2507
"""Set a new value for the option."""
2952
2508
section = self._get_mutable_section()
2953
2509
section.set(name, value)
2954
for hook in ConfigHooks['set']:
2955
hook(self, name, value)
2957
2511
def remove(self, name):
2958
2512
"""Remove an existing option."""
2959
2513
section = self._get_mutable_section()
2960
2514
section.remove(name)
2961
for hook in ConfigHooks['remove']:
2964
2516
def __repr__(self):
2965
2517
# Mostly for debugging use
2966
2518
return "<config.%s(%s)>" % (self.__class__.__name__, id(self))
2969
class _CompatibleStack(Stack):
2970
"""Place holder for compatibility with previous design.
2972
This is intended to ease the transition from the Config-based design to the
2973
Stack-based design and should not be used nor relied upon by plugins.
2975
One assumption made here is that the daughter classes will all use Stores
2976
derived from LockableIniFileStore).
2978
It implements set() by re-loading the store before applying the
2979
modification and saving it.
2981
The long term plan being to implement a single write by store to save
2982
all modifications, this class should not be used in the interim.
2985
def set(self, name, value):
2988
super(_CompatibleStack, self).set(name, value)
2989
# Force a write to persistent storage
2993
class GlobalStack(_CompatibleStack):
2997
gstore = GlobalStore()
2998
super(GlobalStack, self).__init__([gstore.get_sections], gstore)
3001
class LocationStack(_CompatibleStack):
3003
def __init__(self, location):
3004
"""Make a new stack for a location and global configuration.
3006
:param location: A URL prefix to """
3007
lstore = LocationStore()
3008
matcher = LocationMatcher(lstore, location)
3009
gstore = GlobalStore()
3010
super(LocationStack, self).__init__(
3011
[matcher.get_sections, gstore.get_sections], lstore)
3013
class BranchStack(_CompatibleStack):
3015
def __init__(self, branch):
3016
bstore = BranchStore(branch)
3017
lstore = LocationStore()
3018
matcher = LocationMatcher(lstore, branch.base)
3019
gstore = GlobalStore()
3020
super(BranchStack, self).__init__(
3021
[matcher.get_sections, bstore.get_sections, gstore.get_sections],
3023
self.branch = branch
3026
2521
class cmd_config(commands.Command):
3027
2522
__doc__ = """Display, set or remove a configuration option.