summaryrefslogtreecommitdiff
path: root/grc/python
diff options
context:
space:
mode:
Diffstat (limited to 'grc/python')
-rw-r--r--grc/python/Block.py14
-rw-r--r--grc/python/Connection.py9
-rw-r--r--grc/python/FlowGraph.py20
-rw-r--r--grc/python/Param.py54
-rw-r--r--grc/python/Platform.py9
-rw-r--r--grc/python/Port.py93
-rw-r--r--grc/python/flow_graph.tmpl44
7 files changed, 174 insertions, 69 deletions
diff --git a/grc/python/Block.py b/grc/python/Block.py
index 47fe13a3c..dd39b095d 100644
--- a/grc/python/Block.py
+++ b/grc/python/Block.py
@@ -18,10 +18,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
from .. base.Block import Block as _Block
+from .. gui.Block import Block as _GUIBlock
import extract_docs
import extract_category
-class Block(_Block):
+class Block(_Block, _GUIBlock):
+
+ def is_virtual_sink(self): return self.get_key() == 'virtual_sink'
+ def is_virtual_source(self): return self.get_key() == 'virtual_source'
##for make source to keep track of indexes
_source_count = 0
@@ -48,13 +52,13 @@ class Block(_Block):
flow_graph=flow_graph,
n=n,
)
+ _GUIBlock.__init__(self)
def validate(self):
"""
Validate this block.
Call the base class validate.
Evaluate the checks: each check must evaluate to True.
- Adjust the nports.
"""
_Block.validate(self)
#evaluate the checks
@@ -65,6 +69,12 @@ class Block(_Block):
try: assert check_eval
except AssertionError: self.add_error_message('Check "%s" failed.'%check)
except: self.add_error_message('Check "%s" did not evaluate.'%check)
+
+ def rewrite(self):
+ """
+ Add and remove ports to adjust for the nports.
+ """
+ _Block.rewrite(self)
#adjust nports
for get_ports, get_port in (
(self.get_sources, self.get_source),
diff --git a/grc/python/Connection.py b/grc/python/Connection.py
index 5eba9f24d..edc18841a 100644
--- a/grc/python/Connection.py
+++ b/grc/python/Connection.py
@@ -1,5 +1,5 @@
"""
-Copyright 2008 Free Software Foundation, Inc.
+Copyright 2008, 2009 Free Software Foundation, Inc.
This file is part of GNU Radio
GNU Radio Companion is free software; you can redistribute it and/or
@@ -18,8 +18,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
from .. base.Connection import Connection as _Connection
+from .. gui.Connection import Connection as _GUIConnection
-class Connection(_Connection):
+class Connection(_Connection, _GUIConnection):
+
+ def __init__(self, **kwargs):
+ _Connection.__init__(self, **kwargs)
+ _GUIConnection.__init__(self)
def is_msg(self):
return self.get_source().get_type() == self.get_sink().get_type() == 'msg'
diff --git a/grc/python/FlowGraph.py b/grc/python/FlowGraph.py
index 8cad8be49..6b2936c75 100644
--- a/grc/python/FlowGraph.py
+++ b/grc/python/FlowGraph.py
@@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import expr_utils
from .. base.FlowGraph import FlowGraph as _FlowGraph
+from .. gui.FlowGraph import FlowGraph as _GUIFlowGraph
from Block import Block
from Connection import Connection
import re
@@ -26,9 +27,13 @@ import re
_variable_matcher = re.compile('^(variable\w*)$')
_parameter_matcher = re.compile('^(parameter)$')
-class FlowGraph(_FlowGraph):
+class FlowGraph(_FlowGraph, _GUIFlowGraph):
+
+ def __init__(self, **kwargs):
+ _FlowGraph.__init__(self, **kwargs)
+ _GUIFlowGraph.__init__(self)
+ self._eval_cache = dict()
- _eval_cache = dict()
def _eval(self, code, namespace, namespace_hash):
"""
Evaluate the code with the given namespace.
@@ -109,6 +114,13 @@ class FlowGraph(_FlowGraph):
parameters = filter(lambda b: _parameter_matcher.match(b.get_key()), self.get_enabled_blocks())
return parameters
+ def rewrite(self):
+ """
+ Flag the namespace to be renewed.
+ """
+ self._renew_eval_ns = True
+ _FlowGraph.rewrite(self)
+
def evaluate(self, expr):
"""
Evaluate the expression.
@@ -116,8 +128,8 @@ class FlowGraph(_FlowGraph):
@throw Exception bad expression
@return the evaluated data
"""
- if self.is_flagged():
- self.deflag()
+ if self._renew_eval_ns:
+ self._renew_eval_ns = False
#reload namespace
n = dict()
#load imports
diff --git a/grc/python/Param.py b/grc/python/Param.py
index f971d0c3f..e61779136 100644
--- a/grc/python/Param.py
+++ b/grc/python/Param.py
@@ -18,7 +18,9 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
import expr_utils
-from .. base.Param import Param as _Param, EntryParam
+from .. base.Param import Param as _Param
+from .. gui.Param import Param as _GUIParam
+from .. gui.Param import EntryParam
import Constants
import numpy
import os
@@ -83,21 +85,24 @@ COMPLEX_TYPES = tuple(COMPLEX_TYPES + REAL_TYPES + INT_TYPES)
REAL_TYPES = tuple(REAL_TYPES + INT_TYPES)
INT_TYPES = tuple(INT_TYPES)
-class Param(_Param):
+class Param(_Param, _GUIParam):
- _init = False
- _hostage_cells = list()
+ def __init__(self, **kwargs):
+ _Param.__init__(self, **kwargs)
+ _GUIParam.__init__(self)
+ self._init = False
+ self._hostage_cells = list()
- ##possible param types
- TYPES = _Param.TYPES + [
+ def get_types(self): return (
+ 'raw', 'enum',
'complex', 'real', 'int',
'complex_vector', 'real_vector', 'int_vector',
'hex', 'string', 'bool',
'file_open', 'file_save',
- 'id',
+ 'id', 'stream_id',
'grid_pos', 'notebook',
'import',
- ]
+ )
def __repr__(self):
"""
@@ -150,7 +155,7 @@ class Param(_Param):
def get_input_class(self):
if self.get_type() in ('file_open', 'file_save'): return FileParam
- return _Param.get_input_class(self)
+ return _GUIParam.get_input_class(self)
def get_color(self):
"""
@@ -172,6 +177,7 @@ class Param(_Param):
'hex': Constants.INT_COLOR_SPEC,
'string': Constants.BYTE_VECTOR_COLOR_SPEC,
'id': Constants.ID_COLOR_SPEC,
+ 'stream_id': Constants.ID_COLOR_SPEC,
'grid_pos': Constants.INT_VECTOR_COLOR_SPEC,
'notebook': Constants.INT_VECTOR_COLOR_SPEC,
'raw': Constants.WILDCARD_COLOR_SPEC,
@@ -310,14 +316,31 @@ class Param(_Param):
#can python use this as a variable?
try: assert _check_id_matcher.match(v)
except AssertionError: raise Exception, 'ID "%s" must begin with a letter and may contain letters, numbers, and underscores.'%v
- params = self.get_all_params('id')
- keys = [param.get_value() for param in params]
- try: assert keys.count(v) <= 1 #id should only appear once, or zero times if block is disabled
+ ids = [param.get_value() for param in self.get_all_params(t)]
+ try: assert ids.count(v) <= 1 #id should only appear once, or zero times if block is disabled
except: raise Exception, 'ID "%s" is not unique.'%v
try: assert v not in ID_BLACKLIST
except: raise Exception, 'ID "%s" is blacklisted.'%v
return v
#########################
+ # Stream ID Type
+ #########################
+ elif t == 'stream_id':
+ #get a list of all stream ids used in the virtual sinks
+ ids = [param.get_value() for param in filter(
+ lambda p: p.get_parent().is_virtual_sink(),
+ self.get_all_params(t),
+ )]
+ #check that the virtual sink's stream id is unique
+ if self.get_parent().is_virtual_sink():
+ try: assert ids.count(v) <= 1 #id should only appear once, or zero times if block is disabled
+ except: raise Exception, 'Stream ID "%s" is not unique.'%v
+ #check that the virtual source's steam id is found
+ if self.get_parent().is_virtual_source():
+ try: assert v in ids
+ except: raise Exception, 'Stream ID "%s" is not found.'%v
+ return v
+ #########################
# Grid Position Type
#########################
elif t == 'grid_pos':
@@ -380,17 +403,18 @@ class Param(_Param):
def to_code(self):
"""
Convert the value to code.
+ For string and list types, check the init flag, call evaluate().
+ This ensures that evaluate() was called to set the xxxify_flags.
@return a string representing the code
"""
- #run init tasks in evaluate
- #such as setting flags
- if not self._init: self.evaluate()
v = self.get_value()
t = self.get_type()
if t in ('string', 'file_open', 'file_save'): #string types
+ if not self._init: self.evaluate()
if self._stringify_flag: return '"%s"'%v.replace('"', '\"')
else: return v
elif t in ('complex_vector', 'real_vector', 'int_vector'): #vector types
+ if not self._init: self.evaluate()
if self._lisitify_flag: return '(%s, )'%v
else: return '(%s)'%v
else: return v
diff --git a/grc/python/Platform.py b/grc/python/Platform.py
index d55dbf4ce..bb56d361b 100644
--- a/grc/python/Platform.py
+++ b/grc/python/Platform.py
@@ -20,10 +20,11 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
import os
from gnuradio import gr
from .. base.Platform import Platform as _Platform
+from .. gui.Platform import Platform as _GUIPlatform
from FlowGraph import FlowGraph as _FlowGraph
from Connection import Connection as _Connection
from Block import Block as _Block
-from Port import Source,Sink
+from Port import Port as _Port
from Param import Param as _Param
from Generator import Generator
from Constants import \
@@ -46,7 +47,7 @@ COLORS = (#title, #color spec
('Message', Constants.MSG_COLOR_SPEC),
)
-class Platform(_Platform):
+class Platform(_Platform, _GUIPlatform):
def __init__(self):
"""
@@ -70,6 +71,7 @@ class Platform(_Platform):
generator=Generator,
colors=COLORS,
)
+ _GUIPlatform.__init__(self)
##############################################
# Constructors
@@ -77,6 +79,5 @@ class Platform(_Platform):
FlowGraph = _FlowGraph
Connection = _Connection
Block = _Block
- Source = Source
- Sink = Sink
+ Port = _Port
Param = _Param
diff --git a/grc/python/Port.py b/grc/python/Port.py
index daf8f9ca3..33426d905 100644
--- a/grc/python/Port.py
+++ b/grc/python/Port.py
@@ -18,41 +18,100 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
"""
from .. base.Port import Port as _Port
+from .. gui.Port import Port as _GUIPort
import Constants
-class Port(_Port):
-
- ##possible port types
- TYPES = ['complex', 'float', 'int', 'short', 'byte', 'msg']
-
- def __init__(self, block, n):
+def _get_source_from_virtual_sink_port(vsp):
+ """
+ Resolve the source port that is connected to the given virtual sink port.
+ Use the get source from virtual source to recursively resolve subsequent ports.
+ """
+ try: return _get_source_from_virtual_source_port(
+ vsp.get_enabled_connections()[0].get_source())
+ except: raise Exception, 'Could not resolve source for virtual sink port %s'%vsp
+
+def _get_source_from_virtual_source_port(vsp, traversed=[]):
+ """
+ Recursively resolve source ports over the virtual connections.
+ Keep track of traversed sources to avoid recursive loops.
+ """
+ if not vsp.get_parent().is_virtual_source(): return vsp
+ if vsp in traversed: raise Exception, 'Loop found when resolving virtual source %s'%vsp
+ try: return _get_source_from_virtual_source_port(
+ _get_source_from_virtual_sink_port(
+ filter(#get all virtual sinks with a matching stream id
+ lambda vs: vs.get_param('stream_id').get_value() == vsp.get_parent().get_param('stream_id').get_value(),
+ filter(#get all enabled blocks that are also virtual sinks
+ lambda b: b.is_virtual_sink(),
+ vsp.get_parent().get_parent().get_enabled_blocks(),
+ ),
+ )[0].get_sink(vsp.get_key())
+ ), traversed + [vsp],
+ )
+ except: raise Exception, 'Could not resolve source for virtual source port %s'%vsp
+
+class Port(_Port, _GUIPort):
+
+ def __init__(self, block, n, dir):
"""
Make a new port from nested data.
@param block the parent element
@param n the nested odict
+ @param dir the direction
"""
+ self._n = n
+ if n['type'] == 'msg': n['key'] = 'msg'
+ if dir == 'source' and not n.find('key'):
+ n['key'] = str(block._source_count)
+ block._source_count += 1
+ if dir == 'sink' and not n.find('key'):
+ n['key'] = str(block._sink_count)
+ block._sink_count += 1
#build the port
_Port.__init__(
self,
block=block,
n=n,
+ dir=dir,
)
+ _GUIPort.__init__(self)
self._nports = n.find('nports') or ''
self._vlen = n.find('vlen') or ''
self._optional = bool(n.find('optional'))
+ def get_types(self): return ('complex', 'float', 'int', 'short', 'byte', 'msg', '')
+
def validate(self):
_Port.validate(self)
try: assert self.get_enabled_connections() or self.get_optional()
except AssertionError: self.add_error_message('Port is not connected.')
try: assert self.is_source() or len(self.get_enabled_connections()) <= 1
except AssertionError: self.add_error_message('Port has too many connections.')
+ #message port logic
if self.get_type() == 'msg':
try: assert not self.get_nports()
except AssertionError: self.add_error_message('A port of type "msg" cannot have "nports" set.')
try: assert self.get_vlen() == 1
except AssertionError: self.add_error_message('A port of type "msg" must have a "vlen" of 1.')
+ def rewrite(self):
+ """
+ Handle the port cloning for virtual blocks.
+ """
+ _Port.rewrite(self)
+ if self.get_parent().is_virtual_sink() or self.get_parent().is_virtual_source():
+ try: #clone type and vlen
+ source = self.resolve_virtual_source()
+ self._type = str(source.get_type())
+ self._vlen = str(source.get_vlen())
+ except: #reset type and vlen
+ self._type = ''
+ self._vlen = ''
+
+ def resolve_virtual_source(self):
+ if self.get_parent().is_virtual_sink(): return _get_source_from_virtual_sink_port(self)
+ if self.get_parent().is_virtual_source(): return _get_source_from_virtual_source_port(self)
+
def get_vlen(self):
"""
Get the vector length.
@@ -109,24 +168,4 @@ class Port(_Port):
def copy(self, new_key=None):
n = self._n.copy()
if new_key: n['key'] = new_key
- return self.__class__(self.get_parent(), n)
-
-class Source(Port):
-
- def __init__(self, block, n):
- self._n = n #save n
- if n['type'] == 'msg': n['key'] = 'msg'
- if not n.find('key'):
- n['key'] = str(block._source_count)
- block._source_count = block._source_count + 1
- Port.__init__(self, block, n)
-
-class Sink(Port):
-
- def __init__(self, block, n):
- self._n = n #save n
- if n['type'] == 'msg': n['key'] = 'msg'
- if not n.find('key'):
- n['key'] = str(block._sink_count)
- block._sink_count = block._sink_count + 1
- Port.__init__(self, block, n)
+ return self.__class__(self.get_parent(), n, self._dir)
diff --git a/grc/python/flow_graph.tmpl b/grc/python/flow_graph.tmpl
index df346dd16..dce4037d5 100644
--- a/grc/python/flow_graph.tmpl
+++ b/grc/python/flow_graph.tmpl
@@ -154,6 +154,13 @@ class $(class_name)(gr.hier_block2):
## The port name should be the id of the parent block.
## However, port names for IO pads should be self.
########################################################
+#def make_port_name($port)
+ #if $port.get_parent().get_key().startswith('pad_')
+self#slurp
+ #else
+self.$port.get_parent().get_id()#slurp
+ #end if
+#end def
#if $connections
$DIVIDER
@@ -163,17 +170,14 @@ class $(class_name)(gr.hier_block2):
#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()
+ ##resolve virtual sources to the actual sources
+ #if $source.get_parent().is_virtual_source()
+ #set $source = $source.resolve_virtual_source()
#end if
- #if $sink.get_parent().get_key() == 'pad_sink'
- #set $sink_name = 'self'
- #else
- #set $sink_name = 'self.' + $sink.get_parent().get_id()
+ ##do not generate connections with virtual sinks
+ #if not $sink.get_parent().is_virtual_sink()
+ self.connect(($make_port_name($source), $source.get_key()), ($make_port_name($sink), $sink.get_key()))
#end if
- self.connect(($source_name, $source.get_key()), ($sink_name, $sink.get_key()))
#end for
########################################################
@@ -194,6 +198,20 @@ class $(class_name)(gr.hier_block2):
## For top block code, generate a main routine.
## Instantiate the top block and run as gui or cli.
########################################################
+#def make_default($type, $param)
+ #if $type == 'eng_float'
+eng_notation.num_to_str($param.get_make())#slurp
+ #else
+$param.get_make()#slurp
+ #end if
+#end def
+#def make_short_id($param)
+ #set $short_id = $param.get_param('short_id').get_evaluated()
+ #if $short_id
+ #set $short_id = '-' + $short_id
+ #end if
+$short_id#slurp
+#end def
#if $generate_options != 'hb'
if __name__ == '__main__':
parser = OptionParser(option_class=eng_option, usage="%prog: [options]")
@@ -202,12 +220,8 @@ if __name__ == '__main__':
#set $type = $param.get_param('type').get_value()
#if $type
#silent $params_eq_list.append('%s=options.%s'%($param.get_id(), $param.get_id()))
- #set $short_id = $param.get_param('short_id').get_evaluated()
- #if $short_id
- #set $short_id = '-' + $short_id
- #end if
- parser.add_option("$short_id", "--$param.get_id()", dest="$param.get_id()", type="$type", default=$param.get_make(),
- help="Set $($param.get_param('label').evaluate() or $param.get_id()) [default=%default]")
+ parser.add_option("$make_short_id($param)", "--$param.get_id().replace('_', '-')", dest="$param.get_id()", type="$type", default=$make_default($type, $param),
+ help="Set $($param.get_param('label').get_evaluated() or $param.get_id()) [default=%default]")
#end if
#end for
(options, args) = parser.parse_args()