+# Decorators
+class LeafDecorator():
+ def __init__(self, container_name):
+ self.container_name = container_name
+
+ def __call__(self, f):
+ if this_module._exec_mode == this_module._default_mode:
+ return f
+ co = f.__code__
+ import py2yacs
+ props = py2yacs.function_properties(co.co_filename, co.co_name)
+ nodeType = LeafNodeType(co.co_filename, co.co_name,
+ props.inputs, props.outputs, self.container_name)
+ def my_func(*args, **kwargs):
+ if len(args) + len(kwargs) != len(nodeType.inputs):
+ mes = "Wrong number of arguments when calling function '{}'.\n".format(
+ nodeType.fn_name)
+ mes += " {} arguments expected and {} arguments found.\n".format(
+ len(nodeType.inputs), len(args) + len(kwargs))
+ raise Exception(mes)
+ idx = 0
+ args_dic = {}
+ for a in args:
+ args_dic[nodeType.inputs[idx]] = a
+ idx += 1
+ for k,v in kwargs.items():
+ args_dic[k] = v
+ if len(args_dic) != len(nodeType.inputs):
+ mes="Wrong arguments when calling function {}.\n".format(nodeType.fn_name)
+ raise Exception(mes)
+ return nodeType.createNewNode(args_dic)
+ return my_func
+
+def leaf(arg):
+ """
+ Decorator for python scripts.
+ """
+ if callable(arg):
+ # decorator used without parameters. arg is the function
+ container = ContainerManager.defaultContainerName
+ ret = (LeafDecorator(container))(arg)
+ else:
+ # decorator used with parameter. arg is the container name
+ ret = LeafDecorator(arg)
+ return ret
+
+def bloc(f):
+ """
+ Decorator for blocs.
+ """
+ #co = f.__code__
+ #print("bloc :", co.co_name)
+ #print(" file:", co.co_filename)
+ #print(" line:", co.co_firstlineno)
+ #print(" args:", co.co_varnames)
+ return f
+
+def default_foreach(f):
+ def my_func(lst):
+ result = []
+ for e in lst:
+ result.append(f(e))
+ t_result = result
+ if len(result) > 0 :
+ if type(result[0]) is tuple:
+ # transform the list of tuples in a tuple of lists
+ l_result = []
+ for e in result[0]:
+ l_result.append([])
+ for t in result:
+ idx = 0
+ for e in t:
+ l_result[idx].append(e)
+ idx += 1
+ t_result = tuple(l_result)
+ return t_result
+ return my_func
+
+def yacs_foreach(f):
+ #co = f.__code__
+ #import yacsvisit
+ #props = yacsvisit.main(co.co_filename, co.co_name)
+ def my_func(input_list):
+ fn_name = f.__code__.co_name
+ generator = getGenerator()
+ sample_port = generator.beginForeach(fn_name, input_list)
+ output_list = f(sample_port)
+ output_list = generator.endForeach(output_list)
+ return output_list
+ return my_func
+
+def foreach(f):
+ """
+ Decorator to generate foreach blocs
+ """
+ if this_module._exec_mode == this_module._default_mode:
+ return default_foreach(f)
+ elif this_module._exec_mode == this_module._yacs_mode:
+ return yacs_foreach(f)
+