added rewrite methods to element to separate from validation logic
[debian/gnuradio] / grc / base / Element.py
1 """
2 Copyright 2008, 2009 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
20 class Element(object):
21
22         def __init__(self, parent=None):
23                 self._parent = parent
24                 self.flag()
25
26         def test(self):
27                 """
28                 Test the element against failures.
29                 Overload this method in sub-classes.
30                 """
31                 pass
32
33         ##################################################
34         # Element Validation API
35         ##################################################
36         def validate(self): self._error_messages = list()
37         def is_valid(self): return not self.get_error_messages() or not self.get_enabled()
38         def add_error_message(self, msg): self._error_messages.append(msg)
39         def get_error_messages(self): return self._error_messages
40
41         def rewrite(self): pass
42
43         def get_enabled(self): return True
44
45         def get_parent(self): return self._parent
46
47         ##############################################
48         ## Update flagging
49         ##############################################
50         def is_flagged(self): return self._flag
51         def flag(self):
52                 self._flag = True
53                 if self.get_parent(): self.get_parent().flag()
54         def deflag(self):
55                 self._flag = False
56                 if self.get_parent(): self.get_parent().deflag()
57
58         ##############################################
59         ## Type testing methods
60         ##############################################
61         def is_element(self): return True
62         def is_platform(self): return False
63         def is_flow_graph(self): return False
64         def is_connection(self): return False
65         def is_block(self): return False
66         def is_source(self): return False
67         def is_sink(self): return False
68         def is_port(self): return False
69         def is_param(self): return False