misc: use global evaluation dictionary when evaluating data sources (#16978)
This makes it possible to use "evalutils" functions and "complex" types.
This commit is contained in:
parent
ba4b56e7b2
commit
e3ab691c89
|
@ -104,6 +104,14 @@ def test_python_datasource():
|
|||
('foo', 'Foo', 'foo', {'id': 'foo', 'text': 'Foo'}),
|
||||
('bar', 'Bar', 'bar', {'id': 'bar', 'text': 'Bar', 'disabled': True})]
|
||||
|
||||
def test_python_datasource_with_evalutils():
|
||||
plain_list = [
|
||||
{'id': 'foo', 'text': 'Foo', 'value': '2017-01-01'},
|
||||
{'id': 'bar', 'text': 'Bar', 'value': '2015-01-01'}]
|
||||
datasource = {'type': 'formula', 'value': '[x for x in %s if date(x["value"]) > date("2016-01-01")]' % repr(plain_list)}
|
||||
assert data_sources.get_items(datasource) == [
|
||||
('foo', 'Foo', 'foo', {'id': 'foo', 'text': 'Foo', 'value': '2017-01-01'})]
|
||||
|
||||
def test_json_datasource():
|
||||
datasource = {'type': 'json', 'value': ''}
|
||||
assert data_sources.get_items(datasource) == []
|
||||
|
|
|
@ -116,9 +116,11 @@ def get_structured_items(data_source):
|
|||
# - three elements, (id, text, key)
|
||||
# - two elements, (id, text)
|
||||
# - a single element, (id,)
|
||||
vars = get_publisher().substitutions.get_context_variables()
|
||||
variables = get_publisher().substitutions.get_context_variables()
|
||||
global_eval_dict = get_publisher().get_global_eval_dict()
|
||||
global_eval_dict.update(data_source_functions)
|
||||
try:
|
||||
value = eval(data_source.get('value'), vars, data_source_functions)
|
||||
value = eval(data_source.get('value'), global_eval_dict, variables)
|
||||
if not isinstance(value, collections.Iterable):
|
||||
get_logger().warn('Python data source (%r) gave a non-iterable result' % \
|
||||
data_source.get('value'))
|
||||
|
|
Loading…
Reference in New Issue