diff mbox series

[1/1] utils: add is_enabled function

Message ID 20260311131917.2203296-1-pierre-loup.gosse@smile.fr
State New
Headers show
Series [1/1] utils: add is_enabled function | expand

Commit Message

Pierre-loup GOSSE March 11, 2026, 1:19 p.m. UTC
From: Pierre-Loup GOSSE <pierre-loup.gosse@smile.fr>

Check the boolean value of the variable and return the 'truevalue' or
the 'falsevalue'.

This simplify the following syntax:

  "${@ 'truevalue' if bb.utils.to_boolean(d.getVar('VARIABLE')) else 'falsevalue'}"

To:

  "${@bb.utils.is_enabled('VARIABLE', 'truevalue', 'falsevalue', d)}"

Signed-off-by: Pierre-Loup GOSSE <pierre-loup.gosse@smile.fr>
---
Note: Once merged, a patch with the new function will be submitted to the
documentation mailing list.
---
 lib/bb/utils.py | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

Comments

Richard Purdie March 11, 2026, 11:26 p.m. UTC | #1
On Wed, 2026-03-11 at 14:19 +0100, Pierre-loup GOSSE via lists.openembedded.org wrote:
> From: Pierre-Loup GOSSE <pierre-loup.gosse@smile.fr>
> 
> Check the boolean value of the variable and return the 'truevalue' or
> the 'falsevalue'.
> 
> This simplify the following syntax:
> 
>   "${@ 'truevalue' if bb.utils.to_boolean(d.getVar('VARIABLE')) else 'falsevalue'}"
> 
> To:
> 
>   "${@bb.utils.is_enabled('VARIABLE', 'truevalue', 'falsevalue', d)}"
> 
> Signed-off-by: Pierre-Loup GOSSE <pierre-loup.gosse@smile.fr>
> ---
> Note: Once merged, a patch with the new function will be submitted to the
> documentation mailing list.
> ---
>  lib/bb/utils.py | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
> 
> diff --git a/lib/bb/utils.py b/lib/bb/utils.py
> index b2bda08cb..9937994db 100644
> --- a/lib/bb/utils.py
> +++ b/lib/bb/utils.py
> @@ -1272,6 +1272,37 @@ def to_boolean(string, default=None):
>      else:
>          raise ValueError("Invalid value for to_boolean: %s" % string)
>  
> +def is_enabled(variable, truevalue, falsevalue, d):
> +    """
> +    Check the boolean value of the variable.
> +
> +    Arguments:
> +
> +    -  ``variable``: the variable name. This will be fetched and expanded (using
> +       d.getVar(variable)).
> +    -  ``truevalue``: the value to return if the boolean value is True.
> +    -  ``falsevalue``: the value to return if the boolean value is False.
> +    -  ``d``: the data store.
> +
> +    Returns ``truevalue`` if the variable boolean value is True, ``falsevalue``
> +    otherwise.
> +    """
> +
> +    val = d.getVar(variable)
> +    if not val:
> +        return falsevalue
> +
> +    if isinstance(val, int):
> +        return truevalue if val else falsevalue
> +
> +    normalized = val.lower()
> +    if normalized in ("y", "yes", "1", "true"):
> +        return truevalue
> +    elif normalized in ("n", "no", "0", "false"):
> +        return falsevalue
> +    else:
> +        raise ValueError("Invalid value for is_enabled: %s" % val)
> +

This looks like it duplicates much of the to_boolean() code and I'd
prefer it didn't so we only have one copy of "true" and "false" values
to maintain. Can't you call to_boolean() from this function?

Cheers,

Richard
diff mbox series

Patch

diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index b2bda08cb..9937994db 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -1272,6 +1272,37 @@  def to_boolean(string, default=None):
     else:
         raise ValueError("Invalid value for to_boolean: %s" % string)
 
+def is_enabled(variable, truevalue, falsevalue, d):
+    """
+    Check the boolean value of the variable.
+
+    Arguments:
+
+    -  ``variable``: the variable name. This will be fetched and expanded (using
+       d.getVar(variable)).
+    -  ``truevalue``: the value to return if the boolean value is True.
+    -  ``falsevalue``: the value to return if the boolean value is False.
+    -  ``d``: the data store.
+
+    Returns ``truevalue`` if the variable boolean value is True, ``falsevalue``
+    otherwise.
+    """
+
+    val = d.getVar(variable)
+    if not val:
+        return falsevalue
+
+    if isinstance(val, int):
+        return truevalue if val else falsevalue
+
+    normalized = val.lower()
+    if normalized in ("y", "yes", "1", "true"):
+        return truevalue
+    elif normalized in ("n", "no", "0", "false"):
+        return falsevalue
+    else:
+        raise ValueError("Invalid value for is_enabled: %s" % val)
+
 def contains(variable, checkvalues, truevalue, falsevalue, d):
     """Check if a variable contains all the values specified.