Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / python / gnuradio / gr / hier_block.py
1 #
2 # Copyright 2005 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 2, 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 io_signature
23 import weakref
24
25 class hier_block_base(object):
26     """
27     Abstract base class for hierarchical signal processing blocks.
28     """
29     def __init__(self, fg):
30         """
31         @param fg: The flow graph that contains this hierarchical block.
32         @type fg: gr.flow_graph
33         """
34         self.fg = weakref.proxy(fg)
35
36     def input_signature(self):
37         """
38         @return input signature of hierarchical block.
39         @rtype gr.io_signature
40         """
41         raise NotImplemented
42
43     def output_signature(self):
44         """
45         @return output signature of hierarchical block.
46         @rtype gr.io_signature
47         """
48         raise NotImplemented
49
50     def resolve_input_port(self, port_number):
51         """
52         @param port_number: which input port number to resolve to an endpoint.
53         @type port_number: int
54         @return: sequence of endpoints
55         @rtype: sequence of endpoint
56
57         Note that an input port can resolve to more than one endpoint.
58         """
59         raise NotImplemented
60     
61     def resolve_output_port(self, port_number):
62         """
63         @param port_number: which output port number to resolve to an endpoint.
64         @type port_number: int
65         @return: endpoint
66         @rtype: endpoint
67
68         Output ports resolve to a single endpoint.
69         """
70         raise NotImplemented
71     
72
73 class hier_block(hier_block_base):
74     """
75     Simple concrete class for building hierarchical blocks.
76
77     This class assumes that there is at most a single block at the
78     head of the chain and a single block at the end of the chain.
79     Either head or tail may be None indicating a sink or source respectively.
80
81     If you needs something more elaborate than this, derive a new class from
82     hier_block_base.
83     """
84     def __init__(self, fg, head_block, tail_block):
85         """
86         @param fg: The flow graph that contains this hierarchical block.
87         @type fg: flow_graph
88         @param head_block: the first block in the signal processing chain.
89         @type head_block: None or subclass of gr.block or gr.hier_block_base
90         @param tail_block: the last block in the signal processing chain.
91         @type tail_block: None or subclass of gr.block or gr.hier_block_base
92         """
93         hier_block_base.__init__(self, fg)
94         # FIXME add type checks here for easier debugging of misuse
95         self.head = head_block
96         self.tail = tail_block
97
98     def input_signature(self):
99         if self.head:
100             return self.head.input_signature()
101         else:
102             return io_signature(0,0,0)
103     
104     def output_signature(self):
105         if self.tail:
106             return self.tail.output_signature()
107         else:
108             return io_signature(0,0,0)
109     
110     def resolve_input_port(self, port_number):
111         return ((self.head, port_number),)
112
113     def resolve_output_port(self, port_number):
114         return (self.tail, port_number)
115
116
117 class compose(hier_block):
118     """
119     Compose one or more blocks (primitive or hierarchical) into a new hierarchical block.
120     """
121     def __init__(self, fg, *blocks):
122         """
123         @param fg: flow graph
124         @type fg: gr.flow_graph
125         @param *blocks: list of blocks
126         @type *blocks: list of blocks
127         """
128         if len(blocks) < 1:
129             raise ValueError, ("compose requires at least one block; none provided.")
130         if len(blocks) > 1:
131             fg.connect(*blocks)
132         hier_block.__init__(self, fg, blocks[0], blocks[-1])