Merge commit 'v3.3.0' into upstream
[debian/gnuradio] / grc / base / Element.py
index 16000c46c0a87068491da35f6228c58577c3be37..a57090f3b24a2f5928d3f7c6facb1bfee48b7969 100644 (file)
@@ -1,5 +1,5 @@
 """
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
 This file is part of GNU Radio
 
 GNU Radio Companion is free software; you can redistribute it and/or
@@ -21,37 +21,59 @@ class Element(object):
 
        def __init__(self, parent=None):
                self._parent = parent
-               self.flag()
-
-       def test(self):
-               """
-               Test the element against failures.
-               Overload this method in sub-classes.
-               """
-               pass
 
        ##################################################
        # Element Validation API
        ##################################################
-       def validate(self): self._error_messages = list()
-       def is_valid(self): return not self.get_error_messages() or not self.get_enabled()
-       def add_error_message(self, msg): self._error_messages.append(msg)
-       def get_error_messages(self): return self._error_messages
+       def validate(self):
+               """
+               Validate this element and call validate on all children.
+               Call this base method before adding error messages in the subclass.
+               """
+               self._error_messages = list()
+               for child in self.get_children(): child.validate()
 
-       def get_enabled(self): return True
+       def is_valid(self):
+               """
+               Is this element valid?
+               @return true when the element is enabled and has no error messages
+               """
+               return not self.get_error_messages() or not self.get_enabled()
 
-       def get_parent(self): return self._parent
+       def add_error_message(self, msg):
+               """
+               Add an error message to the list of errors.
+               @param msg the error message string
+               """
+               self._error_messages.append(msg)
+
+       def get_error_messages(self):
+               """
+               Get the list of error messages from this element and all of its children.
+               Do not include the error messages from disabled children.
+               Cleverly indent the children error messages for printing purposes.
+               @return a list of error message strings
+               """
+               error_messages = list(self._error_messages) #make a copy
+               for child in filter(lambda c: c.get_enabled(), self.get_children()):
+                       for msg in child.get_error_messages():
+                               error_messages.append("%s:\n\t%s"%(child, msg.replace("\n", "\n\t")))
+               return error_messages
+
+       def rewrite(self):
+               """
+               Rewrite this element and call rewrite on all children.
+               Call this base method before rewriting the element.
+               """
+               for child in self.get_children(): child.rewrite()
+
+       def get_enabled(self): return True
 
        ##############################################
-       ## Update flagging
+       ## Tree-like API
        ##############################################
-       def is_flagged(self): return self._flag
-       def flag(self):
-               self._flag = True
-               if self.get_parent(): self.get_parent().flag()
-       def deflag(self):
-               self._flag = False
-               if self.get_parent(): self.get_parent().deflag()
+       def get_parent(self): return self._parent
+       def get_children(self): return list()
 
        ##############################################
        ## Type testing methods