From 4cbd96ddd202007ee76247d581e759c04528ef0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Sat, 29 Jul 2017 14:39:33 +0200 Subject: [PATCH] misc: use global evaluation dictionary when evaluating data sources (#16978) This makes it possible to use "evalutils" functions and "complex" types. --- tests/test_datasource.py | 8 ++++++++ wcs/data_sources.py | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_datasource.py b/tests/test_datasource.py index 85b2e3c3..f0c56124 100644 --- a/tests/test_datasource.py +++ b/tests/test_datasource.py @@ -108,6 +108,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(http_requests): datasource = {'type': 'json', 'value': ''} assert data_sources.get_items(datasource) == [] diff --git a/wcs/data_sources.py b/wcs/data_sources.py index b063813e..f54efce3 100644 --- a/wcs/data_sources.py +++ b/wcs/data_sources.py @@ -115,9 +115,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')) -- 2.15.1