commit-gnuradio
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Commit-gnuradio] [gnuradio] 54/101: grc: refactor: replace get_enabled


From: git
Subject: [Commit-gnuradio] [gnuradio] 54/101: grc: refactor: replace get_enabled by prop
Date: Thu, 16 Mar 2017 14:58:06 +0000 (UTC)

This is an automated email from the git hooks/post-receive script.

jcorgan pushed a commit to branch python3
in repository gnuradio.

commit 6b99b6fded94ae1ed8421c624246362e7925fb08
Author: Sebastian Koslowski <address@hidden>
Date:   Thu Jul 21 11:30:42 2016 +0200

    grc: refactor: replace get_enabled by prop
---
 grc/core/Block.py               | 28 +++++-----------------------
 grc/core/Connection.py          | 35 ++++++++++++++++++-----------------
 grc/core/Element.py             |  9 +++++----
 grc/core/Element.pyi            | 25 ++++++++++---------------
 grc/core/FlowGraph.py           | 12 +++++-------
 grc/core/Port.py                |  2 +-
 grc/core/generator/Generator.py |  4 ++--
 grc/core/utils/_complexity.py   |  2 +-
 grc/gui/Block.py                |  4 ++--
 grc/gui/Connection.py           |  2 +-
 grc/gui/FlowGraph.py            |  6 +++---
 grc/gui/VariableEditor.py       | 12 ++++++------
 12 files changed, 59 insertions(+), 82 deletions(-)

diff --git a/grc/core/Block.py b/grc/core/Block.py
index 7042ba7..9572982 100644
--- a/grc/core/Block.py
+++ b/grc/core/Block.py
@@ -332,30 +332,11 @@ class Block(Element):
         self.states['_enabled'] = encoded
 
     # Enable/Disable Aliases
-    def get_enabled(self):
-        """
-        Get the enabled state of the block.
-
-        Returns:
-            true for enabled
-        """
+    @property
+    def enabled(self):
+        """Get the enabled state of the block"""
         return self.state != 'disabled'
 
-    def set_enabled(self, enabled):
-        """
-        Set the enabled state of the block.
-
-        Args:
-            enabled: true for enabled
-
-        Returns:
-            True if block changed state
-        """
-        old_state = self.state
-        new_state = 'enabled' if enabled else 'disabled'
-        self.state = new_state
-        return old_state != new_state
-
     # Block bypassing
     def get_bypassed(self):
         """
@@ -739,7 +720,8 @@ class DummyBlock(Block):
     def is_valid(self):
         return False
 
-    def get_enabled(self):
+    @property
+    def enabled(self):
         return False
 
     def add_missing_port(self, key, dir):
diff --git a/grc/core/Connection.py b/grc/core/Connection.py
index aec7a21..6be1ccb 100644
--- a/grc/core/Connection.py
+++ b/grc/core/Connection.py
@@ -76,6 +76,24 @@ class Connection(Element):
             raise ValueError('Connection could not isolate sink')
         return source, sink
 
+    @lazy_property
+    def source_block(self):
+        return self.source_port.parent_block
+
+    @lazy_property
+    def sink_block(self):
+        return self.sink_port.parent_block
+
+    @property
+    def enabled(self):
+        """
+        Get the enabled state of this connection.
+
+        Returns:
+            true if source and sink blocks are enabled
+        """
+        return self.source_block.enabled and self.sink_block.enabled
+
     def __str__(self):
         return 'Connection (\n\t{}\n\t\t{}\n\t{}\n\t\t{}\n)'.format(
             self.source_block, self.source_port, self.sink_block, 
self.sink_port,
@@ -125,23 +143,6 @@ class Connection(Element):
         if source_size != sink_size:
             self.add_error_message('Source IO size "{}" does not match sink IO 
size "{}".'.format(source_size, sink_size))
 
-    def get_enabled(self):
-        """
-        Get the enabled state of this connection.
-
-        Returns:
-            true if source and sink blocks are enabled
-        """
-        return self.source_block.get_enabled() and 
self.sink_block.get_enabled()
-
-    @lazy_property
-    def source_block(self):
-        return self.source_port.parent_block
-
-    @lazy_property
-    def sink_block(self):
-        return self.sink_port.parent_block
-
     ##############################################
     # Import/Export Methods
     ##############################################
diff --git a/grc/core/Element.py b/grc/core/Element.py
index 415b086..32afabb 100644
--- a/grc/core/Element.py
+++ b/grc/core/Element.py
@@ -66,7 +66,7 @@ class Element(object):
         Returns:
             true when the element is enabled and has no error messages or is 
bypassed
         """
-        return (not self.get_error_messages() or not self.get_enabled()) or 
self.get_bypassed()
+        return (not self.get_error_messages() or not self.enabled) or 
self.get_bypassed()
 
     def add_error_message(self, msg):
         """
@@ -88,7 +88,7 @@ class Element(object):
         """
         error_messages = list(self._error_messages)  # Make a copy
         for child in self.get_children():
-            if not child.get_enabled() or child.get_bypassed():
+            if not child.enabled or child.get_bypassed():
                 continue
             for msg in child.get_error_messages():
                 error_messages.append("{}:\n\t{}".format(child, 
msg.replace("\n", "\n\t")))
@@ -102,7 +102,8 @@ class Element(object):
         for child in self.get_children():
             child.rewrite()
 
-    def get_enabled(self):
+    @property
+    def enabled(self):
         return True
 
     def get_bypassed(self):
@@ -141,7 +142,7 @@ class Element(object):
 
     def reset_parents_by_type(self):
         """Reset all lazy properties"""
-        for name, obj in vars(Element):
+        for name, obj in vars(Element):  # explicitly only in Element, not 
subclasses
             if isinstance(obj, lazy_property):
                 delattr(self, name)
 
diff --git a/grc/core/Element.pyi b/grc/core/Element.pyi
index 46c6d34..2a2aed4 100644
--- a/grc/core/Element.pyi
+++ b/grc/core/Element.pyi
@@ -15,32 +15,27 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
USA
 
+from typing import Union
+
 from . import Platform, FlowGraph, Block
 
-def lazy_property(func):
-    return func
+lazy_property = property  # fixme: descriptors don't seems to be supported
 
 
 class Element(object):
 
-    def __init__(self, parent=None):
-        ...
+    def __init__(self, parent: Union[None, 'Element'] = None): ...
 
-    @property
-    def parent(self): -> 'Element'
-        ...
+    @lazy_property
+    def parent(self) -> 'Element': ...
 
-    def get_parent_by_type(self, cls): -> 'Element'
-        ...
+    def get_parent_by_type(self, cls) -> Union[None, 'Element']: ...
 
     @lazy_property
-    def parent_platform(self): -> Platform.Platform
-        ...
+    def parent_platform(self) -> Platform.Platform: ...
 
     @lazy_property
-    def parent_flowgraph(self): -> FlowGraph.FlowGraph
-        ...
+    def parent_flowgraph(self) -> FlowGraph.FlowGraph: ...
 
     @lazy_property
-    def parent_block(self): -> Block.Block
-        ...
+    def parent_block(self) -> Block.Block: ...
diff --git a/grc/core/FlowGraph.py b/grc/core/FlowGraph.py
index cb2a56c..97a4c37 100644
--- a/grc/core/FlowGraph.py
+++ b/grc/core/FlowGraph.py
@@ -21,11 +21,9 @@ import imp
 import time
 import re
 from itertools import chain
-from operator import methodcaller, attrgetter
+from operator import methodcaller
 import collections
 
-from six.moves import filter
-
 from . import Messages
 from .Constants import FLOW_GRAPH_FILE_FORMAT_VERSION
 from .Element import Element
@@ -86,7 +84,7 @@ class FlowGraph(Element):
         Returns:
             a sorted list of variable blocks in order of dependency (indep -> 
dep)
         """
-        variables = list(filter(attrgetter('is_variable'), 
self.iter_enabled_blocks()))
+        variables = [block for block in self.iter_enabled_blocks() if 
block.is_variable]
         return expr_utils.sort_objects(variables, methodcaller('get_id'), 
methodcaller('get_var_make'))
 
     def get_parameters(self):
@@ -142,7 +140,7 @@ class FlowGraph(Element):
         """
         Get an iterator of all blocks that are enabled and not bypassed.
         """
-        return filter(methodcaller('get_enabled'), self.blocks)
+        return (block for block in self.blocks if block.enabled)
 
     def get_enabled_blocks(self):
         """
@@ -160,7 +158,7 @@ class FlowGraph(Element):
         Returns:
             a list of blocks
         """
-        return list(filter(methodcaller('get_bypassed'), self.blocks))
+        return [block for block in self.blocks if block.get_bypassed()]
 
     def get_enabled_connections(self):
         """
@@ -169,7 +167,7 @@ class FlowGraph(Element):
         Returns:
             a list of connections
         """
-        return list(filter(methodcaller('get_enabled'), self.connections))
+        return [connection for connection in self.connections if 
connection.enabled]
 
     def get_option(self, key):
         """
diff --git a/grc/core/Port.py b/grc/core/Port.py
index f7046ad..0d9298f 100644
--- a/grc/core/Port.py
+++ b/grc/core/Port.py
@@ -332,7 +332,7 @@ class Port(Element):
         Returns:
             a list of connection objects
         """
-        return [c for c in self.get_connections() if c.get_enabled()]
+        return [c for c in self.get_connections() if c.enabled]
 
     def get_associated_ports(self):
         if not self.get_type() == 'bus':
diff --git a/grc/core/generator/Generator.py b/grc/core/generator/Generator.py
index dcbf767..7c9a1ec 100644
--- a/grc/core/generator/Generator.py
+++ b/grc/core/generator/Generator.py
@@ -145,7 +145,7 @@ class TopBlockGenerator(object):
             return code
 
         blocks_all = expr_utils.sort_objects(
-            [b for b in fg.blocks if b.get_enabled() and not b.get_bypassed()],
+            [b for b in fg.blocks if b.enabled and not b.get_bypassed()],
             operator.methodcaller('get_id'), _get_block_sort_text
         )
         deprecated_block_keys = set(b.name for b in blocks_all if 
b.is_deprecated)
@@ -198,7 +198,7 @@ class TopBlockGenerator(object):
 
             # Loop through all the downstream connections
             for sink in (c for c in connections if c.source_port == 
block.sources[0]):
-                if not sink.get_enabled():
+                if not sink.enabled:
                     # Ignore disabled connections
                     continue
                 sink_port = sink.sink_port
diff --git a/grc/core/utils/_complexity.py b/grc/core/utils/_complexity.py
index 6da16eb..c0f3ae9 100644
--- a/grc/core/utils/_complexity.py
+++ b/grc/core/utils/_complexity.py
@@ -29,7 +29,7 @@ def calculate_flowgraph_complexity(flowgraph):
     blocks = float(len(flowgraph.blocks))
     connections = float(len(flowgraph.connections))
     elements = blocks + connections
-    disabled_connections = sum(not c.get_enabled() for c in 
flowgraph.connections)
+    disabled_connections = sum(not c.enabled for c in flowgraph.connections)
 
     variables = elements - blocks - connections
     enabled = float(len(flowgraph.get_enabled_blocks()))
diff --git a/grc/gui/Block.py b/grc/gui/Block.py
index e7dc033..db14773 100644
--- a/grc/gui/Block.py
+++ b/grc/gui/Block.py
@@ -137,7 +137,7 @@ class Block(CoreBlock, Element):
         self._bg_color = (
             Colors.MISSING_BLOCK_BACKGROUND_COLOR if self.is_dummy_block else
             Colors.BLOCK_BYPASSED_COLOR if self.get_bypassed() else
-            Colors.BLOCK_ENABLED_COLOR if self.get_enabled() else
+            Colors.BLOCK_ENABLED_COLOR if self.enabled else
             Colors.BLOCK_DISABLED_COLOR
         )
 
@@ -234,7 +234,7 @@ class Block(CoreBlock, Element):
                 markups.append('<span></span>')
 
             markups.append('<span foreground="{foreground}" 
font_desc="{font}">{comment}</span>'.format(
-                foreground='#444' if self.get_enabled() else '#888', 
font=BLOCK_FONT, comment=Utils.encode(comment)
+                foreground='#444' if self.enabled else '#888', 
font=BLOCK_FONT, comment=Utils.encode(comment)
             ))
         if markups:
             layout = self._comment_layout = 
Gtk.DrawingArea().create_pango_layout('')
diff --git a/grc/gui/Connection.py b/grc/gui/Connection.py
index 87dd97a..b6e84f8 100644
--- a/grc/gui/Connection.py
+++ b/grc/gui/Connection.py
@@ -171,7 +171,7 @@ class Connection(Element, _Connection):
         # draw
         color1, color2 = (
             Colors.HIGHLIGHT_COLOR if self.highlighted else
-            Colors.CONNECTION_DISABLED_COLOR if not self.get_enabled() else
+            Colors.CONNECTION_DISABLED_COLOR if not self.enabled else
             color for color in (self._color, self._color2))
 
         Element.draw(self, widget, cr, color1, 
Colors.FLOWGRAPH_BACKGROUND_COLOR)
diff --git a/grc/gui/FlowGraph.py b/grc/gui/FlowGraph.py
index da88636..a3dd379 100644
--- a/grc/gui/FlowGraph.py
+++ b/grc/gui/FlowGraph.py
@@ -456,13 +456,13 @@ class FlowGraph(Element, _Flowgraph):
         hide_variables = Actions.TOGGLE_HIDE_VARIABLES.get_active()
 
         def draw_order(elem):
-            return elem.highlighted, elem.is_block, elem.get_enabled()
+            return elem.highlighted, elem.is_block, elem.enabled
 
         elements = sorted(self.get_elements(), key=draw_order)
         del self._elements_to_draw[:]
 
         for element in elements:
-            if hide_disabled_blocks and not element.get_enabled():
+            if hide_disabled_blocks and not element.enabled:
                 continue  # skip hidden disabled blocks and connections
             if hide_variables and (element.is_variable or element.is_import):
                 continue  # skip hidden disabled blocks and connections
@@ -471,7 +471,7 @@ class FlowGraph(Element, _Flowgraph):
     def _drawables(self):
         show_comments = Actions.TOGGLE_SHOW_BLOCK_COMMENTS.get_active()
         for element in self._elements_to_draw:
-            if element.is_block and show_comments and element.get_enabled():
+            if element.is_block and show_comments and element.enabled:
                 yield element.draw_comment
         for element in self._elements_to_draw:
             yield element.draw
diff --git a/grc/gui/VariableEditor.py b/grc/gui/VariableEditor.py
index 11284f5..09fe629 100644
--- a/grc/gui/VariableEditor.py
+++ b/grc/gui/VariableEditor.py
@@ -211,7 +211,7 @@ class VariableEditor(Gtk.VBox):
 
         # Block specific changes
         if block:
-            if not block.get_enabled():
+            if not block.enabled:
                 # Disabled block. But, this should still be editable
                 sp('editable', True)
                 sp('foreground', 'gray')
@@ -274,9 +274,9 @@ class VariableEditor(Gtk.VBox):
             else:
                 self.handle_action(None, self.DELETE_BLOCK, None)
         elif key == self.ENABLE_BLOCK:
-            self._block.set_enabled(True)
+            self._block.state = 'enabled'
         elif key == self.DISABLE_BLOCK:
-            self._block.set_enabled(False)
+            self._block.state = 'disabled'
         Actions.VARIABLE_EDITOR_UPDATE()
 
     def _handle_mouse_button_press(self, widget, event):
@@ -318,7 +318,7 @@ class VariableEditor(Gtk.VBox):
                         return True
             elif event.button == 3 and event.type == 
Gdk.EventType.BUTTON_PRESS:
                 if self._block:
-                    self._context_menu.update_sensitive(True, 
enabled=self._block.get_enabled())
+                    self._context_menu.update_sensitive(True, 
enabled=self._block.enabled)
                 else:
                     self._context_menu.update_sensitive(False)
                 self._context_menu.popup(None, None, None, None, event.button, 
event.time)
@@ -341,10 +341,10 @@ class VariableEditor(Gtk.VBox):
     def _handle_key_button_press(self, widget, event):
         model, path = self.treeview.get_selection().get_selected_rows()
         if path and self._block:
-            if self._block.get_enabled() and event.string == "d":
+            if self._block.enabled and event.string == "d":
                 self.handle_action(None, self.DISABLE_BLOCK, None)
                 return True
-            elif not self._block.get_enabled() and event.string == "e":
+            elif not self._block.enabled and event.string == "e":
                 self.handle_action(None, self.ENABLE_BLOCK, None)
                 return True
         return False



reply via email to

[Prev in Thread] Current Thread [Next in Thread]