2261
2270
The option *values* are stored in config files and found in sections.
2263
2272
Here we define various properties about the option itself, its default
2264
value, in which config files it can be stored, etc (TBC).
2273
value, how to convert it from stores, what to do when invalid values are
2274
encoutered, in which config files it can be stored.
2267
def __init__(self, name, default=None):
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
2268
2299
self.name = name
2269
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
2271
2307
def get_default(self):
2272
2308
return self.default
2277
option_registry = registry.Registry()
2280
option_registry.register(
2281
'editor', Option('editor'),
2282
help='The command called to launch an editor to enter a message.')
2310
# Predefined converters to get proper values from store
2312
def bool_from_store(unicode_str):
2313
return ui.bool_from_string(unicode_str)
2316
def int_from_store(unicode_str):
2317
return int(unicode_str)
2320
def list_from_store(unicode_str):
2321
# ConfigObj return '' instead of u''. Use 'str' below to catch all cases.
2322
if isinstance(unicode_str, (str, unicode)):
2324
# A single value, most probably the user forgot (or didn't care to
2325
# add) the final ','
2328
# The empty string, convert to empty list
2331
# We rely on ConfigObj providing us with a list already
2336
class OptionRegistry(registry.Registry):
2337
"""Register config options by their name.
2339
This overrides ``registry.Registry`` to simplify registration by acquiring
2340
some information from the option object itself.
2343
def register(self, option):
2344
"""Register a new option to its name.
2346
:param option: The option to register. Its name is used as the key.
2348
super(OptionRegistry, self).register(option.name, option,
2351
def register_lazy(self, key, module_name, member_name):
2352
"""Register a new option to be loaded on request.
2354
:param key: This is the key to use to request the option later. Since
2355
the registration is lazy, it should be provided and match the
2358
:param module_name: The python path to the module. Such as 'os.path'.
2360
:param member_name: The member of the module to return. If empty or
2361
None, get() will return the module itself.
2363
super(OptionRegistry, self).register_lazy(key,
2364
module_name, member_name)
2366
def get_help(self, key=None):
2367
"""Get the help text associated with the given key"""
2368
option = self.get(key)
2369
the_help = option.help
2370
if callable(the_help):
2371
return the_help(self, key)
2375
option_registry = OptionRegistry()
2378
# Registered options in lexicographical order
2380
option_registry.register(
2381
Option('dirstate.fdatasync', default=True, from_unicode=bool_from_store,
2383
Flush dirstate changes onto physical disk?
2385
If true (default), working tree metadata changes are flushed through the
2386
OS buffers to physical disk. This is somewhat slower, but means data
2387
should not be lost if the machine crashes. See also repository.fdatasync.
2389
option_registry.register(
2390
Option('default_format', default='2a',
2391
help='Format used when creating branches.'))
2392
option_registry.register(
2394
help='The command called to launch an editor to enter a message.'))
2395
option_registry.register(
2397
help='Language to translate messages into.'))
2398
option_registry.register(
2399
Option('output_encoding',
2400
help= 'Unicode encoding for output'
2401
' (terminal encoding if not specified).'))
2402
option_registry.register(
2403
Option('repository.fdatasync', default=True, from_unicode=bool_from_store,
2405
Flush repository changes onto physical disk?
2407
If true (default), repository changes are flushed through the OS buffers
2408
to physical disk. This is somewhat slower, but means data should not be
2409
lost if the machine crashes. See also dirstate.fdatasync.
2285
2413
class Section(object):
2552
2680
class GlobalStore(LockableIniFileStore):
2554
2682
def __init__(self, possible_transports=None):
2555
t = transport.get_transport(config_dir(),
2556
possible_transports=possible_transports)
2683
t = transport.get_transport_from_path(
2684
config_dir(), possible_transports=possible_transports)
2557
2685
super(GlobalStore, self).__init__(t, 'bazaar.conf')
2560
2688
class LocationStore(LockableIniFileStore):
2562
2690
def __init__(self, possible_transports=None):
2563
t = transport.get_transport(config_dir(),
2564
possible_transports=possible_transports)
2691
t = transport.get_transport_from_path(
2692
config_dir(), possible_transports=possible_transports)
2565
2693
super(LocationStore, self).__init__(t, 'locations.conf')
2734
2862
if value is not None:
2864
# If the option is registered, it may provide additional info about
2867
opt = option_registry.get(name)
2871
if (opt is not None and opt.from_unicode is not None
2872
and value is not None):
2873
# If a value exists and the option provides a converter, use it
2875
converted = opt.from_unicode(value)
2876
except (ValueError, TypeError):
2877
# Invalid values are ignored
2879
if converted is None and opt.invalid is not None:
2880
# The conversion failed
2881
if opt.invalid == 'warning':
2882
trace.warning('Value "%s" is not valid for "%s"',
2884
elif opt.invalid == 'error':
2885
raise errors.ConfigOptionValueError(name, value)
2736
2887
if value is None:
2737
2888
# If the option is registered, it may provide a default value
2739
opt = option_registry.get(name)
2743
2889
if opt is not None:
2744
2890
value = opt.get_default()
2745
2891
for hook in ConfigHooks['get']: