2 # Copyright 2006,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 hier_block2_swig
25 # This hack forces a 'has-a' relationship to look like an 'is-a' one.
27 # It allows Python classes to subclass this one, while passing through
28 # method calls to the C++ class shared pointer from SWIG.
30 # It also allows us to intercept method calls if needed
32 class hier_block2(object):
34 Python wrapper around the C++ hierarchical block implementation.
35 Provides convenience functions and allows proper Python subclassing.
38 def __init__(self, name, input_signature, output_signature):
40 Create a hierarchical block with a given name and I/O signatures.
42 self._hb = hier_block2_swig(name, input_signature, output_signature)
44 def __getattr__(self, name):
46 Pass-through member requests to the C++ object.
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)
52 def connect(self, *points):
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
58 To connect the hierarchical block external inputs or outputs to internal block
59 inputs or outputs, use 'self' in the connect call.
61 If multiple arguments are provided, connect will attempt to wire them in series,
62 interpreting the endpoints as inputs or outputs as appropriate.
66 raise ValueError, ("connect requires at least one endpoint; %d provided." % (len (points),))
69 self._hb.connect(points[0].basic_block())
71 for i in range (1, len (points)):
72 self._connect(points[i-1], points[i])
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)
80 def _coerce_endpoint(self, endp):
81 if hasattr(endp, 'basic_block'):
84 if hasattr(endp, "__getitem__") and len(endp) == 2:
85 return endp # Assume user put (block, port)
87 raise ValueError("unable to coerce endpoint")
89 def disconnect(self, *points):
91 Disconnect two endpoints in the flowgraph.
93 To disconnect the hierarchical block external inputs or outputs to internal block
94 inputs or outputs, use 'self' in the connect call.
96 If more than two arguments are provided, they are disconnected successively.
100 raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
102 if len (points) == 1:
103 self._hb.disconnect(points[0].basic_block())
105 for i in range (1, len (points)):
106 self._disconnect(points[i-1], points[i])
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)