Merge branch 'dfsg-orig'
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / eng_option.py
1 #
2 # Copyright 2004 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
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)
9 # any later version.
10
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.
15
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.
20
21
22 '''Add support for engineering notation to optparse.OptionParser'''
23
24 from copy import copy
25 from optparse import Option, OptionValueError
26 import eng_notation
27
28 def check_eng_float (option, opt, value):
29     try:
30         return eng_notation.str_to_num(value)
31     except:
32         raise OptionValueError (
33             "option %s: invalid engineering notation value: %r" % (opt, value))
34
35 def check_intx (option, opt, value):
36     try:
37         return int (value, 0)
38     except:
39         raise OptionValueError (
40             "option %s: invalid integer value: %r" % (opt, value))
41
42 def check_subdev (option, opt, value):
43     """
44     Value has the form: (A|B)(:0|1)?
45
46     @returns a 2-tuple (0|1, 0|1)
47     """
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) }
50     try:
51         return d[value.upper()]
52     except:
53         raise OptionValueError(
54             "option %s: invalid subdev: '%r', must be one of %s" % (opt, value, ', '.join(sorted(d.keys()))))
55
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
62