2 Copyright 2008, 2009 Free Software Foundation, Inc.
3 This file is part of GNU Radio
5 GNU Radio Companion is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 GNU Radio Companion is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 from Element import Element
23 def _get_keys(lst): return [elem.get_key() for elem in lst]
24 def _get_elem(lst, key):
25 try: return lst[_get_keys(lst).index(key)]
26 except ValueError: raise ValueError, 'Key "%s" not found in %s.'%(key, _get_keys(lst))
28 class Option(Element):
30 def __init__(self, param, n):
31 Element.__init__(self, param)
32 self._name = n.find('name')
33 self._key = n.find('key')
35 opts = n.findall('opt')
36 #test against opts when non enum
37 try: assert self.get_parent().is_enum() or not opts
38 except AssertionError: raise Exception, 'Options for non-enum types cannot have sub-options'
41 #separate the key:value
42 try: key, value = opt.split(':')
43 except: raise Exception, 'Error separating "%s" into key:value'%opt
44 #test against repeated keys
45 try: assert not self._opts.has_key(key)
46 except AssertionError: raise Exception, 'Key "%s" already exists in option'%key
48 self._opts[key] = value
50 def __str__(self): return 'Option %s(%s)'%(self.get_name(), self.get_key())
51 def get_name(self): return self._name
52 def get_key(self): return self._key
54 ##############################################
56 ##############################################
57 def get_opt_keys(self): return self._opts.keys()
58 def get_opt(self, key): return self._opts[key]
59 def get_opts(self): return self._opts.values()
63 def __init__(self, block, n):
65 Make a new param from nested data.
66 @param block the parent element
67 @param n the nested odict
70 self._name = n.find('name')
71 self._key = n.find('key')
72 value = n.find('value') or ''
73 self._type = n.find('type')
74 self._hide = n.find('hide') or ''
76 Element.__init__(self, block)
77 #create the Option objects from the n data
78 self._options = list()
79 for option in map(lambda o: Option(param=self, n=o), n.findall('option')):
80 key = option.get_key()
81 #test against repeated keys
82 try: assert key not in self.get_option_keys()
83 except AssertionError: raise Exception, 'Key "%s" already exists in options'%key
85 self.get_options().append(option)
86 #test the enum options
88 #test against options with identical keys
89 try: assert len(set(self.get_option_keys())) == len(self.get_options())
90 except AssertionError: raise Exception, 'Options keys "%s" are not unique.'%self.get_option_keys()
91 #test against inconsistent keys in options
92 opt_keys = self.get_options()[0].get_opt_keys()
93 for option in self.get_options():
94 try: assert set(opt_keys) == set(option.get_opt_keys())
95 except AssertionError: raise Exception, 'Opt keys "%s" are not identical across all options.'%opt_keys
96 #if a value is specified, it must be in the options keys
97 self._value = value or self.get_option_keys()[0]
98 try: assert self.get_value() in self.get_option_keys()
99 except AssertionError: raise Exception, 'The value "%s" is not in the possible values of "%s".'%(self.get_value(), self.get_option_keys())
100 else: self._value = value or ''
106 call test on all children
108 map(lambda c: c.test(), self.get_options())
113 The value must be evaluated and type must a possible type.
115 Element.validate(self)
116 try: assert self.get_type() in self.get_types()
117 except AssertionError: self.add_error_message('Type "%s" is not a possible type.'%self.get_type())
119 def get_evaluated(self): raise NotImplementedError
123 Convert the value to code.
124 @throw NotImplementedError
126 raise NotImplementedError
130 Get a list of all possible param types.
131 @throw NotImplementedError
133 raise NotImplementedError
135 def get_color(self): return '#FFFFFF'
136 def __str__(self): return 'Param - %s(%s)'%(self.get_name(), self.get_key())
137 def is_param(self): return True
138 def get_name(self): return self._name
139 def get_key(self): return self._key
140 def get_hide(self): return self.get_parent().resolve_dependencies(self._hide).strip()
144 if self.is_enum() and value not in self.get_option_keys():
145 value = self.get_option_keys()[0]
146 self.set_value(value)
149 def set_value(self, value):
151 self._value = str(value) #must be a string
153 def get_type(self): return self.get_parent().resolve_dependencies(self._type)
154 def is_enum(self): return self._type == 'enum'
158 Get the repr (nice string format) for this param.
159 Just return the value (special case enum).
160 Derived classes can handle complex formatting.
161 @return the string representation
163 if self.is_enum(): return self.get_option(self.get_value()).get_name()
164 return self.get_value()
166 ##############################################
168 ##############################################
169 def get_option_keys(self): return _get_keys(self.get_options())
170 def get_option(self, key): return _get_elem(self.get_options(), key)
171 def get_options(self): return self._options
173 ##############################################
175 ##############################################
176 def get_opt_keys(self): return self.get_option(self.get_value()).get_opt_keys()
177 def get_opt(self, key): return self.get_option(self.get_value()).get_opt(key)
178 def get_opts(self): return self.get_option(self.get_value()).get_opts()
180 ##############################################
181 ## Import/Export Methods
182 ##############################################
183 def export_data(self):
185 Export this param's key/value.
186 @return a nested data odict
189 n['key'] = self.get_key()
190 n['value'] = self.get_value()