~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/config.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2011-08-12 07:57:38 UTC
  • mfrom: (6059.1.5 invalid-config-value)
  • Revision ID: pqm@pqm.ubuntu.com-20110812075738-z6kjnvy20806946j
(vila) Allow option to require a warning or error when an invalid value is
 found in a config file. (Vincent Ladeuil)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2272
2272
    The option *values* are stored in config files and found in sections.
2273
2273
 
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.
2276
2277
    """
2277
2278
 
2278
 
    def __init__(self, name, default=None, help=None, from_unicode=None):
 
2279
    def __init__(self, name, default=None, help=None, from_unicode=None,
 
2280
                 invalid=None):
 
2281
        """Build an option definition.
 
2282
 
 
2283
        :param name: the name used to refer to the option.
 
2284
 
 
2285
        :param default: the default value to use when none exist in the config
 
2286
            stores.
 
2287
 
 
2288
        :param help: a doc string to explain the option to the user.
 
2289
 
 
2290
        :param from_unicode: a callable to convert the unicode string
 
2291
            representing the option value in a store. This is not called for
 
2292
            the default value.
 
2293
 
 
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
 
2299
            terminates.
 
2300
        """
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
2283
2308
 
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
2831
2856
            try:
2832
 
                value = opt.from_unicode(value)
 
2857
                converted = opt.from_unicode(value)
2833
2858
            except ValueError:
2834
2859
                # Invalid values are ignored
2835
 
                value = None
 
2860
                converted = None
 
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"',
 
2865
                                  value, name)
 
2866
                elif opt.invalid == 'error':
 
2867
                    raise errors.ConfigOptionValueError(name, value)
 
2868
            value = converted
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: