Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / hier_block2.py
1 #
2 # Copyright 2006 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, gr_make_runtime, \
23     runtime_run_unlocked, runtime_start_unlocked, runtime_stop_unlocked, \
24     runtime_wait_unlocked, runtime_restart_unlocked, io_signature
25
26 #
27 # This hack forces a 'has-a' relationship to look like an 'is-a' one.
28 #
29 # It allows Python classes to subclass this one, while passing through
30 # method calls to the C++ class shared pointer from SWIG.
31 #
32 # It also allows us to intercept method calls if needed
33 #
34 class hier_block2(object):
35     def __init__(self, name, input_signature, output_signature):
36         self._hb = hier_block2_swig(name, input_signature, output_signature)
37
38     def __getattr__(self, name):
39         return getattr(self._hb, name)
40
41     def connect(self, *points):
42         '''connect requires two or more arguments that can be coerced to endpoints.
43         If more than two arguments are provided, they are connected together successively.
44         '''
45         if len (points) < 2:
46             raise ValueError, ("connect requires at least two endpoints; %d provided." % (len (points),))
47         for i in range (1, len (points)):
48             self._connect(points[i-1], points[i])
49
50     def _connect(self, src, dst):
51         (src_block, src_port) = self._coerce_endpoint(src)
52         (dst_block, dst_port) = self._coerce_endpoint(dst)
53         self._hb.connect(src_block.basic_block(), src_port,
54                          dst_block.basic_block(), dst_port)
55
56     def _coerce_endpoint(self, endp):
57         if hasattr(endp, 'basic_block'):
58             return (endp, 0)
59         else:
60             if hasattr(endp, "__getitem__") and len(endp) == 2:
61                 return endp # Assume user put (block, port)
62             else:
63                 raise ValueError("unable to coerce endpoint")
64
65     def disconnect(self, *points):
66         '''connect requires two or more arguments that can be coerced to endpoints.
67         If more than two arguments are provided, they are disconnected successively.
68         '''
69         if len (points) < 2:
70             raise ValueError, ("disconnect requires at least two endpoints; %d provided." % (len (points),))
71         for i in range (1, len (points)):
72             self._disconnect(points[i-1], points[i])
73
74     def _disconnect(self, src, dst):
75         (src_block, src_port) = self._coerce_endpoint(src)
76         (dst_block, dst_port) = self._coerce_endpoint(dst)
77         self._hb.disconnect(src_block.basic_block(), src_port,
78                             dst_block.basic_block(), dst_port)
79
80 # Convenience class to create a no input, no output block for runtime top block
81 class top_block(hier_block2):
82     def __init__(self, name):
83         hier_block2.__init__(self, name, io_signature(0,0,0), io_signature(0,0,0))
84
85 # This allows the 'run_locked' methods, which are defined in gr_runtime.i,
86 # to release the Python global interpreter lock before calling the actual
87 # method in gr.runtime
88 #
89 # This probably should be elsewhere but it works here
90 class runtime(object):
91     def __init__(self, top_block):
92         if (isinstance(top_block, hier_block2)):
93             self._r = gr_make_runtime(top_block._hb)            
94         else:
95             self._r = gr_make_runtime(top_block)
96
97     def run(self):
98         runtime_run_unlocked(self._r)
99
100     def start(self):
101         runtime_start_unlocked(self._r)
102
103     def stop(self):
104         runtime_stop_unlocked(self._r)
105
106     def wait(self):
107         runtime_wait_unlocked(self._r)
108
109     def restart(self):
110         runtime_restart_unlocked(self._r)