2 # Copyright 2007 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 top_block_swig, \
23 top_block_wait_unlocked, top_block_run_unlocked
25 #import gnuradio.gr.gr_threading as _threading
26 import gr_threading as _threading
30 # There is no problem that can't be solved with an additional
31 # level of indirection...
33 # This kludge allows ^C to interrupt top_block.run and top_block.wait
35 class _top_block_waiter(_threading.Thread):
36 def __init__(self, tb):
37 _threading.Thread.__init__(self)
40 self.event = _threading.Event()
44 top_block_wait_unlocked(self.tb)
48 while not self.event.isSet():
49 self.event.wait(0.100)
53 # This hack forces a 'has-a' relationship to look like an 'is-a' one.
55 # It allows Python classes to subclass this one, while passing through
56 # method calls to the C++ class shared pointer from SWIG.
58 # It also allows us to intercept method calls if needed.
60 # This allows the 'run_locked' methods, which are defined in gr_top_block.i,
61 # to release the Python global interpreter lock before calling the actual
62 # method in gr_top_block
64 class top_block(object):
65 def __init__(self, name="top_block"):
66 self._tb = top_block_swig(name)
68 def __getattr__(self, name):
69 return getattr(self._tb, name)
82 _top_block_waiter(self._tb).wait()
85 # FIXME: these are duplicated from hier_block2.py; they should really be implemented
86 # in the original C++ class (gr_hier_block2), then they would all be inherited here
88 def connect(self, *points):
89 '''connect requires one or more arguments that can be coerced to endpoints.
90 If more than two arguments are provided, they are connected together successively.
93 raise ValueError, ("connect requires at least one endpoint; %d provided." % (len (points),))
96 self._tb.connect(points[0].basic_block())
98 for i in range (1, len (points)):
99 self._connect(points[i-1], points[i])
101 def _connect(self, src, dst):
102 (src_block, src_port) = self._coerce_endpoint(src)
103 (dst_block, dst_port) = self._coerce_endpoint(dst)
104 self._tb.connect(src_block.basic_block(), src_port,
105 dst_block.basic_block(), dst_port)
107 def _coerce_endpoint(self, endp):
108 if hasattr(endp, 'basic_block'):
111 if hasattr(endp, "__getitem__") and len(endp) == 2:
112 return endp # Assume user put (block, port)
114 raise ValueError("unable to coerce endpoint")
116 def disconnect(self, *points):
117 '''connect requires one or more arguments that can be coerced to endpoints.
118 If more than two arguments are provided, they are disconnected successively.
121 raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
124 self._tb.disconnect(points[0].basic_block())
126 for i in range (1, len (points)):
127 self._disconnect(points[i-1], points[i])
129 def _disconnect(self, src, dst):
130 (src_block, src_port) = self._coerce_endpoint(src)
131 (dst_block, dst_port) = self._coerce_endpoint(dst)
132 self._tb.disconnect(src_block.basic_block(), src_port,
133 dst_block.basic_block(), dst_port)