Merge r6461:6464 from jcorgan/t162-staging into trunk.
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / top_block.py
1 #
2 # Copyright 2007 Free Software Foundation, Inc.
3
4 # This file is part of GNU Radio
5
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)
9 # any later version.
10
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.
15
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.
20
21
22 from gnuradio_swig_python import top_block_swig, \
23     top_block_wait_unlocked, top_block_run_unlocked
24
25 #
26 # This hack forces a 'has-a' relationship to look like an 'is-a' one.
27 #
28 # It allows Python classes to subclass this one, while passing through
29 # method calls to the C++ class shared pointer from SWIG.
30 #
31 # It also allows us to intercept method calls if needed.
32 #
33 # This allows the 'run_locked' methods, which are defined in gr_top_block.i,
34 # to release the Python global interpreter lock before calling the actual
35 # method in gr_top_block
36 #
37 class top_block(object):
38     def __init__(self, name="top_block"):
39         self._tb = top_block_swig(name)
40
41     def __getattr__(self, name):
42         return getattr(self._tb, name)
43
44     def run(self):
45         top_block_run_unlocked(self._tb)
46
47     def wait(self):
48         top_block_wait_unlocked(self._tb)
49
50     # FIXME: these are duplicated from hier_block2.py; they should really be implemented
51     # in the original C++ class (gr_hier_block2), then they would all be inherited here
52
53     def connect(self, *points):
54         '''connect requires one or more arguments that can be coerced to endpoints.
55         If more than two arguments are provided, they are connected together successively.
56         '''
57         if len (points) < 1:
58             raise ValueError, ("connect requires at least one endpoint; %d provided." % (len (points),))
59         else:
60             if len(points) == 1:
61                 self._tb.connect(points[0].basic_block())
62             else:
63                 for i in range (1, len (points)):
64                     self._connect(points[i-1], points[i])
65
66     def _connect(self, src, dst):
67         (src_block, src_port) = self._coerce_endpoint(src)
68         (dst_block, dst_port) = self._coerce_endpoint(dst)
69         self._tb.connect(src_block.basic_block(), src_port,
70                          dst_block.basic_block(), dst_port)
71
72     def _coerce_endpoint(self, endp):
73         if hasattr(endp, 'basic_block'):
74             return (endp, 0)
75         else:
76             if hasattr(endp, "__getitem__") and len(endp) == 2:
77                 return endp # Assume user put (block, port)
78             else:
79                 raise ValueError("unable to coerce endpoint")
80
81     def disconnect(self, *points):
82         '''connect requires one or more arguments that can be coerced to endpoints.
83         If more than two arguments are provided, they are disconnected successively.
84         '''
85         if len (points) < 1:
86             raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
87         else:
88             if len(points) == 1:
89                 self._tb.disconnect(points[0].basic_block())
90             else:
91                 for i in range (1, len (points)):
92                     self._disconnect(points[i-1], points[i])
93
94     def _disconnect(self, src, dst):
95         (src_block, src_port) = self._coerce_endpoint(src)
96         (dst_block, dst_port) = self._coerce_endpoint(dst)
97         self._tb.disconnect(src_block.basic_block(), src_port,
98                             dst_block.basic_block(), dst_port)
99