switch source package format to 3.0 quilt
[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
25         ##################################################
26         # Element Validation API
27         ##################################################
28         def validate(self):
29                 """
30                 Validate this element and call validate on all children.
31                 Call this base method before adding error messages in the subclass.
32                 """
33                 self._error_messages = list()
34                 for child in self.get_children(): child.validate()
35
36         def is_valid(self):
37                 """
38                 Is this element valid?
39                 @return true when the element is enabled and has no error messages
40                 """
41                 return not self.get_error_messages() or not self.get_enabled()
42
43         def add_error_message(self, msg):
44                 """
45                 Add an error message to the list of errors.
46                 @param msg the error message string
47                 """
48                 self._error_messages.append(msg)
49
50         def get_error_messages(self):
51                 """
52                 Get the list of error messages from this element and all of its children.
53                 Do not include the error messages from disabled children.
54                 Cleverly indent the children error messages for printing purposes.
55                 @return a list of error message strings
56                 """
57                 error_messages = list(self._error_messages) #make a copy
58                 for child in filter(lambda c: c.get_enabled(), self.get_children()):
59                         for msg in child.get_error_messages():
60                                 error_messages.append("%s:\n\t%s"%(child, msg.replace("\n", "\n\t")))
61                 return error_messages
62
63         def rewrite(self):
64                 """
65                 Rewrite this element and call rewrite on all children.
66                 Call this base method before rewriting the element.
67                 """
68                 for child in self.get_children(): child.rewrite()
69
70         def get_enabled(self): return True
71
72         ##############################################
73         ## Tree-like API
74         ##############################################
75         def get_parent(self): return self._parent
76         def get_children(self): return list()
77
78         ##############################################
79         ## Type testing methods
80         ##############################################
81         def is_element(self): return True
82         def is_platform(self): return False
83         def is_flow_graph(self): return False
84         def is_connection(self): return False
85         def is_block(self): return False
86         def is_source(self): return False
87         def is_sink(self): return False
88         def is_port(self): return False
89         def is_param(self): return False