Merging r11186:11273 from grc branch.
[debian/gnuradio] / grc / python / flow_graph.tmpl
diff --git a/grc/python/flow_graph.tmpl b/grc/python/flow_graph.tmpl
new file mode 100644 (file)
index 0000000..a45a918
--- /dev/null
@@ -0,0 +1,205 @@
+#!/usr/bin/env python
+########################################################
+##Cheetah template - gnuradio_python
+##
+##@param imports the import statements
+##@param flow_graph the flow_graph
+##@param variables the variable blocks
+##@param controls the variables with gui controls
+##@param parameters the paramater blocks
+##@param blocks the signal blocks
+##@param connections the connections
+##@param generate_options the type of flow graph
+##@param var_id2cbs variable id map to callback strings
+########################################################
+#def indent($code)
+#set $code = '\n\t\t'.join(str($code).splitlines())
+$code#slurp
+#end def
+#import time
+#set $DIVIDER = '#'*50
+$DIVIDER
+# Gnuradio Python Flow Graph
+# Title: $title
+#if $flow_graph.get_option('author')
+# Author: $flow_graph.get_option('author')
+#end if
+#if $flow_graph.get_option('description')
+# Description: $flow_graph.get_option('description')
+#end if
+# Generated: $time.ctime()
+$DIVIDER
+
+########################################################
+##Create Imports
+########################################################
+#for $imp in $imports
+$imp
+#end for
+
+########################################################
+##Create Class
+##     Write the class declaration for a top or hier block.
+##     The parameter names are the arguments to __init__.
+##     Determine the absolute icon path (wx gui only).
+##     Setup the IO signature (hier block only).
+########################################################
+#set $class_name = $flow_graph.get_option('id')
+#set $param_str = ', '.join(['self'] + ['%s=%s'%(param.get_id(), param.get_make()) for param in $parameters])
+#if $generate_options == 'wx_gui'
+       #import gtk
+       #set $icon = gtk.IconTheme().lookup_icon('gnuradio-grc', 32, 0)
+class $(class_name)(grc_wxgui.top_block_gui):
+
+       def __init__($param_str):
+               grc_wxgui.top_block_gui.__init__(
+                       self,
+                       title="$title",
+       #if $icon
+                       icon="$icon.get_filename()",
+       #end if
+               )
+#elif $generate_options == 'no_gui'
+class $(class_name)(gr.top_block):
+
+       def __init__($param_str):
+               gr.top_block.__init__(self, "$title")
+#elif $generate_options == 'hb'
+       #set $in_sig = $flow_graph.get_input_signature()
+       #set $out_sig = $flow_graph.get_output_signature()
+class $(class_name)(gr.hier_block2):
+
+       def __init__($param_str):
+               gr.hier_block2.__init__(
+                       self,
+                       "$title",
+                       gr.io_signature($in_sig.nports, $in_sig.nports, $in_sig.size*$in_sig.vlen),
+                       gr.io_signature($out_sig.nports, $out_sig.nports, $out_sig.size*$out_sig.vlen),
+               )
+#end if
+########################################################
+##Create Parameters
+##     Set the parameter to a property of self.
+########################################################
+#if $parameters
+
+               $DIVIDER
+               # Parameters
+               $DIVIDER
+#end if
+#for $param in $parameters
+               $indent($param.get_var_make())
+#end for
+########################################################
+##Create Variables
+##     Set the variable to a property of self.
+##     Write the first line of the variable make.
+########################################################
+#if $variables
+
+               $DIVIDER
+               # Variables
+               $DIVIDER
+#end if
+#for $var in $variables
+               $indent($var.get_var_make())
+#end for
+########################################################
+##Create Controls
+##     Write the variable make (excluding first line).
+##     Indent each line with 2 tabs.
+########################################################
+#if $controls
+
+               $DIVIDER
+               # Controls
+               $DIVIDER
+#end if
+#for $ctrl in $controls
+               $indent($ctrl.get_make())
+#end for
+########################################################
+##Create Blocks
+##     Write the block make, and indent with 2 tabs.
+########################################################
+#if $blocks
+
+               $DIVIDER
+               # Blocks
+               $DIVIDER
+#end if
+#for $blk in filter(lambda b: b.get_make(), $blocks)
+               self.$blk.get_id() = $indent($blk.get_make())
+#end for
+########################################################
+##Create Connections
+##     The port name should be the id of the parent block.
+##     However, port names for IO pads should be self.
+########################################################
+#if $connections
+
+               $DIVIDER
+               # Connections
+               $DIVIDER
+#end if
+#for $con in $connections
+       #set $source = $con.get_source()
+       #set $sink = $con.get_sink()
+       #if $source.get_parent().get_key() == 'pad_source'
+               #set $source_name = 'self'
+       #else
+               #set $source_name = 'self.' + $source.get_parent().get_id()
+       #end if
+       #if $sink.get_parent().get_key() == 'pad_sink'
+               #set $sink_name = 'self'
+       #else
+               #set $sink_name = 'self.' + $sink.get_parent().get_id()
+       #end if
+               self.connect(($source_name, $source.get_key()), ($sink_name, $sink.get_key()))
+#end for
+
+########################################################
+##Create Callbacks
+##     Write a set method for this variable that calls the callbacks
+##     and sets the direct variable dependencies.
+########################################################
+#for $var in $parameters + $variables
+       #set $id = $var.get_id()
+       def set_$(id)(self, $id):
+               self.$id = $id
+       #for $callback in $var_id2cbs[$id]
+               $indent($callback)
+       #end for
+
+#end for
+########################################################
+##Create Main
+##     For top block code, generate a main routine.
+##     Instantiate the top block and run as gui or cli.
+########################################################
+#if $generate_options != 'hb'
+if __name__ == '__main__':
+       parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
+       #set $params_eq_list = list()
+       #for $param in $parameters
+               #set $type = $param.get_param('type').get_value()
+               #if $type
+                       #silent $params_eq_list.append('%s=options.%s'%($param.get_id(), $param.get_id()))
+       parser.add_option("--$param.get_id()", dest="$param.get_id()", type="$type", default=$param.get_make())
+               #end if
+       #end for
+       (options, args) = parser.parse_args()
+       #if $flow_graph.get_option('realtime_scheduling')
+       if gr.enable_realtime_scheduling() != gr.RT_OK:
+               print "Error: failed to enable realtime scheduling."
+       #end if
+       tb = $(class_name)($(', '.join($params_eq_list)))
+       #if $generate_options == 'wx_gui'
+       tb.Run($flow_graph.get_option('autostart'))
+       #elif $generate_options == 'no_gui'
+       tb.start()
+       raw_input('Press Enter to quit: ')
+       tb.stop()
+       #end if
+#end if
+