diff options
author | jcorgan | 2007-08-27 18:49:11 +0000 |
---|---|---|
committer | jcorgan | 2007-08-27 18:49:11 +0000 |
commit | c088a546ac7ae55748e5421201f3387f3e1286f9 (patch) | |
tree | b655773370d082062c2842181bff1638354c57c8 /gnuradio-core/src/python | |
parent | 06945c04b4af8af035aeee2e28d5e5626888f5ee (diff) | |
download | gnuradio-c088a546ac7ae55748e5421201f3387f3e1286f9.tar.gz gnuradio-c088a546ac7ae55748e5421201f3387f3e1286f9.tar.bz2 gnuradio-c088a546ac7ae55748e5421201f3387f3e1286f9.zip |
Merged r6171:6186 from jcorgan/fg into trunk.
Changes hierarchical flow graph API to use gr.top_block instead
of gr.runtime.
See discuss-gnuradio mailing list for explanation of changes.
GRC has not been updated to use the changed API.
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@6187 221aa14e-8319-0410-a670-987f0aec2ac5
Diffstat (limited to 'gnuradio-core/src/python')
6 files changed, 98 insertions, 69 deletions
diff --git a/gnuradio-core/src/python/gnuradio/gr/Makefile.am b/gnuradio-core/src/python/gnuradio/gr/Makefile.am index c9c946c2b..77cc53e3f 100644 --- a/gnuradio-core/src/python/gnuradio/gr/Makefile.am +++ b/gnuradio-core/src/python/gnuradio/gr/Makefile.am @@ -41,7 +41,8 @@ grgrpython_PYTHON = \ hier_block.py \ hier_block2.py \ prefs.py \ - scheduler.py + scheduler.py \ + top_block.py noinst_PYTHON = \ benchmark_filters.py \ diff --git a/gnuradio-core/src/python/gnuradio/gr/__init__.py b/gnuradio-core/src/python/gnuradio/gr/__init__.py index 8da93ec57..69f745fbf 100644 --- a/gnuradio-core/src/python/gnuradio/gr/__init__.py +++ b/gnuradio-core/src/python/gnuradio/gr/__init__.py @@ -30,6 +30,7 @@ from flow_graph import * from exceptions import * from hier_block import * from hier_block2 import * +from top_block import * # create a couple of aliases serial_to_parallel = stream_to_vector diff --git a/gnuradio-core/src/python/gnuradio/gr/hier_block2.py b/gnuradio-core/src/python/gnuradio/gr/hier_block2.py index 4699bb6fe..bc6402b87 100644 --- a/gnuradio-core/src/python/gnuradio/gr/hier_block2.py +++ b/gnuradio-core/src/python/gnuradio/gr/hier_block2.py @@ -1,5 +1,5 @@ # -# Copyright 2006 Free Software Foundation, Inc. +# Copyright 2006,2007 Free Software Foundation, Inc. # # This file is part of GNU Radio # @@ -19,9 +19,7 @@ # Boston, MA 02110-1301, USA. # -from gnuradio_swig_python import hier_block2_swig, gr_make_runtime, \ - runtime_run_unlocked, runtime_start_unlocked, runtime_stop_unlocked, \ - runtime_wait_unlocked, runtime_restart_unlocked, io_signature +from gnuradio_swig_python import hier_block2_swig # # This hack forces a 'has-a' relationship to look like an 'is-a' one. @@ -77,34 +75,3 @@ class hier_block2(object): self._hb.disconnect(src_block.basic_block(), src_port, dst_block.basic_block(), dst_port) -# Convenience class to create a no input, no output block for runtime top block -class top_block(hier_block2): - def __init__(self, name): - hier_block2.__init__(self, name, io_signature(0,0,0), io_signature(0,0,0)) - -# This allows the 'run_locked' methods, which are defined in gr_runtime.i, -# to release the Python global interpreter lock before calling the actual -# method in gr.runtime -# -# This probably should be elsewhere but it works here -class runtime(object): - def __init__(self, top_block): - if (isinstance(top_block, hier_block2)): - self._r = gr_make_runtime(top_block._hb) - else: - self._r = gr_make_runtime(top_block) - - def run(self): - runtime_run_unlocked(self._r) - - def start(self): - runtime_start_unlocked(self._r) - - def stop(self): - runtime_stop_unlocked(self._r) - - def wait(self): - runtime_wait_unlocked(self._r) - - def restart(self): - runtime_restart_unlocked(self._r) diff --git a/gnuradio-core/src/python/gnuradio/gr/qa_hier_block2.py b/gnuradio-core/src/python/gnuradio/gr/qa_hier_block2.py index 9fa002501..d07d4cb38 100755 --- a/gnuradio-core/src/python/gnuradio/gr/qa_hier_block2.py +++ b/gnuradio-core/src/python/gnuradio/gr/qa_hier_block2.py @@ -100,8 +100,7 @@ class test_hier_block2(gr_unittest.TestCase): sink2 = gr.vector_sink_f() hblock.connect(src, sink1) hblock.connect(src, sink2) - runtime = gr.runtime(hblock) - runtime.run() + hblock.run() actual1 = sink1.data() actual2 = sink2.data() self.assertEquals(expected, actual1) @@ -185,8 +184,7 @@ class test_hier_block2(gr_unittest.TestCase): src = gr.vector_source_f(data, False) dst = gr.vector_sink_f() hblock.connect(src, dst) - r = gr.runtime(hblock) - r.run() + hblock.run() self.assertEquals(data, dst.data()) if __name__ == "__main__": diff --git a/gnuradio-core/src/python/gnuradio/gr/qa_runtime.py b/gnuradio-core/src/python/gnuradio/gr/qa_runtime.py deleted file mode 100755 index ce0bdde21..000000000 --- a/gnuradio-core/src/python/gnuradio/gr/qa_runtime.py +++ /dev/null @@ -1,29 +0,0 @@ -#!/usr/bin/env python - -from gnuradio import gr, gr_unittest - -class test_runtime(gr_unittest.TestCase): - - def setUp(self): - pass - - def tearDown(self): - pass - - def test_001_run(self): - hblock = gr.hier_block2("test_block", - gr.io_signature(0,0,0), - gr.io_signature(0,0,0)) - runtime = gr.runtime(hblock) - runtime.run() - - def test_002_run_twice(self): - hblock = gr.hier_block2("test_block", - gr.io_signature(0,0,0), - gr.io_signature(0,0,0)) - runtime = gr.runtime(hblock) - runtime.run() - self.assertRaises(RuntimeError, lambda: runtime.run()) - -if __name__ == "__main__": - gr_unittest.main() diff --git a/gnuradio-core/src/python/gnuradio/gr/top_block.py b/gnuradio-core/src/python/gnuradio/gr/top_block.py new file mode 100644 index 000000000..9b709c01d --- /dev/null +++ b/gnuradio-core/src/python/gnuradio/gr/top_block.py @@ -0,0 +1,91 @@ +# +# Copyright 2007 Free Software Foundation, Inc. +# +# This file is part of GNU Radio +# +# GNU Radio is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# GNU Radio is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with GNU Radio; see the file COPYING. If not, write to +# the Free Software Foundation, Inc., 51 Franklin Street, +# Boston, MA 02110-1301, USA. +# + +from gnuradio_swig_python import top_block_swig, \ + top_block_wait_unlocked, top_block_run_unlocked + +# +# This hack forces a 'has-a' relationship to look like an 'is-a' one. +# +# It allows Python classes to subclass this one, while passing through +# method calls to the C++ class shared pointer from SWIG. +# +# It also allows us to intercept method calls if needed. +# +# This allows the 'run_locked' methods, which are defined in gr_top_block.i, +# to release the Python global interpreter lock before calling the actual +# method in gr_top_block +# +class top_block(object): + def __init__(self, name="top_block"): + self._tb = top_block_swig(name) + + def __getattr__(self, name): + return getattr(self._tb, name) + + def run(self): + top_block_run_unlocked(self._tb) + + def wait(self): + top_block_wait_unlocked(self._tb) + + # FIXME: these are duplicated from hier_block2.py; they should really be implemented + # in the original C++ class (gr_hier_block2), then they would all be inherited here + + def connect(self, *points): + '''connect requires two or more arguments that can be coerced to endpoints. + If more than two arguments are provided, they are connected together successively. + ''' + if len (points) < 2: + raise ValueError, ("connect requires at least two endpoints; %d provided." % (len (points),)) + for i in range (1, len (points)): + self._connect(points[i-1], points[i]) + + def _connect(self, src, dst): + (src_block, src_port) = self._coerce_endpoint(src) + (dst_block, dst_port) = self._coerce_endpoint(dst) + self._tb.connect(src_block.basic_block(), src_port, + dst_block.basic_block(), dst_port) + + def _coerce_endpoint(self, endp): + if hasattr(endp, 'basic_block'): + return (endp, 0) + else: + if hasattr(endp, "__getitem__") and len(endp) == 2: + return endp # Assume user put (block, port) + else: + raise ValueError("unable to coerce endpoint") + + def disconnect(self, *points): + '''connect requires two or more arguments that can be coerced to endpoints. + If more than two arguments are provided, they are disconnected successively. + ''' + if len (points) < 2: + raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),)) + for i in range (1, len (points)): + self._disconnect(points[i-1], points[i]) + + def _disconnect(self, src, dst): + (src_block, src_port) = self._coerce_endpoint(src) + (dst_block, dst_port) = self._coerce_endpoint(dst) + self._tb.disconnect(src_block.basic_block(), src_port, + dst_block.basic_block(), dst_port) + |