2272
2272
The option *values* are stored in config files and found in sections.
2274
2274
Here we define various properties about the option itself, its default
2275
value, in which config files it can be stored, etc (TBC).
2275
value, how to convert it from stores, what to do when invalid values are
2276
encoutered, in which config files it can be stored.
2278
def __init__(self, name, default=None, help=None, from_unicode=None):
2279
def __init__(self, name, default=None, help=None, from_unicode=None,
2281
"""Build an option definition.
2283
:param name: the name used to refer to the option.
2285
:param default: the default value to use when none exist in the config
2288
:param help: a doc string to explain the option to the user.
2290
:param from_unicode: a callable to convert the unicode string
2291
representing the option value in a store. This is not called for
2294
:param invalid: the action to be taken when an invalid value is
2295
encountered in a store. This is called only when from_unicode is
2296
invoked to convert a string and returns None or raise
2297
ValueError. Accepted values are: None (ignore invalid values),
2298
'warning' (emit a warning), 'error' emit an error message and
2279
2301
self.name = name
2280
2302
self.default = default
2281
2303
self.help = help
2282
2304
self.from_unicode = from_unicode
2305
if invalid and invalid not in ('warning', 'error'):
2306
raise AssertionError("%s not supported for 'invalid'" % (invalid,))
2307
self.invalid = invalid
2284
2309
def get_default(self):
2285
2310
return self.default
2829
2854
and value is not None):
2830
2855
# If a value exists and the option provides a converter, use it
2832
value = opt.from_unicode(value)
2857
converted = opt.from_unicode(value)
2833
2858
except ValueError:
2834
2859
# Invalid values are ignored
2861
if converted is None and opt.invalid is not None:
2862
# The conversion failed
2863
if opt.invalid == 'warning':
2864
trace.warning('Value "%s" is not valid for "%s"',
2866
elif opt.invalid == 'error':
2867
raise errors.ConfigOptionValueError(name, value)
2836
2869
if value is None:
2837
2870
# If the option is registered, it may provide a default value
2838
2871
if opt is not None: