diff options
author | Xavier Claessens <xavier.claessens@collabora.com> | 2023-05-30 09:27:42 -0400 |
---|---|---|
committer | Xavier Claessens <xclaesse@gmail.com> | 2023-08-02 13:35:29 -0400 |
commit | cec3edc08a6cd6a89761c49292ba6a3bace8b3c1 (patch) | |
tree | ca8f4504ead8fe875ef582299b2cbb9f07d85688 /mesonbuild/interpreterbase | |
parent | 465ad6d261e2733c60c3a066eebabee72f14346a (diff) | |
download | meson-cec3edc08a6cd6a89761c49292ba6a3bace8b3c1.zip meson-cec3edc08a6cd6a89761c49292ba6a3bace8b3c1.tar.gz meson-cec3edc08a6cd6a89761c49292ba6a3bace8b3c1.tar.bz2 |
Unify message(), format() and fstring formatting
Share a common function to convert objects to display strings for
consistency.
While at it, also add support for formatting user options.
Diffstat (limited to 'mesonbuild/interpreterbase')
-rw-r--r-- | mesonbuild/interpreterbase/__init__.py | 8 | ||||
-rw-r--r-- | mesonbuild/interpreterbase/helpers.py | 25 | ||||
-rw-r--r-- | mesonbuild/interpreterbase/interpreterbase.py | 13 |
3 files changed, 37 insertions, 9 deletions
diff --git a/mesonbuild/interpreterbase/__init__.py b/mesonbuild/interpreterbase/__init__.py index f0c2002..3cb9530 100644 --- a/mesonbuild/interpreterbase/__init__.py +++ b/mesonbuild/interpreterbase/__init__.py @@ -35,6 +35,7 @@ __all__ = [ 'default_resolve_key', 'flatten', 'resolve_second_level_holders', + 'stringifyUserArguments', 'noPosargs', 'noKwargs', @@ -134,6 +135,11 @@ from .exceptions import ( ) from .disabler import Disabler, is_disabled -from .helpers import default_resolve_key, flatten, resolve_second_level_holders +from .helpers import ( + default_resolve_key, + flatten, + resolve_second_level_holders, + stringifyUserArguments, +) from .interpreterbase import InterpreterBase from .operator import MesonOperator diff --git a/mesonbuild/interpreterbase/helpers.py b/mesonbuild/interpreterbase/helpers.py index 2196b4e..f2ee1b1 100644 --- a/mesonbuild/interpreterbase/helpers.py +++ b/mesonbuild/interpreterbase/helpers.py @@ -14,13 +14,15 @@ from __future__ import annotations from .. import mesonlib, mparser -from .exceptions import InterpreterException +from .exceptions import InterpreterException, InvalidArguments +from ..coredata import UserOption + import collections.abc import typing as T if T.TYPE_CHECKING: - from .baseobjects import TYPE_var, TYPE_kwargs + from .baseobjects import TYPE_var, TYPE_kwargs, SubProject def flatten(args: T.Union['TYPE_var', T.List['TYPE_var']]) -> T.List['TYPE_var']: if isinstance(args, mparser.StringNode): @@ -54,3 +56,22 @@ def default_resolve_key(key: mparser.BaseNode) -> str: if not isinstance(key, mparser.IdNode): raise InterpreterException('Invalid kwargs format.') return key.value + +def stringifyUserArguments(args: TYPE_var, subproject: SubProject, quote: bool = False) -> str: + if isinstance(args, str): + return f"'{args}'" if quote else args + elif isinstance(args, bool): + return 'true' if args else 'false' + elif isinstance(args, int): + return str(args) + elif isinstance(args, list): + return '[%s]' % ', '.join([stringifyUserArguments(x, subproject, True) for x in args]) + elif isinstance(args, dict): + l = ['{} : {}'.format(stringifyUserArguments(k, subproject, True), + stringifyUserArguments(v, subproject, True)) for k, v in args.items()] + return '{%s}' % ', '.join(l) + elif isinstance(args, UserOption): + from .decorators import FeatureNew + FeatureNew.single_use('User option in string format', '1.3.0', subproject) + return stringifyUserArguments(args.printable_value(), subproject) + raise InvalidArguments('Value other than strings, integers, bools, options, dictionaries and lists thereof.') diff --git a/mesonbuild/interpreterbase/interpreterbase.py b/mesonbuild/interpreterbase/interpreterbase.py index 9aff5b9..d23a23d 100644 --- a/mesonbuild/interpreterbase/interpreterbase.py +++ b/mesonbuild/interpreterbase/interpreterbase.py @@ -40,7 +40,7 @@ from .exceptions import ( from .decorators import FeatureNew from .disabler import Disabler, is_disabled -from .helpers import default_resolve_key, flatten, resolve_second_level_holders +from .helpers import default_resolve_key, flatten, resolve_second_level_holders, stringifyUserArguments from .operator import MesonOperator from ._unholder import _unholder @@ -433,11 +433,12 @@ class InterpreterBase: var = str(match.group(1)) try: val = _unholder(self.variables[var]) - if not isinstance(val, (str, int, float, bool)): - raise InvalidCode(f'Identifier "{var}" does not name a formattable variable ' + - '(has to be an integer, a string, a floating point number or a boolean).') - - return str(val) + if isinstance(val, (list, dict)): + FeatureNew.single_use('List or dictionary in f-string', '1.3.0', self.subproject, location=self.current_node) + try: + return stringifyUserArguments(val, self.subproject) + except InvalidArguments as e: + raise InvalidArguments(f'f-string: {str(e)}') except KeyError: raise InvalidCode(f'Identifier "{var}" does not name a variable.') |