2 # Copyright 2006 Free Software Foundation, Inc.
4 # This file is part of GNU Radio
6 # GNU Radio is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3, or (at your option)
11 # GNU Radio is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with GNU Radio; see the file COPYING. If not, write to
18 # the Free Software Foundation, Inc., 51 Franklin Street,
19 # Boston, MA 02110-1301, USA.
22 from gnuradio_swig_python import hier_block2_swig, gr_make_runtime, \
23 runtime_run_unlocked, runtime_start_unlocked, runtime_stop_unlocked, \
24 runtime_wait_unlocked, runtime_restart_unlocked, io_signature
27 # This hack forces a 'has-a' relationship to look like an 'is-a' one.
29 # It allows Python classes to subclass this one, while passing through
30 # method calls to the C++ class shared pointer from SWIG.
32 # It also allows us to intercept method calls if needed
34 class hier_block2(object):
35 def __init__(self, name, input_signature, output_signature):
36 self._hb = hier_block2_swig(name, input_signature, output_signature)
38 def __getattr__(self, name):
39 return getattr(self._hb, name)
41 def connect(self, *points):
42 '''connect requires two or more arguments that can be coerced to endpoints.
43 If more than two arguments are provided, they are connected together successively.
46 raise ValueError, ("connect requires at least two endpoints; %d provided." % (len (points),))
47 for i in range (1, len (points)):
48 self._connect(points[i-1], points[i])
50 def _connect(self, src, dst):
51 (src_block, src_port) = self._coerce_endpoint(src)
52 (dst_block, dst_port) = self._coerce_endpoint(dst)
53 self._hb.connect(src_block.basic_block(), src_port,
54 dst_block.basic_block(), dst_port)
56 def _coerce_endpoint(self, endp):
57 if hasattr(endp, 'basic_block'):
60 if hasattr(endp, "__getitem__") and len(endp) == 2:
61 return endp # Assume user put (block, port)
63 raise ValueError("unable to coerce endpoint")
65 def disconnect(self, *points):
66 '''connect requires two or more arguments that can be coerced to endpoints.
67 If more than two arguments are provided, they are disconnected successively.
70 raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
71 for i in range (1, len (points)):
72 self._disconnect(points[i-1], points[i])
74 def _disconnect(self, src, dst):
75 (src_block, src_port) = self._coerce_endpoint(src)
76 (dst_block, dst_port) = self._coerce_endpoint(dst)
77 self._hb.disconnect(src_block.basic_block(), src_port,
78 dst_block.basic_block(), dst_port)
80 # Convenience class to create a no input, no output block for runtime top block
81 class top_block(hier_block2):
82 def __init__(self, name):
83 hier_block2.__init__(self, name, io_signature(0,0,0), io_signature(0,0,0))
85 # This allows the 'run_locked' methods, which are defined in gr_runtime.i,
86 # to release the Python global interpreter lock before calling the actual
87 # method in gr.runtime
89 # This probably should be elsewhere but it works here
90 class runtime(object):
91 def __init__(self, top_block):
92 if (isinstance(top_block, hier_block2)):
93 self._r = gr_make_runtime(top_block._hb)
95 self._r = gr_make_runtime(top_block)
98 runtime_run_unlocked(self._r)
101 runtime_start_unlocked(self._r)
104 runtime_stop_unlocked(self._r)
107 runtime_wait_unlocked(self._r)
110 runtime_restart_unlocked(self._r)