Standarize API for inputs
D is data with raw passed values R is make_arr result for jinja we also pass flat input as kwargs
This commit is contained in:
parent
698e9f069f
commit
72762979da
@ -14,6 +14,7 @@
|
||||
|
||||
from jinja2.sandbox import SandboxedEnvironment
|
||||
from solar.computable_inputs import ComputableInputProcessor
|
||||
from solar.computable_inputs import ComputablePassedTypes
|
||||
|
||||
|
||||
def make_arr(data):
|
||||
@ -34,4 +35,13 @@ class JinjaProcessor(ComputableInputProcessor):
|
||||
|
||||
def run(self, resource_name, computable_type, funct, data):
|
||||
t = self.env.from_string(funct, globals=self._globals)
|
||||
return t.render(resource_name=resource_name, data=data).strip()
|
||||
if computable_type == ComputablePassedTypes.full.name:
|
||||
arr = make_arr(data)
|
||||
my_inputs = arr[resource_name]
|
||||
else:
|
||||
my_inputs = {}
|
||||
arr = {}
|
||||
return t.render(resource_name=resource_name,
|
||||
D=data,
|
||||
R=arr,
|
||||
**my_inputs).strip()
|
||||
|
@ -38,9 +38,9 @@ class LuaProcessor(ComputableInputProcessor):
|
||||
if not funct.startswith('function') \
|
||||
and not funct.endswith('end'):
|
||||
if computable_type == ComputablePassedTypes.full.name:
|
||||
make_arr = 'local res = make_arr(data)'
|
||||
make_arr = 'local R = make_arr(D)'
|
||||
funct = "%s\n%s" % (make_arr, funct)
|
||||
return 'function (data, resource_name) %s end' % funct
|
||||
return 'function (D, resource_name) %s end' % funct
|
||||
return funct
|
||||
|
||||
def run(self, resource_name, computable_type, funct, data):
|
||||
|
@ -19,6 +19,7 @@ import struct
|
||||
import subprocess
|
||||
|
||||
from solar.computable_inputs import ComputableInputProcessor
|
||||
from solar.computable_inputs import ComputablePassedTypes
|
||||
from solar.computable_inputs import HELPERS_PATH
|
||||
|
||||
|
||||
@ -93,17 +94,19 @@ class PyProcessor(ComputableInputProcessor):
|
||||
self.mgr = Mgr()
|
||||
self.mgr.run()
|
||||
|
||||
def check_funct(self, funct):
|
||||
def check_funct(self, funct, computable_type):
|
||||
if not funct.startswith('def calculate_input('):
|
||||
code = funct.splitlines()
|
||||
if computable_type == ComputablePassedTypes.full.name:
|
||||
code.insert(0, 'R = make_arr(D)')
|
||||
code = '\n '.join(code)
|
||||
return 'def calculate_input(data, resource_name):\n %s' % code
|
||||
return 'def calculate_input(D, resource_name):\n %s' % code
|
||||
return funct
|
||||
|
||||
def run(self, resource_name, computable_type, funct, data):
|
||||
funct = self.check_funct(funct)
|
||||
funct = self.check_funct(funct, computable_type)
|
||||
value = self.mgr.run_code(code=funct,
|
||||
fname='calculate_input',
|
||||
kwargs={'data': data,
|
||||
kwargs={'D': data,
|
||||
'resource_name': resource_name})
|
||||
return value
|
||||
|
@ -74,7 +74,7 @@ def test_lua_simple_lua_simple_max(rk):
|
||||
r2 = create_resource(k2, {'name': 'target1',
|
||||
'inputs': {'input1': None}})
|
||||
|
||||
lua_funct = 'return math.max(unpack(data))'
|
||||
lua_funct = 'return math.max(unpack(D))'
|
||||
r2.meta_inputs['input1']['computable'] = {'func': lua_funct,
|
||||
'lang': 'lua'}
|
||||
r1.connect(r2, {'input1': 'input1'})
|
||||
@ -100,7 +100,7 @@ def test_lua_full_lua_array(rk):
|
||||
'inputs': {'input1': None}})
|
||||
|
||||
# raw python object, counts from 0
|
||||
lua_funct = 'return data'
|
||||
lua_funct = 'return D'
|
||||
r2.meta_inputs['input1']['computable'] = {'func': lua_funct,
|
||||
'type': CPT.full.name,
|
||||
'lang': 'lua'}
|
||||
@ -134,7 +134,7 @@ def test_lua_connect_to_computed(rk):
|
||||
r4 = create_resource(k4, {'name': 'target1',
|
||||
'inputs': {'input1': None}})
|
||||
|
||||
lua_funct = 'return math.max(unpack(data))'
|
||||
lua_funct = 'return math.max(unpack(D))'
|
||||
r2.meta_inputs['input1']['computable'] = {'func': lua_funct,
|
||||
'lang': 'lua'}
|
||||
r1.connect(r2, {'input1': 'input1'})
|
||||
@ -166,7 +166,7 @@ def test_lua_join_different_values(rk):
|
||||
'inputs': {'input': None}})
|
||||
|
||||
lua_funct = """
|
||||
return res["r1"]["input1"] .. "@" .. res["r2"]["input2"]"""
|
||||
return R["r1"]["input1"] .. "@" .. R["r2"]["input2"]"""
|
||||
|
||||
r3.meta_inputs['input']['computable'] = {"func": lua_funct,
|
||||
'lang': 'lua',
|
||||
@ -203,14 +203,14 @@ def test_lua_join_replace_in_lua(rk):
|
||||
'inputs': {'input': None}})
|
||||
|
||||
lua_funct = """
|
||||
return res["r1"]["input1"] .. "@" .. res["r2"]["input2"]
|
||||
return R["r1"]["input1"] .. "@" .. R["r2"]["input2"]
|
||||
"""
|
||||
|
||||
r3.meta_inputs['input']['computable'] = {"func": lua_funct,
|
||||
'lang': 'lua',
|
||||
'type': CPT.full.name}
|
||||
|
||||
lua_funct2 = """local v = data[1]
|
||||
lua_funct2 = """local v = D[1]
|
||||
v = v:gsub("@", "-", 1)
|
||||
return v
|
||||
"""
|
||||
@ -243,7 +243,7 @@ def test_lua_join_self_computable(rk):
|
||||
'input3': None}})
|
||||
|
||||
lua_funct = """
|
||||
return resource_name .. res["r1"]["input2"] .. res["r1"]["input1"]
|
||||
return resource_name .. R["r1"]["input2"] .. R["r1"]["input1"]
|
||||
"""
|
||||
|
||||
r1.meta_inputs['input3']['computable'] = {'func': lua_funct,
|
||||
@ -265,8 +265,8 @@ def test_python_join_self_computable(rk):
|
||||
'inputs': {'input1': 'bar',
|
||||
'input2': 'foo',
|
||||
'input3': None}})
|
||||
py_funct = """l = make_arr(data)
|
||||
return resource_name + l["r1"]["input2"] + l["r1"]["input1"]
|
||||
py_funct = """
|
||||
return resource_name + R["r1"]["input2"] + R["r1"]["input1"]
|
||||
"""
|
||||
|
||||
r1.meta_inputs['input3']['computable'] = {'func': py_funct,
|
||||
@ -288,8 +288,8 @@ def test_jinja_join_self_computable(rk):
|
||||
'inputs': {'input1': 'bar',
|
||||
'input2': 'foo',
|
||||
'input3': None}})
|
||||
jinja_funct = """{% set l = make_arr(data) %}
|
||||
{{resource_name}}{{l['r1']['input2']}}{{l['r1']['input1']}}
|
||||
jinja_funct = """
|
||||
{{resource_name}}{{input2}}{{input1}}
|
||||
"""
|
||||
|
||||
r1.meta_inputs['input3']['computable'] = {'func': jinja_funct,
|
||||
@ -302,3 +302,49 @@ def test_jinja_join_self_computable(rk):
|
||||
r1.save()
|
||||
|
||||
assert r1.inputs['input3'] == 'r1foobar'
|
||||
|
||||
|
||||
def test_jinja_join_self_sum(rk):
|
||||
k1 = next(rk)
|
||||
|
||||
r1 = create_resource(k1, {'name': "r1",
|
||||
'inputs': {'input1': 3,
|
||||
'input2': 2,
|
||||
'input3': None}})
|
||||
jinja_funct = """
|
||||
{{[input1, input2]|sum}}
|
||||
"""
|
||||
|
||||
r1.meta_inputs['input3']['computable'] = {'func': jinja_funct,
|
||||
'lang': 'jinja2',
|
||||
'type': CPT.full.name}
|
||||
|
||||
r1.connect(r1, {'input1': 'input3'})
|
||||
r1.connect(r1, {'input2': 'input3'})
|
||||
|
||||
r1.save()
|
||||
|
||||
assert r1.inputs['input3'] == '5'
|
||||
|
||||
|
||||
def test_jinja_join_self_sum_simple(rk):
|
||||
k1 = next(rk)
|
||||
|
||||
r1 = create_resource(k1, {'name': "r1",
|
||||
'inputs': {'input1': 3,
|
||||
'input2': 2,
|
||||
'input3': None}})
|
||||
jinja_funct = """
|
||||
{{D|sum}}
|
||||
"""
|
||||
|
||||
r1.meta_inputs['input3']['computable'] = {'func': jinja_funct,
|
||||
'lang': 'jinja2',
|
||||
'type': CPT.values.name}
|
||||
|
||||
r1.connect(r1, {'input1': 'input3'})
|
||||
r1.connect(r1, {'input2': 'input3'})
|
||||
|
||||
r1.save()
|
||||
|
||||
assert r1.inputs['input3'] == '5'
|
||||
|
Loading…
x
Reference in New Issue
Block a user