Imported Upstream version 3.2.2
[debian/gnuradio] / grc / python / flow_graph.tmpl
1 #!/usr/bin/env python
2 ########################################################
3 ##Cheetah template - gnuradio_python
4 ##
5 ##@param imports the import statements
6 ##@param flow_graph the flow_graph
7 ##@param variables the variable blocks
8 ##@param notebooks a list of notebook blocks
9 ##@param controls the variables with gui controls
10 ##@param parameters the paramater blocks
11 ##@param blocks the signal blocks
12 ##@param connections the connections
13 ##@param generate_options the type of flow graph
14 ##@param var_id2cbs variable id map to callback strings
15 ########################################################
16 #def indent($code)
17 #set $code = '\n\t\t'.join(str($code).splitlines())
18 $code#slurp
19 #end def
20 #import time
21 #set $DIVIDER = '#'*50
22 $DIVIDER
23 # Gnuradio Python Flow Graph
24 # Title: $title
25 #if $flow_graph.get_option('author')
26 # Author: $flow_graph.get_option('author')
27 #end if
28 #if $flow_graph.get_option('description')
29 # Description: $flow_graph.get_option('description')
30 #end if
31 # Generated: $time.ctime()
32 $DIVIDER
33
34 ########################################################
35 ##Create Imports
36 ########################################################
37 #for $imp in $imports
38 $imp
39 #end for
40
41 ########################################################
42 ##Create Class
43 ##      Write the class declaration for a top or hier block.
44 ##      The parameter names are the arguments to __init__.
45 ##      Determine the absolute icon path (wx gui only).
46 ##      Setup the IO signature (hier block only).
47 ########################################################
48 #set $class_name = $flow_graph.get_option('id')
49 #set $param_str = ', '.join(['self'] + ['%s=%s'%(param.get_id(), param.get_make()) for param in $parameters])
50 #if $generate_options == 'wx_gui'
51         #import gtk
52         #set $icon = gtk.IconTheme().lookup_icon('gnuradio-grc', 32, 0)
53 class $(class_name)(grc_wxgui.top_block_gui):
54
55         def __init__($param_str):
56                 grc_wxgui.top_block_gui.__init__(self, title="$title")
57         #if $icon
58                 _icon_path = "$icon.get_filename()"
59                 self.SetIcon(wx.Icon(_icon_path, wx.BITMAP_TYPE_ANY))
60         #end if
61 #elif $generate_options == 'no_gui'
62 class $(class_name)(gr.top_block):
63
64         def __init__($param_str):
65                 gr.top_block.__init__(self, "$title")
66 #elif $generate_options == 'hb'
67         #set $in_sig = $flow_graph.get_input_signature()
68         #set $out_sig = $flow_graph.get_output_signature()
69 class $(class_name)(gr.hier_block2):
70
71         def __init__($param_str):
72                 gr.hier_block2.__init__(
73                         self, "$title",
74                         gr.io_signature($in_sig.nports, $in_sig.nports, $in_sig.size*$in_sig.vlen),
75                         gr.io_signature($out_sig.nports, $out_sig.nports, $out_sig.size*$out_sig.vlen),
76                 )
77 #end if
78 ########################################################
79 ##Create Parameters
80 ##      Set the parameter to a property of self.
81 ########################################################
82 #if $parameters
83
84                 $DIVIDER
85                 # Parameters
86                 $DIVIDER
87 #end if
88 #for $param in $parameters
89                 $indent($param.get_var_make())
90 #end for
91 ########################################################
92 ##Create Variables
93 ########################################################
94 #if $variables
95
96                 $DIVIDER
97                 # Variables
98                 $DIVIDER
99 #end if
100 #for $var in $variables
101                 $indent($var.get_var_make())
102 #end for
103 ########################################################
104 ##Create Notebooks
105 ########################################################
106 #if $notebooks
107
108                 $DIVIDER
109                 # Notebooks
110                 $DIVIDER
111 #end if
112 #for $notebook in $notebooks
113                 $indent($notebook.get_make())
114 #end for
115 ########################################################
116 ##Create Controls
117 ########################################################
118 #if $controls
119
120                 $DIVIDER
121                 # Controls
122                 $DIVIDER
123 #end if
124 #for $ctrl in $controls
125                 $indent($ctrl.get_make())
126 #end for
127 ########################################################
128 ##Create Blocks
129 ########################################################
130 #if $blocks
131
132                 $DIVIDER
133                 # Blocks
134                 $DIVIDER
135 #end if
136 #for $blk in filter(lambda b: b.get_make(), $blocks)
137                 self.$blk.get_id() = $indent($blk.get_make())
138 #end for
139 ########################################################
140 ##Create Connections
141 ##      The port name should be the id of the parent block.
142 ##      However, port names for IO pads should be self.
143 ########################################################
144 #if $connections
145
146                 $DIVIDER
147                 # Connections
148                 $DIVIDER
149 #end if
150 #for $con in $connections
151         #set $source = $con.get_source()
152         #set $sink = $con.get_sink()
153         #if $source.get_parent().get_key() == 'pad_source'
154                 #set $source_name = 'self'
155         #else
156                 #set $source_name = 'self.' + $source.get_parent().get_id()
157         #end if
158         #if $sink.get_parent().get_key() == 'pad_sink'
159                 #set $sink_name = 'self'
160         #else
161                 #set $sink_name = 'self.' + $sink.get_parent().get_id()
162         #end if
163                 self.connect(($source_name, $source.get_key()), ($sink_name, $sink.get_key()))
164 #end for
165
166 ########################################################
167 ##Create Callbacks
168 ##      Write a set method for this variable that calls the callbacks
169 ########################################################
170 #for $var in $parameters + $variables
171         #set $id = $var.get_id()
172         def set_$(id)(self, $id):
173                 self.$id = $id
174         #for $callback in $var_id2cbs[$id]
175                 $indent($callback)
176         #end for
177
178 #end for
179 ########################################################
180 ##Create Main
181 ##      For top block code, generate a main routine.
182 ##      Instantiate the top block and run as gui or cli.
183 ########################################################
184 #if $generate_options != 'hb'
185 if __name__ == '__main__':
186         parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
187         #set $params_eq_list = list()
188         #for $param in $parameters
189                 #set $type = $param.get_param('type').get_value()
190                 #if $type
191                         #silent $params_eq_list.append('%s=options.%s'%($param.get_id(), $param.get_id()))
192                         #set $short_id = $param.get_param('short_id').get_evaluated()
193                         #if $short_id
194                                 #set $short_id = '-' + $short_id
195                         #end if
196         parser.add_option("$short_id", "--$param.get_id()", dest="$param.get_id()", type="$type", default=$param.get_make(),
197                 help="Set $($param.get_param('label').evaluate() or $param.get_id()) [default=%default]")
198                 #end if
199         #end for
200         (options, args) = parser.parse_args()
201         #if $flow_graph.get_option('realtime_scheduling')
202         if gr.enable_realtime_scheduling() != gr.RT_OK:
203                 print "Error: failed to enable realtime scheduling."
204         #end if
205         tb = $(class_name)($(', '.join($params_eq_list)))
206         #if $generate_options == 'wx_gui'
207         tb.Run($flow_graph.get_option('run'))
208         #elif $generate_options == 'no_gui'
209         tb.start()
210         raw_input('Press Enter to quit: ')
211         tb.stop()
212         #end if
213 #end if
214