Merge r6461:6464 from jcorgan/t162-staging into trunk.
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / hier_block2.py
1 #
2 # Copyright 2006,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 hier_block2_swig
23
24 #
25 # This hack forces a 'has-a' relationship to look like an 'is-a' one.
26 #
27 # It allows Python classes to subclass this one, while passing through
28 # method calls to the C++ class shared pointer from SWIG.
29 #
30 # It also allows us to intercept method calls if needed
31 #
32 class hier_block2(object):
33     def __init__(self, name, input_signature, output_signature):
34         self._hb = hier_block2_swig(name, input_signature, output_signature)
35
36     def __getattr__(self, name):
37         return getattr(self._hb, name)
38
39     def connect(self, *points):
40         '''connect requires one or more arguments that can be coerced to endpoints.
41         If more than two arguments are provided, they are connected together successively.
42         '''
43         if len (points) < 1:
44             raise ValueError, ("connect requires at least one endpoint; %d provided." % (len (points),))
45         else:
46             if len(points) == 1:
47                 self._hb.connect(points[0].basic_block())
48             else:
49                 for i in range (1, len (points)):
50                     self._connect(points[i-1], points[i])
51
52     def _connect(self, src, dst):
53         (src_block, src_port) = self._coerce_endpoint(src)
54         (dst_block, dst_port) = self._coerce_endpoint(dst)
55         self._hb.connect(src_block.basic_block(), src_port,
56                          dst_block.basic_block(), dst_port)
57
58     def _coerce_endpoint(self, endp):
59         if hasattr(endp, 'basic_block'):
60             return (endp, 0)
61         else:
62             if hasattr(endp, "__getitem__") and len(endp) == 2:
63                 return endp # Assume user put (block, port)
64             else:
65                 raise ValueError("unable to coerce endpoint")
66
67     def disconnect(self, *points):
68         '''connect requires one or more arguments that can be coerced to endpoints.
69         If more than two arguments are provided, they are disconnected successively.
70         '''
71         if len (points) < 1:
72             raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
73         else:
74             if len (points) == 1:
75                 self._hb.disconnect(points[0].basic_block())
76             else:
77                 for i in range (1, len (points)):
78                     self._disconnect(points[i-1], points[i])
79
80     def _disconnect(self, src, dst):
81         (src_block, src_port) = self._coerce_endpoint(src)
82         (dst_block, dst_port) = self._coerce_endpoint(dst)
83         self._hb.disconnect(src_block.basic_block(), src_port,
84                             dst_block.basic_block(), dst_port)
85