move UserArrayOption.listify_value to mesonlib

This function is used at 3 different places and it does not justify it
as being a staticmethod instead of being a free function.
pull/12116/merge
Charles Brunet 10 months ago committed by Dylan Baker
parent b1340e9bb1
commit d08ef2c08b
  1. 27
      mesonbuild/coredata.py
  2. 5
      mesonbuild/mcompile.py
  3. 22
      mesonbuild/utils/universal.py

@ -20,7 +20,7 @@ from .mesonlib import (
PerMachineDefaultable, default_libdir, default_libexecdir,
default_prefix, default_datadir, default_includedir, default_infodir,
default_localedir, default_mandir, default_sbindir, default_sysconfdir,
split_args, OptionKey, OptionType, stringlistify,
listify_array_value, OptionKey, OptionType, stringlistify,
pickle_load
)
from .wrap import WrapMode
@ -268,29 +268,8 @@ class UserArrayOption(UserOption[T.List[str]]):
self.allow_dups = allow_dups
self.set_value(value)
@staticmethod
def listify_value(value: T.Union[str, T.List[str]], shlex_split_args: bool = False) -> T.List[str]:
if isinstance(value, str):
if value.startswith('['):
try:
newvalue = ast.literal_eval(value)
except ValueError:
raise MesonException(f'malformed option {value}')
elif value == '':
newvalue = []
else:
if shlex_split_args:
newvalue = split_args(value)
else:
newvalue = [v.strip() for v in value.split(',')]
elif isinstance(value, list):
newvalue = value
else:
raise MesonException(f'"{value}" should be a string array, but it is not')
return newvalue
def listify(self, value: T.Any) -> T.List[T.Any]:
return self.listify_value(value, self.split_args)
return listify_array_value(value, self.split_args)
def validate_value(self, value: T.Union[str, T.List[str]]) -> T.List[str]:
newvalue = self.listify(value)
@ -364,7 +343,7 @@ class UserStdOption(UserComboOption):
self.choices += gnu_stds_map.keys()
def validate_value(self, value: T.Union[str, T.List[str]]) -> str:
candidates = UserArrayOption.listify_value(value)
candidates = listify_array_value(value)
unknown = [std for std in candidates if std not in self.all_stds]
if unknown:
raise MesonException(f'Unknown {self.lang.upper()} std {unknown}. Possible values are {self.all_stds}.')

@ -16,16 +16,15 @@ from pathlib import Path
from . import mlog
from . import mesonlib
from .mesonlib import MesonException, RealPathAction, join_args, setup_vsenv
from .mesonlib import MesonException, RealPathAction, join_args, listify_array_value, setup_vsenv
from mesonbuild.environment import detect_ninja
from mesonbuild.coredata import UserArrayOption
from mesonbuild import build
if T.TYPE_CHECKING:
import argparse
def array_arg(value: str) -> T.List[str]:
return UserArrayOption.listify_value(value)
return listify_array_value(value)
def validate_builddir(builddir: Path) -> None:
if not (builddir / 'meson-private' / 'coredata.dat').is_file():

@ -7,6 +7,7 @@
from __future__ import annotations
from pathlib import Path
import argparse
import ast
import enum
import sys
import stat
@ -129,6 +130,7 @@ __all__ = [
'iter_regexin_iter',
'join_args',
'listify',
'listify_array_value',
'partition',
'path_is_in_root',
'pickle_load',
@ -1435,6 +1437,26 @@ def listify(item: T.Any, flatten: bool = True) -> T.List[T.Any]:
result.append(i)
return result
def listify_array_value(value: T.Union[str, T.List[str]], shlex_split_args: bool = False) -> T.List[str]:
if isinstance(value, str):
if value.startswith('['):
try:
newvalue = ast.literal_eval(value)
except ValueError:
raise MesonException(f'malformed value {value}')
elif value == '':
newvalue = []
else:
if shlex_split_args:
newvalue = split_args(value)
else:
newvalue = [v.strip() for v in value.split(',')]
elif isinstance(value, list):
newvalue = value
else:
raise MesonException(f'"{value}" should be a string array, but it is not')
assert isinstance(newvalue, list)
return newvalue
def extract_as_list(dict_object: T.Dict[_T, _U], key: _T, pop: bool = False) -> T.List[_U]:
'''

Loading…
Cancel
Save