Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / src / platforms / base / Element.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
20 class Element(object):
21
22         def __init__(self, parent=None):
23                 self._parent = parent
24                 self._error_messages = []
25                 self.flag()
26
27         def test(self):
28                 """
29                 Test the element against failures.
30                 Overload this method in sub-classes.
31                 """
32                 pass
33
34         def validate(self):
35                 """
36                 Validate the data in this element.
37                 Set the error message non blank for errors.
38                 Overload this method in sub-classes.
39                 """
40                 pass
41
42         def is_valid(self):
43                 self._error_messages = []#reset err msgs
44                 try: self.validate()
45                 except: pass
46                 return not self.get_error_messages()
47
48         def _add_error_message(self, msg):
49                 self._error_messages.append(msg)
50
51         def get_error_messages(self):
52                 return self._error_messages
53
54         def get_parent(self):
55                 return self._parent
56
57         def _exit_with_error(self, error):
58                 parent = self
59                 #build hier list of elements
60                 elements = list()
61                 while(parent):
62                         elements.insert(0, parent)
63                         parent = parent.get_parent()
64                 #build error string
65                 err_str = ">>> Error:"
66                 for i, element in enumerate(elements + [error]):
67                         err_str = err_str + '\n' + ''.join('   '*(i+2)) + str(element)
68                 err_str = err_str + '\n'
69                 exit(err_str)
70
71         ##############################################
72         ## Update flagging
73         ##############################################
74         def is_flagged(self): return self._flag
75         def flag(self):
76                 self._flag = True
77                 if self.get_parent(): self.get_parent().flag()
78         def deflag(self):
79                 self._flag = False
80                 if self.get_parent(): self.get_parent().deflag()
81
82         ##############################################
83         ## Type testing methods
84         ##############################################
85         def is_element(self): return True
86         def is_platform(self): return False
87         def is_flow_graph(self): return False
88         def is_connection(self): return False
89         def is_block(self): return False
90         def is_source(self): return False
91         def is_sink(self): return False
92         def is_port(self): return False
93         def is_param(self): return False