diff options
Diffstat (limited to 'pygments/util.py')
-rw-r--r-- | pygments/util.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/pygments/util.py b/pygments/util.py new file mode 100644 index 00000000..65261964 --- /dev/null +++ b/pygments/util.py @@ -0,0 +1,50 @@ +# -*- coding: utf-8 -*- +""" + pygments.util + ~~~~~~~~~~~~ + + Utility functions. + + :copyright: 2006 by Georg Brandl. + :license: GNU LGPL, see LICENSE for more details. +""" + + +class OptionError(Exception): + pass + + +def get_bool_opt(options, optname, default=None): + string = options.get(optname, default) + if isinstance(string, bool): + return string + elif string.lower() in ('1', 'yes', 'true', 'on'): + return True + elif string.lower() in ('0', 'no', 'false', 'off'): + return False + else: + raise OptionError('Invalid value %r for option %s; use ' + '1/0, yes/no, true/false, on/off' % + string, optname) + + +def get_int_opt(options, optname, default=None): + string = options.get(optname, default) + try: + return int(string) + except ValueError: + raise OptionError('Invalid value %r for option %s; you ' + 'must give an integer value' % + string, optname) + + +def get_list_opt(options, optname, default=None): + val = options.get(optname, default) + if isinstance(val, basestring): + return val.split() + elif isinstance(val, (list, tuple)): + return list(val) + else: + raise OptionError('Invalid value %r for option %s; you ' + 'must give a list value' % + val, optname) |