From 6647de1109cd0dffbb8668797a5bd98f54ffa053 Mon Sep 17 00:00:00 2001
From: Sebastian Koslowski <koslowski@kit.edu>
Date: Tue, 17 May 2016 13:23:18 +0200
Subject: grc-refactor: minor clean-up of callback generator code

---
 grc/core/generator/Generator.py | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

(limited to 'grc/core/generator/Generator.py')

diff --git a/grc/core/generator/Generator.py b/grc/core/generator/Generator.py
index 4788711a17..ba272aba3f 100644
--- a/grc/core/generator/Generator.py
+++ b/grc/core/generator/Generator.py
@@ -141,7 +141,7 @@ class TopBlockGenerator(object):
                 pass
             return code
 
-        blocks = expr_utils.sort_objects(
+        blocks_all = expr_utils.sort_objects(
             filter(lambda b: b.get_enabled() and not b.get_bypassed(), fg.blocks),
             lambda b: b.get_id(), _get_block_sort_text
         )
@@ -150,7 +150,7 @@ class TopBlockGenerator(object):
             Messages.send_warning("The block {!r} is deprecated.".format(key))
 
         # List of regular blocks (all blocks minus the special ones)
-        blocks = filter(lambda b: b not in (imports + parameters), blocks)
+        blocks = filter(lambda b: b not in (imports + parameters), blocks_all)
 
         for block in blocks:
             key = block.get_key()
@@ -214,20 +214,22 @@ class TopBlockGenerator(object):
 
         connection_templates = fg.get_parent().connection_templates
         msgs = filter(lambda c: c.is_msg(), fg.get_enabled_connections())
+
         # List of variable names
         var_ids = [var.get_id() for var in parameters + variables]
-        # Prepend self.
-        replace_dict = dict([(var_id, 'self.%s' % var_id) for var_id in var_ids])
-        # List of callbacks
-        callbacks = [
-            expr_utils.expr_replace(cb, replace_dict)
-            for cb in sum([block.get_callbacks() for block in fg.get_enabled_blocks()], [])
-            ]
+        replace_dict = dict((var_id, 'self.' + var_id) for var_id in var_ids)
+        callbacks_all = []
+        for block in blocks_all:
+            callbacks_all.extend(expr_utils.expr_replace(cb, replace_dict) for cb in block.get_callbacks())
+
         # Map var id to callbacks
-        var_id2cbs = dict([
-            (var_id, filter(lambda c: expr_utils.get_variable_dependencies(c, [var_id]), callbacks))
-            for var_id in var_ids
-        ])
+        def uses_var_id():
+            return expr_utils.get_variable_dependencies(callback, [var_id])
+
+        callbacks = {}
+        for var_id in var_ids:
+            callbacks[var_id] = [callback for callback in callbacks_all if uses_var_id()]
+
         # Load the namespace
         namespace = {
             'title': title,
@@ -241,7 +243,7 @@ class TopBlockGenerator(object):
             'connection_templates': connection_templates,
             'msgs': msgs,
             'generate_options': self._generate_options,
-            'var_id2cbs': var_id2cbs,
+            'callbacks': callbacks,
         }
         # Build the template
         t = Template(open(FLOW_GRAPH_TEMPLATE, 'r').read(), namespace)
-- 
cgit v1.2.3


From 1319af2b6cd4631a28a969a4cb54f2f25b8ab8d5 Mon Sep 17 00:00:00 2001
From: Sebastian Koslowski <koslowski@kit.edu>
Date: Tue, 17 May 2016 13:24:54 +0200
Subject: grc: faulty callback code if setter call contained a variable block
 id

(do-over of c85984f105106ff0a7e3b387d680e0f2f5884d55)

If a block contains a callback of the form

    self.block_id.param = $param

the generators subst routine produced

    self.block_id.self.param = self.param

due to a faulty splitting of the expression in expr_utils.py.

This should fix this problem by not tokenizing "VAR0.VAR1".
---
 grc/core/generator/Generator.py | 5 +++--
 grc/core/utils/expr_utils.py    | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)

(limited to 'grc/core/generator/Generator.py')

diff --git a/grc/core/generator/Generator.py b/grc/core/generator/Generator.py
index ba272aba3f..fb7a3afb99 100644
--- a/grc/core/generator/Generator.py
+++ b/grc/core/generator/Generator.py
@@ -145,7 +145,7 @@ class TopBlockGenerator(object):
             filter(lambda b: b.get_enabled() and not b.get_bypassed(), fg.blocks),
             lambda b: b.get_id(), _get_block_sort_text
         )
-        deprecated_block_keys = set(block.get_name() for block in blocks if block.is_deprecated)
+        deprecated_block_keys = set(block.get_name() for block in blocks_all if block.is_deprecated)
         for key in deprecated_block_keys:
             Messages.send_warning("The block {!r} is deprecated.".format(key))
 
@@ -224,7 +224,8 @@ class TopBlockGenerator(object):
 
         # Map var id to callbacks
         def uses_var_id():
-            return expr_utils.get_variable_dependencies(callback, [var_id])
+            used = expr_utils.get_variable_dependencies(callback, [var_id])
+            return used and 'self.' + var_id in callback  # callback might contain var_id itself
 
         callbacks = {}
         for var_id in var_ids:
diff --git a/grc/core/utils/expr_utils.py b/grc/core/utils/expr_utils.py
index 240b99e7a1..2059ceff9f 100644
--- a/grc/core/utils/expr_utils.py
+++ b/grc/core/utils/expr_utils.py
@@ -99,7 +99,7 @@ def expr_replace(expr, replace_dict):
     Returns:
         a new expression with the prepend
     """
-    expr_splits = expr_split(expr)
+    expr_splits = expr_split(expr, var_chars=VAR_CHARS + '.')
     for i, es in enumerate(expr_splits):
         if es in replace_dict.keys():
             expr_splits[i] = replace_dict[es]
-- 
cgit v1.2.3