ESP-BOOL-PARSER

0.2.3 · active · verified Thu Apr 16

esp-bool-parser is a lightweight Python package (version 0.2.3) by Espressif, designed for parsing boolean expressions primarily within the context of ESP-IDF applications. It supports diverse operand types, including SOC capabilities, environment variables, strings, booleans, and integers, and helps process statements based on `soc_caps` files. It is actively maintained by Espressif.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse a boolean expression string using `parse_bool_expr` and how to register a custom attribute handler with `register_addition_attribute`. Note that the evaluation of the parsed expression (`stmt.get_value()`) heavily relies on an internal `ChipAttr` context, which is typically set up within an ESP-IDF build environment. The provided `evaluate_mock` function is a placeholder for illustrating the concept of evaluation and is not part of the library itself.

import esp_bool_parser

# Example 1: Parsing a simple boolean expression
stmt_string = 'IDF_TARGET == "esp32" and CONFIG_FOO'
stmt = esp_bool_parser.parse_bool_expr(stmt_string)

# You would typically have a ChipAttr-like object or context to evaluate this.
# For demonstration, let's mock a simple evaluation.
# In a real ESP-IDF context, 'target' and 'config_name' would come from build system.
# For example, checking against 'esp32' target and a configuration where CONFIG_FOO is True

# A simple mock evaluation function (not part of the library, for example purposes)
def evaluate_mock(parsed_expression, target, config_values):
    # This is a highly simplified representation.
    # The actual library's .get_value() interacts with an internal ChipAttr instance.
    # For this example, we assume parsed_expression has an internal representation
    # that can be evaluated with provided context.
    try:
        # The actual get_value method is more complex and handles SOC_CAPS, ENV_VARs, etc.
        # This line is illustrative, assuming it resolves based on target and config_values
        # In a real scenario, ChipAttr would handle the context.
        result = parsed_expression.get_value(target, config_values.get('config_name', ''))
        return result
    except Exception as e:
        print(f"Error during evaluation: {e}")
        return None

# Example usage of evaluation (simplified)
mock_config = {'config_name': 'default', 'CONFIG_FOO': True}
# Note: Direct call to stmt.get_value requires proper ChipAttr context setup internally.
# The example from docs is `result = stmt.get_value("esp32", "config_name")`
# which implies `"config_name"` is a string identifier for a configuration context.
# For a true runnable quickstart, it's difficult to fully simulate `ChipAttr`'s internal state
# without more knowledge or a simpler public API for evaluation. 
# The primary use is *parsing* the expression. Evaluation depends on the runtime context.
# Let's show the parsing and the registration of an attribute.

print(f"Successfully parsed expression: '{stmt_string}'")

# Example 2: Registering an additional attribute
import typing as t

def my_custom_action(target: str, config_name: str, **kwargs: t.Any) -> str:
    print(f"Custom attribute 'MY_ATTR' called for target: {target}, config: {config_name}")
    return f"Processed_{target}_{config_name}"

esp_bool_parser.register_addition_attribute("MY_ATTR", my_custom_action)

# Now, expressions can use MY_ATTR (e.g., 'MY_ATTR == "Processed_esp32_config"')

view raw JSON →