Merged r9481:9518 on jblum/grc_reorganize into trunk. Reorganized grc source under...
[debian/gnuradio] / grc / data / platforms / 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_id2expr variable id map to expression
14 ##@param var_id2deps variable id map to direct dependencies
15 ##@param var_id2cbs variable id map to callback strings
16 ########################################################
17 #import time
18 #set $DIVIDER = '#'*50
19 $DIVIDER
20 # Gnuradio Python Flow Graph
21 $('# Title: %s'%$flow_graph.get_option('title'))
22 $('# Author: %s'%$flow_graph.get_option('author'))
23 $('# Description: %s'%$flow_graph.get_option('description'))
24 $('# Generated: %s'%time.ctime())
25 $DIVIDER
26
27 ########################################################
28 ##Create Imports
29 ########################################################
30 #for $imp in $imports
31 $imp
32 #end for
33
34 ########################################################
35 ##Create Class
36 ##      Write the class declaration for a top or hier block.
37 ##      The parameter names are the arguments to __init__.
38 ##      Determine the absolute icon path (wx gui only).
39 ##      Setup the IO signature (hier block only).
40 ########################################################
41 #set $class_name = $flow_graph.get_option('id')
42 #set $param_str = ', '.join(['self'] + ['%s=%s'%(param.get_id(), param.get_make()) for param in $parameters])
43 #if $generate_options == 'wx_gui'
44         #from gnuradio.grc.platforms.base.Constants import DATA_DIR
45         #from gnuradio.grc.gui.Constants import MAIN_WINDOW_PREFIX
46 class $(class_name)(grc_wxgui.top_block_gui):
47
48         def __init__($param_str):
49                 grc_wxgui.top_block_gui.__init__(
50                         self,
51                         title="$MAIN_WINDOW_PREFIX - Executing: $flow_graph.get_option('title')",
52                         icon="$(os.path.join($DATA_DIR, 'grc-icon-32.png'))",
53                 )
54 #elif $generate_options == 'no_gui'
55 class $(class_name)(gr.top_block):
56
57         def __init__($param_str):
58                 gr.top_block.__init__(self, "$flow_graph.get_option('title')")
59 #elif $generate_options == 'hb'
60         #set $in_sig = $flow_graph.get_input_signature()
61         #set $out_sig = $flow_graph.get_output_signature()
62 class $(class_name)(gr.hier_block2):
63
64         def __init__($param_str):
65                 gr.hier_block2.__init__(
66                         self,
67                         "$flow_graph.get_option('title')",
68                         gr.io_signature($in_sig.nports, $in_sig.nports, $in_sig.size*$in_sig.vlen),
69                         gr.io_signature($out_sig.nports, $out_sig.nports, $out_sig.size*$out_sig.vlen),
70                 )
71 #end if
72 ########################################################
73 ##Create Parameters
74 ##      Set the parameter to a property of self..
75 ########################################################
76 #if $parameters
77
78                 $DIVIDER
79                 # Parameters
80                 $DIVIDER
81 #end if
82 #for $param in $parameters
83                 self.$param.get_id() = $param.get_id()
84 #end for
85 ########################################################
86 ##Create Variables
87 ##      Set the variable to a property of self.
88 ##      Write the first line of the variable make.
89 ########################################################
90 #if $variables
91
92                 $DIVIDER
93                 # Variables
94                 $DIVIDER
95 #end if
96 #for $var in $variables
97         #set $code = $var.get_make().splitlines()[0]
98                 self.$var.get_id() = $var.get_id() = $code
99 #end for
100 ########################################################
101 ##Create Controls
102 ##      Write the variable make (excluding first line).
103 ##      Indent each line with 2 tabs.
104 ########################################################
105 #if $controls
106
107                 $DIVIDER
108                 # Controls
109                 $DIVIDER
110 #end if
111 #for $ctrl in $controls
112         #set $code = '\n\t\t'.join($ctrl.get_make().splitlines()[1:])
113                 $code
114 #end for
115 ########################################################
116 ##Create Blocks
117 ##      Write the block make, and indent with 2 tabs.
118 ########################################################
119 #if $blocks
120
121                 $DIVIDER
122                 # Blocks
123                 $DIVIDER
124 #end if
125 #for $blk in filter(lambda b: b.get_make(), $blocks)
126         #set $code = '\n\t\t'.join($blk.get_make().splitlines())
127                 $("self.%s = %s"%($blk.get_id(), $code))
128 #end for
129 ########################################################
130 ##Create Connections
131 ##      The port name should be the id of the parent block.
132 ##      However, port names for IO pads should be self.
133 ########################################################
134 #if $connections
135
136                 $DIVIDER
137                 # Connections
138                 $DIVIDER
139 #end if
140 #for $con in $connections
141         #set $source = $con.get_source()
142         #set $sink = $con.get_sink()
143         #if $source.get_parent().get_key() == 'pad_source'
144                 #set $source_name = 'self'
145         #else
146                 #set $source_name = 'self.' + $source.get_parent().get_id()
147         #end if
148         #if $sink.get_parent().get_key() == 'pad_sink'
149                 #set $sink_name = 'self'
150         #else
151                 #set $sink_name = 'self.' + $sink.get_parent().get_id()
152         #end if
153                 $("self.connect((%s, %s), (%s, %s))"%(
154                                 $source_name,
155                                 $source.get_key(),
156                                 $sink_name,
157                                 $sink.get_key(),
158                         )
159                 )
160 #end for
161
162 ########################################################
163 ##Create Callbacks
164 ##      Write a set method for this variable that calls the callbacks
165 ##      and sets the direct variable dependencies.
166 ########################################################
167 #for $var in $parameters + $variables
168         #set $id = $var.get_id()
169         def set_$(id)(self, $id):
170                 self.$id = $id
171         #for $dep in $var_id2deps[$id]
172                 self.set_$(dep)($var_id2expr[$dep])
173         #end for
174         #for $callback in $var_id2cbs[$id]
175                 self.$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         tb = $(class_name)()
187         #if $generate_options == 'wx_gui'
188         tb.Run()
189         #elif $generate_options == 'no_gui'
190         tb.start()
191         raw_input('Press Enter to quit: ')
192         tb.stop()
193         #end if
194 #end if
195