aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2021-08-10 15:06:44 -0700
committerDylan Baker <dylan@pnwbakers.com>2021-08-30 18:05:01 -0700
commitd87b3c7b3f114446792ca5fb5867f538808caeb1 (patch)
tree89488062b68640bc4d7ebd97f6326feeeda08da8
parent75d2ec9a40c2712d99f23a46462c0ef22246e581 (diff)
downloadmeson-d87b3c7b3f114446792ca5fb5867f538808caeb1.zip
meson-d87b3c7b3f114446792ca5fb5867f538808caeb1.tar.gz
meson-d87b3c7b3f114446792ca5fb5867f538808caeb1.tar.bz2
interpreter/type_checking: Add convertor to env keyword argument
This does the conversion to an EnvironmentVariables, so that the receiver always gets a EnvironmentVariables object and not a list, dict, or string
-rw-r--r--mesonbuild/interpreter/type_checking.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/mesonbuild/interpreter/type_checking.py b/mesonbuild/interpreter/type_checking.py
index 0ebb8cd..4eed965 100644
--- a/mesonbuild/interpreter/type_checking.py
+++ b/mesonbuild/interpreter/type_checking.py
@@ -134,8 +134,6 @@ def _env_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Di
split = v.split('=', 1)
if len(split) == 1:
return f'"{v}" is not two string values separated by an "="'
- # We have to cast here, assert isn't good enough to narrow from
- # Tuple[str, ...] -> Tuple[str, str]
return None
if isinstance(value, str):
@@ -159,8 +157,23 @@ def _env_validator(value: T.Union[EnvironmentVariables, T.List['TYPE_var'], T.Di
return None
+def _env_convertor(value: T.Union[EnvironmentVariables, T.List[str], T.Dict[str, str], str, None]) -> EnvironmentVariables:
+ def splitter(input: str) -> T.Tuple[str, str]:
+ a, b = input.split('=', 1)
+ return (a.strip(), b.strip())
+
+ if isinstance(value, (str, list)):
+ return EnvironmentVariables(dict(splitter(v) for v in listify(value)))
+ elif isinstance(value, dict):
+ return EnvironmentVariables(value)
+ elif value is None:
+ return EnvironmentVariables()
+ return value
+
+
ENV_KW: KwargInfo[T.Union[EnvironmentVariables, T.List, T.Dict, str, None]] = KwargInfo(
'env',
- (EnvironmentVariables, list, dict, str),
+ (EnvironmentVariables, list, dict, str, NoneType),
validator=_env_validator,
+ convertor=_env_convertor,
)