commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] [gnuradio] 53/101: grc: make dummy blocks a block subc


From: git
Subject: [Commit-gnuradio] [gnuradio] 53/101: grc: make dummy blocks a block subclass
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 980ab9d2b0455cd0a39730617435774df4af658b
Author: Sebastian Koslowski <address@hidden>
Date:   Thu Jul 21 10:46:00 2016 +0200

    grc: make dummy blocks a block subclass
---
 grc/blocks/dummy.xml  | 11 ----------
 grc/core/Block.py     | 61 +++++++++++++++++++++++++++++++++++++--------------
 grc/core/FlowGraph.py | 40 +++++----------------------------
 grc/core/Platform.py  |  7 +++---
 grc/core/__init__.py  |  3 ---
 5 files changed, 54 insertions(+), 68 deletions(-)

diff --git a/grc/blocks/dummy.xml b/grc/blocks/dummy.xml
deleted file mode 100644
index c0ca3b6..0000000
--- a/grc/blocks/dummy.xml
+++ /dev/null
@@ -1,11 +0,0 @@
-<?xml version="1.0"?>
-<!--
-###################################################
-##Dummy Block
-###################################################
--->
-<block>
-    <name>Missing Block</name>
-    <key>dummy_block</key>
-    <make>raise NotImplementedError()</make>
-</block>
diff --git a/grc/core/Block.py b/grc/core/Block.py
index 8999026..7042ba7 100644
--- a/grc/core/Block.py
+++ b/grc/core/Block.py
@@ -77,6 +77,19 @@ class Block(Element):
         self._grc_source = n.get('grc_source', '')
         self.block_wrapper_path = n.get('block_wrapper_path')
 
+        # Virtual source/sink and pad source/sink blocks are
+        # indistinguishable from normal GR blocks. Make explicit
+        # checks for them here since they have no work function or
+        # buffers to manage.
+        self.is_virtual_or_pad = self.key in (
+            "virtual_source", "virtual_sink", "pad_source", "pad_sink")
+        self.is_variable = self.key.startswith('variable')
+        self.is_import = (self.key == 'import')
+
+        # Disable blocks that are virtual/pads or variables
+        if self.is_virtual_or_pad or self.is_variable:
+            self.flags += BLOCK_FLAG_DISABLE_BYPASS
+
         params_n = n.get('param', [])
         sources_n = n.get('source', [])
         sinks_n = n.get('sink', [])
@@ -106,28 +119,15 @@ class Block(Element):
 
         add_param(key='id', name='ID', type='id')
 
-        # Virtual source/sink and pad source/sink blocks are
-        # indistinguishable from normal GR blocks. Make explicit
-        # checks for them here since they have no work function or
-        # buffers to manage.
-        self.is_virtual_or_pad = is_virtual_or_pad = self.key in (
-            "virtual_source", "virtual_sink", "pad_source", "pad_sink")
-        self.is_variable = is_variable = self.key.startswith('variable')
-        self.is_import = (self.key == 'import')
-
-        # Disable blocks that are virtual/pads or variables
-        if self.is_virtual_or_pad or self.is_variable:
-            self.flags += BLOCK_FLAG_DISABLE_BYPASS
-
-        if not (is_virtual_or_pad or is_variable or self.key == 'options'):
+        if not (self.is_virtual_or_pad or self.is_variable or self.key == 
'options'):
             add_param(key='alias', name='Block Alias', type='string',
                       hide='part', tab=ADVANCED_PARAM_TAB)
 
-        if not is_virtual_or_pad and (has_sources or has_sinks):
+        if not self.is_virtual_or_pad and (has_sources or has_sinks):
             add_param(key='affinity', name='Core Affinity', type='int_vector',
                       hide='part', tab=ADVANCED_PARAM_TAB)
 
-        if not is_virtual_or_pad and has_sources:
+        if not self.is_virtual_or_pad and has_sources:
             add_param(key='minoutbuf', name='Min Output Buffer', type='int',
                       hide='part', value='0', tab=ADVANCED_PARAM_TAB)
             add_param(key='maxoutbuf', name='Max Output Buffer', type='int',
@@ -722,3 +722,32 @@ class EPyBlock(Block):
         super(EPyBlock, self).validate()
         if self._epy_reload_error:
             
self.params['_source_code'].add_error_message(str(self._epy_reload_error))
+
+
+class DummyBlock(Block):
+
+    is_dummy_block = True
+    build_in_param_keys = 'id alias affinity minoutbuf maxoutbuf comment'
+
+    def __init__(self, parent, key, missing_key, params_n):
+        params = [{'key': p['key'], 'name': p['key'], 'type': 'string'}
+                  for p in params_n if p['key'] not in 
self.build_in_param_keys]
+        super(DummyBlock, self).__init__(
+            parent=parent, key=missing_key, name='Missing Block', param=params,
+        )
+
+    def is_valid(self):
+        return False
+
+    def get_enabled(self):
+        return False
+
+    def add_missing_port(self, key, dir):
+        port = self.parent_platform.get_new_port(
+            parent=self, direction=dir, key=key, name='?', type='',
+        )
+        if port.is_source:
+            self.sources.append(port)
+        else:
+            self.sinks.append(port)
+        return port
diff --git a/grc/core/FlowGraph.py b/grc/core/FlowGraph.py
index 08f24b8..cb2a56c 100644
--- a/grc/core/FlowGraph.py
+++ b/grc/core/FlowGraph.py
@@ -267,7 +267,7 @@ class FlowGraph(Element):
     # Add/remove stuff
     ##############################################
 
-    def new_block(self, key):
+    def new_block(self, key, **kwargs):
         """
         Get a new block of the specified key.
         Add the block to the list of elements.
@@ -281,7 +281,7 @@ class FlowGraph(Element):
         if key == 'options':
             return self._options_block
         try:
-            block = self.parent_platform.get_new_block(self, key)
+            block = self.parent_platform.get_new_block(self, key, **kwargs)
             self.blocks.append(block)
         except KeyError:
             block = None
@@ -403,9 +403,8 @@ class FlowGraph(Element):
 
             if not block:  # looks like this block key cannot be found
                 # create a dummy block instead
-                block = self.new_block('dummy_block')
-                # Ugly ugly ugly
-                _initialize_dummy_block(block, block_n)
+                block = self.new_block('_dummy', missing_key=key,
+                                       params_n=block_n.get('param', []))
                 print('Block key "%s" not found' % key)
 
             block.import_data(block_n)
@@ -422,7 +421,7 @@ class FlowGraph(Element):
                     break
             else:
                 if block.is_dummy_block:
-                    port = _dummy_block_add_port(block, key, dir)
+                    port = block.add_missing_port(key, dir)
                 else:
                     raise LookupError('%s key %r not in %s block keys' % (dir, 
key, dir))
             return port
@@ -536,32 +535,3 @@ def _guess_file_format_1(n):
     except:
         pass
     return 0
-
-
-def _initialize_dummy_block(block, block_n):
-    """
-    This is so ugly... dummy-fy a block
-    Modify block object to get the behaviour for a missing block
-    """
-
-    block.key = block_n.get('key')
-    block.is_dummy_block = lambda: True
-    block.is_valid = lambda: False
-    block.get_enabled = lambda: False
-    for param_n in block_n.get('param', []):
-        key = param_n['key']
-        if key not in block.params:
-            new_param_n = {'key': key, 'name': key, 'type': 'string'}
-            param = block.parent_platform.Param(block=block, n=new_param_n)
-            block.params[key] = param
-
-
-def _dummy_block_add_port(block, key, dir):
-    """ This is so ugly... Add a port to a dummy-field block """
-    port_n = {'name': '?', 'key': key, 'type': ''}
-    port = block.parent_platform.Port(block=block, n=port_n, dir=dir)
-    if port.is_source:
-        block.sources.append(port)
-    else:
-        block.sinks.append(port)
-    return port
diff --git a/grc/core/Platform.py b/grc/core/Platform.py
index 997631d..2a8764a 100644
--- a/grc/core/Platform.py
+++ b/grc/core/Platform.py
@@ -32,7 +32,7 @@ from .Element import Element
 from .generator import Generator
 from .FlowGraph import FlowGraph
 from .Connection import Connection
-from .Block import Block, EPyBlock
+from . import Block
 from .Port import Port, PortClone
 from .Param import Param
 
@@ -313,8 +313,9 @@ class Platform(Element):
     FlowGraph = FlowGraph
     Connection = Connection
     block_classes = {
-        None: Block,  # default
-        'epy_block': EPyBlock,
+        None: Block.Block,  # default
+        'epy_block': Block.EPyBlock,
+        '_dummy': Block.DummyBlock,
     }
     port_classes = {
         None: Port,  # default
diff --git a/grc/core/__init__.py b/grc/core/__init__.py
index 3e38807..8b13789 100644
--- a/grc/core/__init__.py
+++ b/grc/core/__init__.py
@@ -1,4 +1 @@
 
-from .Param import Param
-from .Port import Port
-from .Block import Block



reply via email to

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