commit-gnuradio
[Top][All Lists]
Advanced

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

[Commit-gnuradio] [gnuradio] 01/05: grc: auto-add callbacks for epy bloc


From: git
Subject: [Commit-gnuradio] [gnuradio] 01/05: grc: auto-add callbacks for epy block params
Date: Thu, 28 Apr 2016 00:13:12 +0000 (UTC)

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

jcorgan pushed a commit to branch master
in repository gnuradio.

commit 031a9dc18326ce1f4328c8edf131cba5904c3b13
Author: Sebastian Koslowski <address@hidden>
Date:   Wed Apr 27 12:35:12 2016 +0200

    grc: auto-add callbacks for epy block params
---
 grc/blocks/epy_block.xml       |  9 ++++++---
 grc/core/Block.py              | 10 +++++++---
 grc/core/utils/epy_block_io.py | 37 ++++++++++++++++++++++++++++++++-----
 3 files changed, 45 insertions(+), 11 deletions(-)

diff --git a/grc/blocks/epy_block.xml b/grc/blocks/epy_block.xml
index d443d29..3fd5aa8 100644
--- a/grc/blocks/epy_block.xml
+++ b/grc/blocks/epy_block.xml
@@ -25,7 +25,8 @@ be the parameters. All of them are required to have default 
values!
 import numpy as np
 from gnuradio import gr
 
-class blk(gr.sync_block):
+
+class blk(gr.sync_block):  # other base classes are basic_block, decim_block, 
interp_block
     """Embedded Python Block example - a simple multiply const"""
 
     def __init__(self, example_param=1.0):  # only default arguments here
@@ -36,11 +37,13 @@ class blk(gr.sync_block):
             in_sig=[np.complex64],
             out_sig=[np.complex64]
         )
-        self.factor = example_param
+        # if an attribute with the same name as a parameter is found,
+        # a callback is registered (properties work, too).
+        self.example_param = example_param
 
     def work(self, input_items, output_items):
         """example: multiply with constant"""
-        output_items[0][:] = input_items[0] * self.factor
+        output_items[0][:] = input_items[0] * self.example_param
         return len(output_items[0])
 </value>
     <type>_multiline_python_external</type>
diff --git a/grc/core/Block.py b/grc/core/Block.py
index cb4eb0d..106e4bc 100644
--- a/grc/core/Block.py
+++ b/grc/core/Block.py
@@ -418,8 +418,11 @@ class Block(Element):
         except Exception as e:
             self._epy_reload_error = ValueError(str(e))
             try:  # Load last working block io
-                blk_io = epy_block_io.BlockIO(*eval(param_blk.get_value()))
-            except:
+                blk_io_args = eval(param_blk.get_value())
+                if len(blk_io_args) == 6:
+                    blk_io_args += ([],)  # add empty callbacks
+                blk_io = epy_block_io.BlockIO(*blk_io_args)
+            except Exception:
                 return
         else:
             self._epy_reload_error = None  # Clear previous errors
@@ -432,7 +435,8 @@ class Block(Element):
         self._doc = blk_io.doc
         self._imports[0] = 'import ' + self.get_id()
         self._make = '{0}.{1}({2})'.format(self.get_id(), blk_io.cls, ', 
'.join(
-            '{0}=${0}'.format(key) for key, _ in blk_io.params))
+            '{0}=${{ {0} }}'.format(key) for key, _ in blk_io.params))
+        self._callbacks = ['{0} = ${{ {0} }}'.format(attr) for attr in 
blk_io.callbacks]
 
         params = {}
         for param in list(self._params):
diff --git a/grc/core/utils/epy_block_io.py b/grc/core/utils/epy_block_io.py
index df3a4bb..76b5005 100644
--- a/grc/core/utils/epy_block_io.py
+++ b/grc/core/utils/epy_block_io.py
@@ -11,7 +11,7 @@ TYPE_MAP = {
     'int8': 'byte', 'uint8': 'byte',
 }
 
-BlockIO = collections.namedtuple('BlockIO', 'name cls params sinks sources 
doc')
+BlockIO = collections.namedtuple('BlockIO', 'name cls params sinks sources doc 
callbacks')
 
 
 def _ports(sigs, msgs):
@@ -51,6 +51,7 @@ def extract(cls):
         cls = _find_block_class(cls, gr.gateway.gateway_block)
 
     spec = inspect.getargspec(cls.__init__)
+    init_args = spec.args[1:]
     defaults = map(repr, spec.defaults or ())
     doc = cls.__doc__ or cls.__init__.__doc__ or ''
     cls_name = cls.__name__
@@ -64,14 +65,23 @@ def extract(cls):
         raise RuntimeError("Can't create an instance of your block: " + str(e))
 
     name = instance.name()
-    params = list(zip(spec.args[1:], defaults))
+
+    params = list(zip(init_args, defaults))
+
+    def settable(attr):
+        try:
+            return callable(getattr(cls, attr).fset)  # check for a property 
with setter
+        except AttributeError:
+            return attr in instance.__dict__  # not dir() - only the instance 
attribs
+
+    callbacks = [attr for attr in dir(instance) if attr in init_args and 
settable(attr)]
 
     sinks = _ports(instance.in_sig(),
                    pmt.to_python(instance.message_ports_in()))
     sources = _ports(instance.out_sig(),
                      pmt.to_python(instance.message_ports_out()))
 
-    return BlockIO(name, cls_name, params, sinks, sources, doc)
+    return BlockIO(name, cls_name, params, sinks, sources, doc, callbacks)
 
 
 if __name__ == '__main__':
@@ -81,7 +91,7 @@ from gnuradio import gr
 import pmt
 
 class blk(gr.sync_block):
-    def __init__(self, param1=None, param2=None):
+    def __init__(self, param1=None, param2=None, param3=None):
         "Test Docu"
         gr.sync_block.__init__(
             self,
@@ -91,8 +101,25 @@ class blk(gr.sync_block):
         )
         self.message_port_register_in(pmt.intern('msg_in'))
         self.message_port_register_out(pmt.intern('msg_out'))
+        self.param1 = param1
+        self._param2 = param2
+        self._param3 = param3
+
+    @property
+    def param2(self):
+        return self._param2
+
+    @property
+    def param3(self):
+        return self._param3
+
+    @param3.setter
+    def param3(self, value):
+        self._param3 = value
 
     def work(self, inputs_items, output_items):
         return 10
     """
-    print extract(blk_code)
+    from pprint import pprint
+    pprint(dict(extract(blk_code)._asdict()))
+



reply via email to

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