Projet

Général

Profil

0001-misc-use-global-evaluation-dictionary-when-evaluatin.patch

Frédéric Péters, 23 décembre 2017 14:07

Télécharger (2,39 ko)

Voir les différences:

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(-)
tests/test_datasource.py
108 108
            ('foo', 'Foo', 'foo', {'id': 'foo', 'text': 'Foo'}),
109 109
            ('bar', 'Bar', 'bar', {'id': 'bar', 'text': 'Bar', 'disabled': True})]
110 110

  
111
def test_python_datasource_with_evalutils():
112
    plain_list = [
113
        {'id': 'foo', 'text': 'Foo', 'value': '2017-01-01'},
114
        {'id': 'bar', 'text': 'Bar', 'value': '2015-01-01'}]
115
    datasource = {'type': 'formula', 'value': '[x for x in %s if date(x["value"]) > date("2016-01-01")]' % repr(plain_list)}
116
    assert data_sources.get_items(datasource) == [
117
            ('foo', 'Foo', 'foo', {'id': 'foo', 'text': 'Foo', 'value': '2017-01-01'})]
118

  
111 119
def test_json_datasource(http_requests):
112 120
    datasource = {'type': 'json', 'value': ''}
113 121
    assert data_sources.get_items(datasource) == []
wcs/data_sources.py
115 115
        #   - three elements, (id, text, key)
116 116
        #   - two elements, (id, text)
117 117
        #   - a single element, (id,)
118
        vars = get_publisher().substitutions.get_context_variables()
118
        variables = get_publisher().substitutions.get_context_variables()
119
        global_eval_dict = get_publisher().get_global_eval_dict()
120
        global_eval_dict.update(data_source_functions)
119 121
        try:
120
            value = eval(data_source.get('value'), vars, data_source_functions)
122
            value = eval(data_source.get('value'), global_eval_dict, variables)
121 123
            if not isinstance(value, collections.Iterable):
122 124
                get_logger().warn('Python data source (%r) gave a non-iterable result' % \
123 125
                                data_source.get('value'))
124
-