File: runner.py

package info (click to toggle)
python-yaql 2.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: python: 7,765; sh: 25; makefile: 19
file content (186 lines) | stat: -rw-r--r-- 6,400 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#    Copyright (c) 2015 Mirantis, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

# flake8: noqa: E731

import sys

from yaql.language import exceptions
from yaql.language import expressions
from yaql.language import utils
from yaql.language import yaqltypes


def call(name, context, args, kwargs, engine, receiver=utils.NO_VALUE,
         data_context=None, use_convention=False, function_filter=None):

    if data_context is None:
        data_context = context

    if function_filter is None:
        function_filter = lambda fd, ctx: True

    if receiver is utils.NO_VALUE:
        predicate = lambda fd, ctx: fd.is_function and function_filter(fd, ctx)
    else:
        predicate = lambda fd, ctx: fd.is_method and function_filter(fd, ctx)

    all_overloads = context.collect_functions(
        name, predicate, use_convention=use_convention)

    if not all_overloads:
        if receiver is utils.NO_VALUE:
            raise exceptions.NoFunctionRegisteredException(name)
        else:
            raise exceptions.NoMethodRegisteredException(name, receiver)
    else:
        delegate = choose_overload(
            name, all_overloads, engine, receiver, data_context, args, kwargs)
        try:
            result = delegate()
            utils.limit_memory_usage(engine, (1, result))
            return result
        except StopIteration as e:
            raise exceptions.WrappedException(e).with_traceback(
                sys.exc_info()[2])


def choose_overload(name, candidates, engine, receiver, context, args, kwargs):
    def raise_ambiguous():
        if receiver is utils.NO_VALUE:
            raise exceptions.AmbiguousFunctionException(name)
        else:
            raise exceptions.AmbiguousMethodException(name, receiver)

    def raise_not_found():
        if receiver is utils.NO_VALUE:
            raise exceptions.NoMatchingFunctionException(name)
        else:
            raise exceptions.NoMatchingMethodException(name, receiver)

    candidates2 = []
    lazy_params = None
    no_kwargs = None
    if receiver is not utils.NO_VALUE:
        args = (receiver,) + args
    for level in candidates:
        new_level = []
        for c in level:
            if no_kwargs is None:
                no_kwargs = c.no_kwargs
                args, kwargs = translate_args(no_kwargs, args, kwargs)
            elif no_kwargs != c.no_kwargs:
                raise_ambiguous()

            mapping = c.map_args(args, kwargs, context, engine)
            if mapping is None:
                continue
            pos, kwd = mapping
            lazy = set()
            for i, pos_arg in enumerate(pos):
                if isinstance(pos_arg.value_type, yaqltypes.LazyParameterType):
                    lazy.add(i)
            for key, value in kwd.items():
                if isinstance(value.value_type, yaqltypes.LazyParameterType):
                    lazy.add(key)
            if lazy_params is None:
                lazy_params = lazy
            elif lazy_params != lazy:
                raise_ambiguous()
            new_level.append((c, mapping))
        if new_level:
            candidates2.append(new_level)

    if len(candidates2) == 0:
        raise_not_found()

    arg_evaluator = lambda i, arg: (  # noqa: E731
        arg(utils.NO_VALUE, context, engine)
        if (i not in lazy_params and isinstance(arg, expressions.Expression)
            and not isinstance(arg, expressions.Constant))
        else arg
    )

    args = tuple(arg_evaluator(i, arg) for i, arg in enumerate(args))
    for key, value in kwargs.items():
        kwargs[key] = arg_evaluator(key, value)

    delegate = None
    winner_mapping = None
    for level in candidates2:
        for c, mapping in level:
            try:
                d = c.get_delegate(receiver, engine, context, args, kwargs)
            except exceptions.ArgumentException:
                pass
            else:
                if delegate is not None:
                    if _is_specialization_of(winner_mapping, mapping):
                        continue
                    elif not _is_specialization_of(mapping, winner_mapping):
                        raise_ambiguous()
                delegate = d
                winner_mapping = mapping
        if delegate is not None:
            break

    if delegate is None:
        raise_not_found()
    return lambda: delegate()


def translate_args(without_kwargs, args, kwargs):
    if without_kwargs:
        if len(kwargs) > 0:
            raise exceptions.ArgumentException(next(iter(kwargs)))
        return args, {}
    pos_args = []
    kw_args = {}
    for t in args:
        if isinstance(t, expressions.MappingRuleExpression):
            param_name = t.source
            if isinstance(param_name, expressions.KeywordConstant):
                param_name = param_name.value
            else:
                raise exceptions.MappingTranslationException()
            kw_args[param_name] = t.destination
        else:
            pos_args.append(t)
    for key, value in kwargs.items():
        if key in kw_args:
            raise exceptions.MappingTranslationException()
        else:
            kw_args[key] = value
    return tuple(pos_args), kw_args


def _is_specialization_of(mapping1, mapping2):
    args_mapping1, kwargs_mapping1 = mapping1
    args_mapping2, kwargs_mapping2 = mapping2
    res = False

    for a1, a2 in zip(args_mapping1, args_mapping2):
        if a2.value_type.is_specialization_of(a1.value_type):
            return False
        elif a1.value_type.is_specialization_of(a2.value_type):
            res = True

    for key, a1 in kwargs_mapping1.items():
        a2 = kwargs_mapping2[key]
        if a2.value_type.is_specialization_of(a1.value_type):
            return False
        elif a1.value_type.is_specialization_of(a2.value_type):
            res = True

    return res