Imported Upstream version 3.2.2
[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     """
34     Python wrapper around the C++ hierarchical block implementation.
35     Provides convenience functions and allows proper Python subclassing.
36     """
37
38     def __init__(self, name, input_signature, output_signature):
39         """
40         Create a hierarchical block with a given name and I/O signatures.
41         """
42         self._hb = hier_block2_swig(name, input_signature, output_signature)
43
44     def __getattr__(self, name):
45         """
46         Pass-through member requests to the C++ object.
47         """
48         if not hasattr(self, "_hb"):
49             raise RuntimeError("hier_block2: invalid state--did you forget to call gr.hier_block2.__init__ in a derived class?")
50         return getattr(self._hb, name)
51
52     def connect(self, *points):
53         """
54         Connect two or more block endpoints.  An endpoint is either a (block, port)
55         tuple or a block instance.  In the latter case, the port number is assumed
56         to be zero.
57
58         To connect the hierarchical block external inputs or outputs to internal block
59         inputs or outputs, use 'self' in the connect call.
60
61         If multiple arguments are provided, connect will attempt to wire them in series,
62         interpreting the endpoints as inputs or outputs as appropriate.
63         """
64
65         if len (points) < 1:
66             raise ValueError, ("connect requires at least one endpoint; %d provided." % (len (points),))
67         else:
68             if len(points) == 1:
69                 self._hb.connect(points[0].basic_block())
70             else:
71                 for i in range (1, len (points)):
72                     self._connect(points[i-1], points[i])
73
74     def _connect(self, src, dst):
75         (src_block, src_port) = self._coerce_endpoint(src)
76         (dst_block, dst_port) = self._coerce_endpoint(dst)
77         self._hb.connect(src_block.basic_block(), src_port,
78                          dst_block.basic_block(), dst_port)
79
80     def _coerce_endpoint(self, endp):
81         if hasattr(endp, 'basic_block'):
82             return (endp, 0)
83         else:
84             if hasattr(endp, "__getitem__") and len(endp) == 2:
85                 return endp # Assume user put (block, port)
86             else:
87                 raise ValueError("unable to coerce endpoint")
88
89     def disconnect(self, *points):
90         """
91         Disconnect two endpoints in the flowgraph.
92
93         To disconnect the hierarchical block external inputs or outputs to internal block
94         inputs or outputs, use 'self' in the connect call.
95
96         If more than two arguments are provided, they are disconnected successively.
97         """
98         
99         if len (points) < 1:
100             raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
101         else:
102             if len (points) == 1:
103                 self._hb.disconnect(points[0].basic_block())
104             else:
105                 for i in range (1, len (points)):
106                     self._disconnect(points[i-1], points[i])
107
108     def _disconnect(self, src, dst):
109         (src_block, src_port) = self._coerce_endpoint(src)
110         (dst_block, dst_port) = self._coerce_endpoint(dst)
111         self._hb.disconnect(src_block.basic_block(), src_port,
112                             dst_block.basic_block(), dst_port)
113