aboutsummaryrefslogtreecommitdiff
path: root/mesonbuild/interpreterbase/helpers.py
diff options
context:
space:
mode:
authorXavier Claessens <xavier.claessens@collabora.com>2023-05-30 09:27:42 -0400
committerXavier Claessens <xclaesse@gmail.com>2023-08-02 13:35:29 -0400
commitcec3edc08a6cd6a89761c49292ba6a3bace8b3c1 (patch)
treeca8f4504ead8fe875ef582299b2cbb9f07d85688 /mesonbuild/interpreterbase/helpers.py
parent465ad6d261e2733c60c3a066eebabee72f14346a (diff)
downloadmeson-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/helpers.py')
-rw-r--r--mesonbuild/interpreterbase/helpers.py25
1 files changed, 23 insertions, 2 deletions
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.')