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