2 # Copyright 2004 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
22 '''Add support for engineering notation to optparse.OptionParser'''
25 from optparse import Option, OptionValueError
28 def check_eng_float (option, opt, value):
30 return eng_notation.str_to_num(value)
32 raise OptionValueError (
33 "option %s: invalid engineering notation value: %r" % (opt, value))
35 def check_intx (option, opt, value):
39 raise OptionValueError (
40 "option %s: invalid integer value: %r" % (opt, value))
42 def check_subdev (option, opt, value):
44 Value has the form: (A|B)(:0|1)?
46 @returns a 2-tuple (0|1, 0|1)
48 d = { 'A' : (0, 0), 'A:0' : (0, 0), 'A:1' : (0, 1), 'A:2' : (0, 2),
49 'B' : (1, 0), 'B:0' : (1, 0), 'B:1' : (1, 1), 'B:2' : (1, 2) }
51 return d[value.upper()]
53 raise OptionValueError(
54 "option %s: invalid subdev: '%r', must be one of %s" % (opt, value, ', '.join(sorted(d.keys()))))
56 class eng_option (Option):
57 TYPES = Option.TYPES + ("eng_float", "intx", "subdev")
58 TYPE_CHECKER = copy (Option.TYPE_CHECKER)
59 TYPE_CHECKER["eng_float"] = check_eng_float
60 TYPE_CHECKER["intx"] = check_intx
61 TYPE_CHECKER["subdev"] = check_subdev