a8aa43004b3bfb3c20e48799a1d74d9731a6d019
[debian/gnuradio] / grc / src / grc / elements / Param.py
1 """
2 Copyright 2008 Free Software Foundation, Inc.
3 This file is part of GNU Radio
4
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.
9
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.
14
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
18 """
19 ##@package grc.elements.Param
20 #Flow graph block parameters.
21 #And options for enum type paramater.
22
23 from grc import Utils
24 from grc.Utils import odict
25 from grc.elements.Element import Element
26
27 class Option(Element):
28
29         def __init__(self, param, name, key, opts):
30                 Element.__init__(self, param)
31                 self._name = name
32                 self._key = key
33                 self._opts = dict()
34                 for opt in opts:
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)
41                         #store the option
42                         self._opts[key] = value
43
44         def __str__(self): return 'Option %s(%s)'%(self.get_name(), self.get_key())
45
46         def get_name(self): return self._name
47
48         def get_key(self): return self._key
49
50         ##############################################
51         # Access Opts
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()
56
57         ##############################################
58         ## Static Make Methods
59         ##############################################
60         def make_option_from_n(param, n):
61                 """
62                 Make a new option from nested data.
63                 @param param the parent element
64                 @param n the nested odict
65                 @return a new option
66                 """
67                 #grab the data
68                 name = n['name']
69                 key = n['key']
70                 opts = Utils.listify(n, 'opt')
71                 #build the option
72                 return Option(
73                         param=param,
74                         name=name,
75                         key=key,
76                         opts=opts,
77                 )
78         make_option_from_n = staticmethod(make_option_from_n)
79
80 class Param(Element):
81
82         ##possible param types
83         TYPES = ['enum', 'raw']
84
85         def __init__(self, block, n):
86                 """
87                 Make a new param from nested data.
88                 @param block the parent element
89                 @param n the nested odict
90                 @return a new param
91                 """
92                 #grab the data
93                 name = n['name']
94                 key = n['key']
95                 value = Utils.exists_or_else(n, 'value', '')
96                 type = n['type']
97                 hide = Utils.exists_or_else(n, 'hide', '')
98                 options = Utils.listify(n, 'option')
99                 #build the param
100                 Element.__init__(self, block)
101                 self._name = name
102                 self._key = key
103                 self._type = type
104                 self._hide = hide
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)
112                         #store the option
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 ''
134
135         def test(self):
136                 """
137                 call test on all children
138                 """
139                 map(lambda c: c.test(), self.get_options())
140
141         def validate(self):
142                 """
143                 Validate the param.
144                 The value must be evaluated and type must a possible type.
145                 """
146                 try:
147                         assert(self.get_type() in self.TYPES)
148                         try: self.evaluate()
149                         except:
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())
154
155         def evaluate(self):
156                 """!
157                 Evaluate the value of this param.
158                 @throw NotImplementedError
159                 """
160                 raise NotImplementedError
161
162         def to_code(self):
163                 """!
164                 Convert the value to code.
165                 @throw NotImplementedError
166                 """
167                 raise NotImplementedError
168
169         def __str__(self): return 'Param - %s(%s)'%(self.get_name(), self.get_key())
170
171         def is_param(self): return True
172
173         def get_name(self): return self._name
174
175         def get_key(self): return self._key
176
177         def get_hide(self): return self.get_parent().resolve_dependencies(self._hide)
178
179         def get_value(self):
180                 value = self._value
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)
184                 return value
185
186         def set_value(self, value):
187                 self.flag()
188                 self._value = str(value) #must be a string
189
190         def get_type(self): return self.get_parent().resolve_dependencies(self._type)
191
192         def is_enum(self): return self._type == 'enum'
193
194         def is_type_dependent(self): return '$' in self._type
195
196         ##############################################
197         # Access Options
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()
202
203         ##############################################
204         # Access Opts
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()
209
210         ##############################################
211         ## Import/Export Methods
212         ##############################################
213         def export_data(self):
214                 """
215                 Export this param's key/value.
216                 @return a nested data odict
217                 """
218                 n = odict()
219                 n['key'] = self.get_key()
220                 n['value'] = self.get_value()
221                 return n