2 Copyright 2008 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
19 ##@package grc.elements.Param
20 #Flow graph block parameters.
21 #And options for enum type paramater.
24 from grc.Utils import odict
25 from grc.elements.Element import Element
27 class Option(Element):
29 def __init__(self, param, name, key, opts):
30 Element.__init__(self, param)
35 #separate the key:value
36 try: key, value = opt.split(':')
37 except: self._exit_with_error('Error separating "%s" into key:value'%opt)
38 #test against repeated keys
39 try: assert(not self._opts.has_key(key))
40 except AssertionError: self._exit_with_error('Key "%s" already exists in option'%key)
42 self._opts[key] = value
44 def __str__(self): return 'Option %s(%s)'%(self.get_name(), self.get_key())
46 def get_name(self): return self._name
48 def get_key(self): return self._key
50 ##############################################
52 ##############################################
53 def get_opt_keys(self): return self._opts.keys()
54 def get_opt(self, key): return self._opts[key]
55 def get_opts(self): return self._opts.values()
57 ##############################################
58 ## Static Make Methods
59 ##############################################
60 def make_option_from_n(param, n):
62 Make a new option from nested data.
63 @param param the parent element
64 @param n the nested odict
70 opts = Utils.listify(n, 'opt')
78 make_option_from_n = staticmethod(make_option_from_n)
82 ##possible param types
83 TYPES = ['enum', 'raw']
85 def __init__(self, block, n):
87 Make a new param from nested data.
88 @param block the parent element
89 @param n the nested odict
95 value = Utils.exists_or_else(n, 'value', '')
97 hide = Utils.exists_or_else(n, 'hide', '')
98 options = Utils.listify(n, 'option')
100 Element.__init__(self, block)
105 #create the Option objects from the n data
106 self._options = odict()
107 for option in map(lambda o: Option.make_option_from_n(self, o), options):
108 key = option.get_key()
109 #test against repeated keys
110 try: assert(key not in self.get_option_keys())
111 except AssertionError: self._exit_with_error('Key "%s" already exists in options'%key)
113 self._options[key] = option
114 #test the enum options
115 if self._options or self.is_enum():
116 #test against bad combos of type and enum
117 try: assert(self._options)
118 except AssertionError: self._exit_with_error('At least one option must exist when type "enum" is set.')
119 try: assert(self.is_enum())
120 except AssertionError: self._exit_with_error('Type "enum" must be set when options are present.')
121 #test against options with identical keys
122 try: assert(len(set(self.get_option_keys())) == len(self._options))
123 except AssertionError: self._exit_with_error('Options keys "%s" are not unique.'%self.get_option_keys())
124 #test against inconsistent keys in options
125 opt_keys = self._options.values()[0].get_opt_keys()
126 for option in self._options.values():
127 try: assert(set(opt_keys) == set(option.get_opt_keys()))
128 except AssertionError: self._exit_with_error('Opt keys "%s" are not identical across all options.'%opt_keys)
129 #if a value is specified, it must be in the options keys
130 self._value = value or self.get_option_keys()[0]
131 try: assert(self.get_value() in self.get_option_keys())
132 except AssertionError: self._exit_with_error('The value "%s" is not in the possible values of "%s".'%(self.get_value(), self.get_option_keys()))
133 else: self._value = value or ''
137 call test on all children
139 map(lambda c: c.test(), self.get_options())
144 The value must be evaluated and type must a possible type.
147 assert(self.get_type() in self.TYPES)
150 #if the evaluate failed but added no error messages, add the generic one below
151 if not self.get_error_messages():
152 self._add_error_message('Value "%s" cannot be evaluated.'%self.get_value())
153 except AssertionError: self._add_error_message('Type "%s" is not a possible type.'%self.get_type())
157 Evaluate the value of this param.
158 @throw NotImplementedError
160 raise NotImplementedError
164 Convert the value to code.
165 @throw NotImplementedError
167 raise NotImplementedError
169 def __str__(self): return 'Param - %s(%s)'%(self.get_name(), self.get_key())
171 def is_param(self): return True
173 def get_name(self): return self._name
175 def get_key(self): return self._key
177 def get_hide(self): return self.get_parent().resolve_dependencies(self._hide)
181 if self.is_enum() and value not in self.get_option_keys():
182 value = self.get_option_keys()[0]
183 self.set_value(value)
186 def set_value(self, value):
188 self._value = str(value) #must be a string
190 def get_type(self): return self.get_parent().resolve_dependencies(self._type)
192 def is_enum(self): return self._type == 'enum'
194 def is_type_dependent(self): return '$' in self._type
196 ##############################################
198 ##############################################
199 def get_option_keys(self): return self._options.keys()
200 def get_option(self, key): return self._options[key]
201 def get_options(self): return self._options.values()
203 ##############################################
205 ##############################################
206 def get_opt_keys(self): return self._options[self.get_value()].get_opt_keys()
207 def get_opt(self, key): return self._options[self.get_value()].get_opt(key)
208 def get_opts(self): return self._options[self.get_value()].get_opts()
210 ##############################################
211 ## Import/Export Methods
212 ##############################################
213 def export_data(self):
215 Export this param's key/value.
216 @return a nested data odict
219 n['key'] = self.get_key()
220 n['value'] = self.get_value()