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