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