summaryrefslogtreecommitdiff
path: root/grc/base
diff options
context:
space:
mode:
Diffstat (limited to 'grc/base')
-rw-r--r--grc/base/Block.py47
-rw-r--r--grc/base/Element.py49
-rw-r--r--grc/base/FlowGraph.py22
3 files changed, 63 insertions, 55 deletions
diff --git a/grc/base/Block.py b/grc/base/Block.py
index fc501205f..b2015cc40 100644
--- a/grc/base/Block.py
+++ b/grc/base/Block.py
@@ -132,28 +132,6 @@ class Block(Element):
"""
self.get_param('_enabled').set_value(str(enabled))
- def rewrite(self):
- """
- Rewrite critical structures.
- Call rewrite on all sub elements.
- """
- Element.rewrite(self)
- for elem in self.get_ports() + self.get_params(): elem.rewrite()
-
- def validate(self):
- """
- Validate the block.
- All ports and params must be valid.
- All checks must evaluate to true.
- Validate the params, ports, and the connections to this block.
- """
- Element.validate(self)
- for c in self.get_params() + self.get_ports() + self.get_connections():
- c.validate()
- if not c.is_valid():
- for msg in c.get_error_messages():
- self.add_error_message('>>> %s:\n\t%s'%(c, msg))
-
def __str__(self): return 'Block - %s - %s(%s)'%(self.get_id(), self.get_name(), self.get_key())
def get_id(self): return self.get_param('id').get_value()
@@ -163,6 +141,7 @@ class Block(Element):
def get_category(self): return self._category
def get_doc(self): return ''
def get_ports(self): return self.get_sources() + self.get_sinks()
+ def get_children(self): return self.get_ports() + self.get_params()
def get_block_wrapper_path(self): return self._block_wrapper_path
##############################################
@@ -254,12 +233,22 @@ class Block(Element):
"""
Import this block's params from nested data.
Any param keys that do not exist will be ignored.
+ Since params can be dynamically created based another param,
+ call rewrite, and repeat the load until the params stick.
+ This call to rewrite will also create any dynamic ports
+ that are needed for the connections creation phase.
@param n the nested data odict
"""
- params_n = n.findall('param')
- for param_n in params_n:
- key = param_n.find('key')
- value = param_n.find('value')
- #the key must exist in this block's params
- if key in self.get_param_keys():
- self.get_param(key).set_value(value)
+ get_hash = lambda: hash(tuple(map(hash, self.get_params())))
+ my_hash = 0
+ while get_hash() != my_hash:
+ params_n = n.findall('param')
+ for param_n in params_n:
+ key = param_n.find('key')
+ value = param_n.find('value')
+ #the key must exist in this block's params
+ if key in self.get_param_keys():
+ self.get_param(key).set_value(value)
+ #store hash and call rewrite
+ my_hash = get_hash()
+ self.rewrite()
diff --git a/grc/base/Element.py b/grc/base/Element.py
index 43cee886c..a57090f3b 100644
--- a/grc/base/Element.py
+++ b/grc/base/Element.py
@@ -25,16 +25,55 @@ class Element(object):
##################################################
# 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 rewrite(self): pass
+ 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 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
+ ##############################################
+ ## Tree-like API
+ ##############################################
def get_parent(self): return self._parent
+ def get_children(self): return list()
##############################################
## Type testing methods
diff --git a/grc/base/FlowGraph.py b/grc/base/FlowGraph.py
index b24f13b09..7c51ef42a 100644
--- a/grc/base/FlowGraph.py
+++ b/grc/base/FlowGraph.py
@@ -68,6 +68,7 @@ class FlowGraph(Element):
def get_block(self, id): return filter(lambda b: b.get_id() == id, self.get_blocks())[0]
def get_blocks(self): return filter(lambda e: e.is_block(), self.get_elements())
def get_connections(self): return filter(lambda e: e.is_connection(), self.get_elements())
+ def get_children(self): return self.get_elements()
def get_elements(self):
"""
Get a list of all the elements.
@@ -144,26 +145,6 @@ class FlowGraph(Element):
"""
raise NotImplementedError
- def rewrite(self):
- """
- Rewrite critical structures.
- Call rewrite on all sub elements.
- """
- Element.rewrite(self)
- for elem in self.get_elements(): elem.rewrite()
-
- def validate(self):
- """
- Validate the flow graph.
- Validate only the blocks.
- Connections will be validated within the blocks.
- """
- Element.validate(self)
- for c in self.get_blocks():
- c.validate()
- if not c.is_valid():
- self.add_error_message('Element "%s" is not valid.'%c)
-
##############################################
## Import/Export Methods
##############################################
@@ -203,7 +184,6 @@ class FlowGraph(Element):
#only load the block when the block key was valid
if block: block.import_data(block_n)
else: Messages.send_error_load('Block key "%s" not found in %s'%(key, self.get_parent()))
- self.rewrite() #rewrite all blocks before connections are made (ex: nports)
#build the connections
for connection_n in connections_n:
#try to make the connection