90ef3bbaeb9929c4497765cb2acec5f6494d61c1
[debian/gnuradio] / grc / data / grc_gnuradio / 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 grc.Constants import MAIN_WINDOW_PREFIX, DATA_DIR
45 class $(class_name)(grc_wxgui.top_block_gui):
46
47         def __init__($param_str):
48                 grc_wxgui.top_block_gui.__init__(
49                         self,
50                         title="$MAIN_WINDOW_PREFIX - Executing: $flow_graph.get_option('title')",
51                         icon="$(os.path.join($DATA_DIR, 'grc-icon-32.png'))",
52                 )
53 #elif $generate_options == 'no_gui'
54 class $(class_name)(gr.top_block):
55
56         def __init__($param_str):
57                 gr.top_block.__init__(self, "$flow_graph.get_option('title')")
58 #elif $generate_options == 'hb'
59         #set $in_sig = $flow_graph.get_input_signature()
60         #set $out_sig = $flow_graph.get_output_signature()
61 class $(class_name)(gr.hier_block2):
62
63         def __init__($param_str):
64                 gr.hier_block2.__init__(
65                         self,
66                         "$flow_graph.get_option('title')",
67                         gr.io_signature($in_sig.nports, $in_sig.nports, $in_sig.size*$in_sig.vlen),
68                         gr.io_signature($out_sig.nports, $out_sig.nports, $out_sig.size*$out_sig.vlen),
69                 )
70 #end if
71 ########################################################
72 ##Create Parameters
73 ##      Set the parameter to a property of self..
74 ########################################################
75 #if $parameters
76
77                 $DIVIDER
78                 # Parameters
79                 $DIVIDER
80 #end if
81 #for $param in $parameters
82                 self.$param.get_id() = $param.get_id()
83 #end for
84 ########################################################
85 ##Create Variables
86 ##      Set the variable to a property of self.
87 ##      Write the first line of the variable make.
88 ########################################################
89 #if $variables
90
91                 $DIVIDER
92                 # Variables
93                 $DIVIDER
94 #end if
95 #for $var in $variables
96         #set $code = $var.get_make().splitlines()[0]
97                 self.$var.get_id() = $var.get_id() = $code
98 #end for
99 ########################################################
100 ##Create Controls
101 ##      Write the variable make (excluding first line).
102 ##      Indent each line with 2 tabs.
103 ########################################################
104 #if $controls
105
106                 $DIVIDER
107                 # Controls
108                 $DIVIDER
109 #end if
110 #for $ctrl in $controls
111         #set $code = '\n\t\t'.join($ctrl.get_make().splitlines()[1:])
112                 $code
113 #end for
114 ########################################################
115 ##Create Blocks
116 ##      Write the block make, and indent with 2 tabs.
117 ########################################################
118 #if $blocks
119
120                 $DIVIDER
121                 # Blocks
122                 $DIVIDER
123 #end if
124 #for $blk in filter(lambda b: b.get_make(), $blocks)
125         #set $code = '\n\t\t'.join($blk.get_make().splitlines())
126                 $("self.%s = %s"%($blk.get_id(), $code))
127 #end for
128 ########################################################
129 ##Create Connections
130 ##      The port name should be the id of the parent block.
131 ##      However, port names for IO pads should be self.
132 ########################################################
133 #if $connections
134
135                 $DIVIDER
136                 # Connections
137                 $DIVIDER
138 #end if
139 #for $con in $connections
140         #set $source = $con.get_source()
141         #set $sink = $con.get_sink()
142         #if $source.get_parent().get_key() == 'pad_source'
143                 #set $source_name = 'self'
144         #else
145                 #set $source_name = 'self.' + $source.get_parent().get_id()
146         #end if
147         #if $sink.get_parent().get_key() == 'pad_sink'
148                 #set $sink_name = 'self'
149         #else
150                 #set $sink_name = 'self.' + $sink.get_parent().get_id()
151         #end if
152                 $("self.connect((%s, %s), (%s, %s))"%(
153                                 $source_name,
154                                 $source.get_key(),
155                                 $sink_name,
156                                 $sink.get_key(),
157                         )
158                 )
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 $dep in $var_id2deps[$id]
171                 self.set_$(dep)($var_id2expr[$dep])
172         #end for
173         #for $callback in $var_id2cbs[$id]
174                 self.$callback
175         #end for
176
177 #end for
178 ########################################################
179 ##Create Main
180 ##      For top block code, generate a main routine.
181 ##      Instantiate the top block and run as gui or cli.
182 ########################################################
183 #if $generate_options != 'hb'
184 if __name__ == '__main__':
185         tb = $(class_name)()
186         #if $generate_options == 'wx_gui'
187         tb.Run()
188         #elif $generate_options == 'no_gui'
189         tb.start()
190         raw_input('Press Enter to quit: ')
191         tb.stop()
192         #end if
193 #end if
194