Element.py

Go to the documentation of this file.
00001 """
00002 Copyright 2008 Free Software Foundation, Inc.
00003 This file is part of GNU Radio
00004 
00005 GNU Radio Companion is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 GNU Radio Companion is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
00018 """
00019 ##@package grc.elements.Element
00020 #The base class for all elements.
00021 #@author Josh Blum
00022 
00023 class Element(object):
00024 
00025         def __init__(self, parent=None):
00026                 self._parent = parent
00027                 self._error_messages = []
00028                 self.flag()
00029 
00030         def test(self):
00031                 """
00032                 Test the element against failures.
00033                 Overload this method in sub-classes.
00034                 """
00035                 pass
00036 
00037         def validate(self):
00038                 """
00039                 Validate the data in this element.
00040                 Set the error message non blank for errors.
00041                 Overload this method in sub-classes.
00042                 """
00043                 pass
00044 
00045         def is_valid(self):
00046                 self._error_messages = []#reset err msgs
00047                 try: self.validate()
00048                 except: pass
00049                 return not self.get_error_messages()
00050 
00051         def _add_error_message(self, msg):
00052                 self._error_messages.append(msg)
00053 
00054         def get_error_messages(self):
00055                 return self._error_messages
00056 
00057         def get_parent(self):
00058                 return self._parent
00059 
00060         def _exit_with_error(self, error):
00061                 parent = self
00062                 #build hier list of elements
00063                 elements = list()
00064                 while(parent):
00065                         elements.insert(0, parent)
00066                         parent = parent.get_parent()
00067                 #build error string
00068                 err_str = ">>> Error:"
00069                 for i, element in enumerate(elements + [error]):
00070                         err_str = err_str + '\n' + ''.join('   '*(i+2)) + str(element)
00071                 err_str = err_str + '\n'
00072                 exit(err_str)
00073 
00074         ##############################################
00075         ## Update flagging
00076         ##############################################
00077         def is_flagged(self): return self._flag
00078         def flag(self):
00079                 self._flag = True
00080                 if self.get_parent(): self.get_parent().flag()
00081         def deflag(self):
00082                 self._flag = False
00083                 if self.get_parent(): self.get_parent().deflag()
00084 
00085         ##############################################
00086         ## Type testing methods
00087         ##############################################
00088         def is_element(self): return True
00089         def is_platform(self): return False
00090         def is_flow_graph(self): return False
00091         def is_connection(self): return False
00092         def is_block(self): return False
00093         def is_source(self): return False
00094         def is_sink(self): return False
00095         def is_port(self): return False
00096         def is_param(self): return False
00097 

Generated on Sat Aug 23 02:00:11 2008 for GNU Radio Companion by  doxygen 1.5.4