summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Koslowski <koslowski@kit.edu>2015-08-19 14:01:03 +0200
committerSebastian Koslowski <koslowski@kit.edu>2015-09-02 11:48:33 +0200
commitc3ed72b9d9fffb234ed266c243a6b3c3b49d42d2 (patch)
tree1582af3616595a5d63a387e3c87aae7d66b52730
parent656e80a7a25465f2df15533c046f406051bde71b (diff)
grc: refactor FlowGraph.py(s)
-rw-r--r--grc/base/FlowGraph.py45
-rw-r--r--grc/gui/FlowGraph.py21
-rw-r--r--grc/python/Generator.py2
3 files changed, 43 insertions, 25 deletions
diff --git a/grc/base/FlowGraph.py b/grc/base/FlowGraph.py
index 697ae8c47a..61e60f9968 100644
--- a/grc/base/FlowGraph.py
+++ b/grc/base/FlowGraph.py
@@ -18,10 +18,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
import time
-from . import odict
-from Element import Element
+from operator import methodcaller
+from itertools import ifilter
+
from .. gui import Messages
-from . Constants import FLOW_GRAPH_FILE_FORMAT_VERSION
+
+from . import odict
+from .Element import Element
+from .Constants import FLOW_GRAPH_FILE_FORMAT_VERSION
class FlowGraph(Element):
@@ -118,18 +122,33 @@ class FlowGraph(Element):
##############################################
## Access Elements
##############################################
- def get_block(self, id): return filter(lambda b: b.get_id() == id, self.get_blocks())[0]
- def get_blocks_unordered(self): return filter(lambda e: e.is_block(), self.get_elements())
+ def get_block(self, id):
+ for block in self.iter_blocks():
+ if block.get_id() == id:
+ return block
+ raise KeyError('No block with ID {0!r}'.format(id))
+
+ def iter_blocks(self):
+ return ifilter(methodcaller('is_block'), self.get_elements())
+
+ def get_blocks_unordered(self):
+ return list(self.iter_blocks())
+
def get_blocks(self):
# refactored the slow, ugly version
# don't know why we need this here, using it for sorted export_data()
- return sorted(self.get_blocks_unordered(), key=lambda b: (
+ return sorted(self.iter_blocks(), key=lambda b: (
b.get_key() != 'options', # options to the front
not b.get_key().startswith('variable'), # then vars
str(b)
))
- def get_connections(self): return filter(lambda e: e.is_connection(), self.get_elements())
- def get_children(self): return self.get_elements()
+
+ def iter_connections(self):
+ return ifilter(methodcaller('is_connection'), self.get_elements())
+
+ def get_connections(self):
+ return list(self.iter_connections())
+
def get_elements(self):
"""
Get a list of all the elements.
@@ -145,6 +164,8 @@ class FlowGraph(Element):
self._elements.remove(self._options_block)
return self._elements
+ get_children = get_elements
+
def get_enabled_blocks(self):
"""
Get a list of all blocks that are enabled and not bypassed.
@@ -152,7 +173,7 @@ class FlowGraph(Element):
Returns:
a list of blocks
"""
- return filter(lambda b: b.get_enabled(), self.get_blocks())
+ return filter(methodcaller('get_enabled'), self.iter_blocks())
def get_bypassed_blocks(self):
"""
@@ -161,7 +182,7 @@ class FlowGraph(Element):
Returns:
a list of blocks
"""
- return filter(lambda b: b.get_bypassed(), self.get_blocks())
+ return filter(methodcaller('get_bypassed'), self.iter_blocks())
def get_enabled_connections(self):
"""
@@ -170,7 +191,7 @@ class FlowGraph(Element):
Returns:
a list of connections
"""
- return filter(lambda c: c.get_enabled(), self.get_connections())
+ return filter(methodcaller('get_enabled'), self.get_connections())
def get_new_block(self, key):
"""
@@ -301,7 +322,7 @@ class FlowGraph(Element):
block.import_data(block_n)
#build the connections
- block_ids = map(lambda b: b.get_id(), self.get_blocks())
+ block_ids = map(methodcaller('get_id'), self.iter_blocks())
for connection_n in connections_n:
try: # to make the connection
#get the block ids
diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py
index bf6e1eed78..4a9fb0b9d8 100644
--- a/grc/gui/FlowGraph.py
+++ b/grc/gui/FlowGraph.py
@@ -17,17 +17,14 @@ along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
-from Constants import SCROLL_PROXIMITY_SENSITIVITY, SCROLL_DISTANCE
-import Actions
-import Colors
-import Utils
-from Element import Element
-import pygtk
-pygtk.require('2.0')
-import gtk
import random
-import Messages
-import Bars
+from itertools import chain
+from operator import methodcaller
+
+from . import Actions, Colors, Utils, Messages, Bars
+from .Constants import SCROLL_PROXIMITY_SENSITIVITY, SCROLL_DISTANCE
+from .Element import Element
+
class FlowGraph(Element):
"""
@@ -294,7 +291,7 @@ class FlowGraph(Element):
window.draw_rectangle(gc, True, 0, 0, W, H)
# draw comments first
if Actions.TOGGLE_SHOW_BLOCK_COMMENTS.get_active():
- for block in self.get_blocks():
+ for block in self.iter_blocks():
if block.get_enabled():
block.draw_comment(gc, window)
#draw multi select rectangle
@@ -312,7 +309,7 @@ class FlowGraph(Element):
window.draw_rectangle(gc, False, x, y, w, h)
#draw blocks on top of connections
hide_disabled_blocks = Actions.TOGGLE_HIDE_DISABLED_BLOCKS.get_active()
- for element in self.get_connections() + self.get_blocks():
+ for element in chain(self.iter_connections(), self.iter_blocks()):
if hide_disabled_blocks and not element.get_enabled():
continue # skip hidden disabled blocks and connections
element.draw(gc, window)
diff --git a/grc/python/Generator.py b/grc/python/Generator.py
index 98fbd0c360..3c687a2d64 100644
--- a/grc/python/Generator.py
+++ b/grc/python/Generator.py
@@ -173,7 +173,7 @@ class TopBlockGenerator(object):
return code
blocks = expr_utils.sort_objects(
- filter(lambda b: b.get_enabled() and not b.get_bypassed(), self._flow_graph.get_blocks()),
+ filter(lambda b: b.get_enabled() and not b.get_bypassed(), self._flow_graph.iter_blocks()),
lambda b: b.get_id(), _get_block_sort_text
)
# List of regular blocks (all blocks minus the special ones)