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