|  | 
|  | 1 | +# SPDX-FileCopyrightText: 2023 The meson-python developers | 
|  | 2 | +# | 
|  | 3 | +# SPDX-License-Identifier: MIT | 
|  | 4 | + | 
|  | 5 | +from __future__ import annotations | 
|  | 6 | + | 
|  | 7 | +import ast | 
|  | 8 | +import operator | 
|  | 9 | +import typing | 
|  | 10 | + | 
|  | 11 | + | 
|  | 12 | +if typing.TYPE_CHECKING:  # pragma: no cover | 
|  | 13 | +    from typing import Any, Callable, Mapping, Optional, Type | 
|  | 14 | + | 
|  | 15 | + | 
|  | 16 | +_methods = {} | 
|  | 17 | + | 
|  | 18 | + | 
|  | 19 | +def _register(nodetype: Type[ast.AST]) -> Callable[..., Callable[..., Any]]: | 
|  | 20 | +    def closure(method: Callable[[Interpreter, ast.AST], Any]) -> Callable[[Interpreter, ast.AST], Any]: | 
|  | 21 | +        _methods[nodetype] = method | 
|  | 22 | +        return method | 
|  | 23 | +    return closure | 
|  | 24 | + | 
|  | 25 | + | 
|  | 26 | +class Interpreter: | 
|  | 27 | + | 
|  | 28 | +    _operators = { | 
|  | 29 | +        ast.Add: operator.add, | 
|  | 30 | +        ast.Sub: operator.sub, | 
|  | 31 | +        ast.Mult: operator.mul, | 
|  | 32 | +        ast.Div: operator.truediv, | 
|  | 33 | +    } | 
|  | 34 | + | 
|  | 35 | +    def __init__(self, variables: Mapping[str, Any]): | 
|  | 36 | +        self._variables = variables | 
|  | 37 | + | 
|  | 38 | +    def eval(self, string: str) -> Any: | 
|  | 39 | +        try: | 
|  | 40 | +            expr = ast.parse(string, mode='eval') | 
|  | 41 | +            return self._eval(expr) | 
|  | 42 | +        except KeyError as exc: | 
|  | 43 | +            raise ValueError(f'unknown variable "{exc.args[0]}"') from exc | 
|  | 44 | +        except NotImplementedError as exc: | 
|  | 45 | +            raise ValueError(f'invalid expression {string!r}') from exc | 
|  | 46 | + | 
|  | 47 | +    __getitem__ = eval | 
|  | 48 | + | 
|  | 49 | +    def _eval(self, node: ast.AST) -> Any: | 
|  | 50 | +        # Cannot use functools.singlemethoddispatch as long as Python 3.7 is supported. | 
|  | 51 | +        method = _methods.get(type(node), None) | 
|  | 52 | +        if method is None: | 
|  | 53 | +            raise NotImplementedError | 
|  | 54 | +        return method(self, node) | 
|  | 55 | + | 
|  | 56 | +    @_register(ast.Expression) | 
|  | 57 | +    def _expression(self, node: ast.Expression) -> Any: | 
|  | 58 | +        return self._eval(node.body) | 
|  | 59 | + | 
|  | 60 | +    @_register(ast.BinOp) | 
|  | 61 | +    def _binop(self, node: ast.BinOp) -> Any: | 
|  | 62 | +        func = self._operators.get(type(node.op)) | 
|  | 63 | +        if func is None: | 
|  | 64 | +            raise NotImplementedError | 
|  | 65 | +        return func(self._eval(node.left), self._eval(node.right)) | 
|  | 66 | + | 
|  | 67 | +    @_register(ast.Constant) | 
|  | 68 | +    def _constant(self, node: ast.Constant) -> Any: | 
|  | 69 | +        return node.value | 
|  | 70 | + | 
|  | 71 | +    # Python 3.7, replaced by ast.Constant is later versions. | 
|  | 72 | +    @_register(ast.Num) | 
|  | 73 | +    def _num(self, node: ast.Num) -> Any: | 
|  | 74 | +        return node.n | 
|  | 75 | + | 
|  | 76 | +    # Python 3.7, replaced by ast.Constant is later versions. | 
|  | 77 | +    @_register(ast.Str) | 
|  | 78 | +    def _str(self, node: ast.Str) -> Any: | 
|  | 79 | +        return node.s | 
|  | 80 | + | 
|  | 81 | +    @_register(ast.Name) | 
|  | 82 | +    def _variable(self, node: ast.Name) -> Any: | 
|  | 83 | +        value = self._variables[node.id] | 
|  | 84 | +        if callable(value): | 
|  | 85 | +            value = value() | 
|  | 86 | +        return value | 
|  | 87 | + | 
|  | 88 | + | 
|  | 89 | +def _ncores() -> int: | 
|  | 90 | +    return 42 | 
|  | 91 | + | 
|  | 92 | + | 
|  | 93 | +def interpolate(string: str, variables: Optional[Mapping[str, Any]] = None) -> str: | 
|  | 94 | +    if variables is None: | 
|  | 95 | +        variables = {'ncores': _ncores} | 
|  | 96 | +    return string % Interpreter(variables) | 
0 commit comments